create binary image with specific parameters

Hi, I need to create a binary image like the following from the following input arguments:
  • Number of rows in the binary image. nR
  • Number of columns in the binary image. nC
  • A MX8 matrix containing the position information of each rectangle. In each row you have the information of each one of the rectangles and by columns you have that the odd ones correspond to X coordinates while the even columns to the Y coordinates of the vertices of the rectangle. data. I give a example of data matrix.
nR = 50;
nC = 50;
A = false(nR,nC);
figure, imshow(A,'InitialMagnification','fit')
hold on
for i = 1 : size(data,1)
plot(data(i,1:2:end),data(i,2:2:end),'*')
end
This is what i have as input variables.
Thank you so much for all. What I need is that the pixels that are inside the rectangles delimited by the vertices pass to take logical value 1 of the possible form, because in the accomplishment the number of rectangles surpasses the 10000.

 Risposta accettata

You need to proceed somethings like this:
nR = 100 ; nC = 100 ;
I = zeros(nR,nC) ;
I(50:60,50:60) = 1 ; % 50:60 are the rows and column indices where I want white pixels
imshow(I)

9 Commenti

I forgot to attach the data array, I just did it, and as you can see for this kind of data it doesn't work to do it that way, even if you round up the values to be pixels.
nR = 100 ; nC = 100 ;
I = zeros(nR,nC) ;
[X,Y] = meshgrid(1:nC,1:nR) ;
x = data(:,1:2:end) ;
y = data(:,2:2:end) ;
x = round(x) ;
y = round(y) ;
for i = 1:4
idx = inpolygon(X,Y,x(i,:),y(i,:)) ;
I(idx) = 1 ;
end
imshow(I) ;
And you know a faster method? Because in the real case with
nR = 7456
nC = 7692
and data size of 360X8 the time is 51.699509 seconds
nR = 100 ; nC = 100 ;
I = zeros(nR,nC) ;
[X,Y] = meshgrid(1:nC,1:nR) ;
x = data(:,1:2:end) ;
y = data(:,2:2:end) ;
x = round(x) ;
y = round(y) ;
for i = 1:4
[Xi,Yi] = meshgrid(min(x(i,:)):max(x(i,:)),min(y(i,:)):max(y(i,:))) ;
idx = inpolygon(Xi,Yi,x(i,:),y(i,:));
idx = sub2ind([nR nC],Yi(idx1), Xi(idx1)) ;
I(idx) = 1 ;
end
imshow(I) ;
Wow yes, thank you so much for all your help, but there is a small mistake in your code:
nR = 100 ; nC = 100 ;
I = zeros(nR,nC) ;
[X,Y] = meshgrid(1:nC,1:nR) ;
x = data(:,1:2:end) ;
y = data(:,2:2:end) ;
x = round(x) ;
y = round(y) ;
for i = 1:4
[Xi,Yi] = meshgrid(min(x(i,:)):max(x(i,:)),min(y(i,:)):max(y(i,:))) ;
idx1 = inpolygon(Xi,Yi,x(i,:),y(i,:));
idx = sub2ind([nR nC],Yi(idx1), Xi(idx1)) ;
I(idx) = 1 ;
end
imshow(I) ;
How does this handle the requirement for a rotated rectangle?
@Image Analyst: inpolygon gives the points lying inside the given rectangles.
@Alajendro Fernandez: it is a typo error.
Yes but it has erros on rotated rectangles. In some cases it doesn't work properly
Alajendro, I was going to suggest an entirely different approach using poly2mask() but since you've accepted this answer, it looks like you figured everything out, like how to get the coordinates from the colored asterisks in the image, how to group them together by color, and how to fill them out to a rectangle, either rotated or not.

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by