Sample random pixels from multiple region of interests from an Image

8 views (last 30 days)
Hello everyone,
I got a binary image which needs to be divided in four stripes/regions. The regions should be divided in pairs e.g. white and red regions. Thereafter I need select a random pair of region and thereafter sample 1 pixel for each stripe of those pairs (white or red).
Each of these stripes/regions should contain 25 % of the total amount of white pixels (stripes don't have equal size), which is shown in the downbelow image.
I now need to sample one pixel for each stripe, but it should be done in pairs (white or red stripes). It should be random if I sample in the red or white area, but if I sample in the white area I need to get a pixel from stripe 1 and 3 and visa versa for the red stripes.
So the result will look like this with 3 samples(square, star and circle) for each stripe pair. ( I added the sympols on my picture by using PS)
This is the code I used to divide my image into 4 intervals:
% code
% Count total number of white cells in the binary image
nWhite = sum(BW(:)); % count all white elements in the image
% Count white cells in a row
sumAlongRows = sum(BW,2)';
% Divide the white rows in 25, 50 and 75 %
w25 = nWhite*0.25;
w50 = nWhite*0.50;
w75 = nWhite*0.75;
w100= nWhite;
% Cummulate the numbers until the different intervals
cumAlongRows = cumsum(sumAlongRows);
nWhite25 = cumAlongRows(cumAlongRows<w25);
nWhite50 = cumAlongRows(cumAlongRows<w50);
nWhite75 = cumAlongRows(cumAlongRows<w75);
nWhite100 = cumAlongRows(cumAlongRows<w100)
I have looked at different functions to select random pixels such as:
% code
A = magic(4)
n = 1;
p = randperm(numel(A),n)
v = A(p)
[r,c] = ind2sub(size(A),p)
However, it doesn't solve my problem, since I need to look at different intervals. None, of the black(0) values should be included, since the white values should have an equal chance to be picked. The samples shouldn't "hit" the edge/border of the white pixels.
I hope someone got a clue how to solve this problem.
Thank you very much, Nick

Accepted Answer

John BG
John BG on 21 Jul 2017
Edited: John BG on 21 Jul 2017
Hi Mr Larsen
1. Acquiring image, which is a screenshot from from question, attached to this answer
clear all;clc;close all
A=imread('001.jpg');
A=A(:,:,1);
B=imbinarize(A);
figure(1);imshow(B);
2.
measuring quarter area strips
[sz1 sz2]=size(B); % sz1 vertical sz2 horizontal
L=[]
for k=1:1:sz1
L=[L sum(B(k,:))];
end
L2=cumsum(L);
figure(2);
yyaxis 'left';plot(L);
yyaxis 'right';plot(L2);grid on
Ar=sum(L); % total amount white pixels
nL=[1:1:length(L2)];
there is command quantile that apparently gets the quarter area markers, but for this answer I chose to use command find
[n1,ny25,v]=find(L2==max(L2(L2<=Ar*.25)));
[n1,ny50,v]=find(L2==max(L2(L2<=Ar*.50)));
[n1,ny75,v]=find(L2==max(L2(L2<=Ar*.75)));
% comment red grey shading
figure(1);hold all;
plot(ones(1,sz2)*ny25,'r');
plot(ones(1,sz2)*ny50,'r');
plot(ones(1,sz2)*ny75,'r');
% figure 003
3.
Checking acquired points correspond to all white points
[nyB,nxB,v]=find(B>0);
plot(nxB,nyB,'g.') % check
% figure 004
All while points are contained in variable P
P=[nxB nyB]; % all white points
4.
acquiring contour points
B2=del2(B);
figure(3);imshow(B2) % finding all contour points
% 004-2
[nyB2,nxB2,v]=find(B2>0);
Pcontour=[nyB2 nxB2];
figure(3);hold all;plot(nxB2,nyB2,'g.') % check contour
.
5. PENDING
excluding contour points from P
% P=P-Pcontour % P1=P(:,1)';P2=P(:,2)';Pc1=Pcontour(:,1)';Pc2=Pcontour(:,2)'; % [in,on]=inpolygon(P1,P2,Pc1,Pc2); % figure;plot(Pc2([end:-1:1]),Pc1,'r.') % figure;plot(P1(~in),P2(~in),'r*') % % or % % for k=nB % PxPc1=intersect(P(k,1),Pcontour(:,1)); % end
% nB=[1:1:numel(nyB)] %
6.
Once sector area points gathered, we can proceed to randomly scatter points within each sector
close all
[row,col,v]=find(nyB<ny25); % points in 1st quarter area
P25=[nxB(row) nyB(row)];
figure(4);imshow(B);hold on;
plot(ones(1,sz2)*ny25,'r');
plot(P25(:,1),P25(:,2),'Color',[1 .5 .5],'LineStyle','none','Marker','.') % check points 1st quarter area
% 005-1
% scatter points across 1st quarter area sector
nP25=[1:1:length(P25)];
N=3; % amount random points to scatter
s=1
str_color=['g' 'b' 'y'];
str_marker=['d' 'o' 's'];
Psct25=[]; % counter for scattered points across 1st quarter area
while s<=N
n0=randi(numel(nP25),1,1);
figure(4);hold all;plot(P25(n0,1),P25(n0,2),'LineStyle','none','Color',str_color(s),'Marker',str_marker(s)); % (d)iamond o:circle (s)quare + * x . ^ v < > (p)entagram (h(exagram
Psct25=[Psct25;P25(n0,:)]; % recording scattered points
P25(n0,:)=[];nP25(n0)=[];
s=s+1;
end
% 006-1
[row_up,col,v]=find(nyB<ny50); % points in 2nd quarter area
[row_down,col,v]=find(nyB>ny25);
row=intersect(row_up,row_down);
P50=[nxB(row) nyB(row)];
figure(5);imshow(B);hold on;
plot(ones(1,sz2)*ny25,'r');
plot(ones(1,sz2)*ny50,'r');
plot(P50(:,1),P50(:,2),'Color',[1 .5 .5],'LineStyle','none','Marker','.') % check points 2nd quarter area
% 005-2
% scatter points across 2nd quarter area sector
nP50=[1:1:length(P50)];
N=3; % amount random points to scatter
s=1
str_color=['g' 'b' 'y'];
str_marker=['d' 'o' 's'];
Psct50=[]; % counter for scattered points across 2nd quarter area
while s<=N
n0=randi(numel(nP25),1,1);
figure(5);hold all;plot(P50(n0,1),P50(n0,2),'LineStyle','none','Color',str_color(s),'Marker',str_marker(s)); % (d)iamond o:circle (s)quare + * x . ^ v < > (p)entagram (h(exagram
Psct50=[Psct50;P50(n0,:)]; % recording scattered points
P50(n0,:)=[];nP50(n0)=[];
s=s+1;
end
[row_up,col,v]=find(nyB<ny75); % points in 3rd quarter area
[row_down,col,v]=find(nyB>ny50);
row=intersect(row_up,row_down);
P75=[nxB(row) nyB(row)];
figure(6);imshow(B);hold on;
plot(ones(1,sz2)*ny75,'r');
plot(ones(1,sz2)*ny50,'r');
plot(P75(:,1),P75(:,2),'Color',[1 .5 .5],'LineStyle','none','Marker','.') % check points 3rd quarter area
% 005-3
% scatter points across 3rd quarter area sector
nP75=[1:1:length(P75)];
N=3; % amount random points to scatter
s=1
str_color=['g' 'b' 'y'];
str_marker=['d' 'o' 's'];
Psct75=[]; % counter for scattered points across 3rd quarter area
while s<=N
n0=randi(numel(nP75),1,1);
figure(6);hold all;plot(P75(n0,1),P75(n0,2),'LineStyle','none','Color',str_color(s),'Marker',str_marker(s)); % (d)iamond o:circle (s)quare + * x . ^ v < > (p)entagram (h(exagram
Psct75=[Psct75;P75(n0,:)]; % recording scattered points
P75(n0,:)=[];nP75(n0)=[];
s=s+1;
end
[row,col,v]=find(nyB>ny75); % points in 4th quarter area
P100=[nxB(row) nyB(row)];
figure(7);imshow(B);hold on;
plot(ones(1,sz2)*ny75,'r');
plot(P100(:,1),P100(:,2),'Color',[1 .5 .5],'LineStyle','none','Marker','.') % check points 1st quarter area
% 005-4
% scatter points across 4th quarter area sector
nP100=[1:1:length(P100)];
N=3; % amount random points to scatter
s=1
str_color=['g' 'b' 'y'];
str_marker=['d' 'o' 's'];
Psct100=[]; % counter for scattered points across 4th quarter area
while s<=N
n0=randi(numel(nP100),1,1);
figure(7);hold all;plot(P100(n0,1),P100(n0,2),'LineStyle','none','Color',str_color(s),'Marker',str_marker(s)); % (d)iamond o:circle (s)quare + * x . ^ v < > (p)entagram (h(exagram
Psct100=[Psct100;P100(n0,:)]; % recording scattered points
P100(n0,:)=[];nP100(n0)=[];
s=s+1;
end
.
7.
the scattered points are readily available here
Psct25
=
189 224
187 188
204 221
Psct50
=
270 239
210 234
233 237
Psct75
=
162 284
234 257
165 262
Psct100
=
184 305
199 320
215 317
8. Comment
A function could be built to reduce amount code lines, for each area sector
9. Comment
the displayed points in the sector figures correspond to different run than these point values
.
So, Mr Larsen
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
additional comment:
I am half way to concisely exclude edge points from P. Tried couple for loops, and command union, but union apparently is 1D
  4 Comments
Ahlam Benahmeida
Ahlam Benahmeida on 16 Sep 2018
how can I get a number of random pixels from a color image into a matrix? any tips, please?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!