how can I draw a rectangle on a image?

Hello friends I'm with a problem! I have a vector with x and y coordinates and I need to draw a rectangle over an image.A rectangle with the dimensions 3x3 with the red line from the centroid point how can I do this?

 Respuesta aceptada

Walter Roberson
Walter Roberson el 19 de Jun. de 2015
For any one (x, y) centroid pair:
linelen = 3;
pixbefore = floor((linelen - 1)/2);
pixafter = linelen - 1 - pixbefore;
paintcolor = [255, 0, 0];
for K = 1 : length(paintcolor)
YourImage(y - pixbefore : y + pixafter, [x - pixbefore, x + pixafter], K) = red(K); %left and right edge
YourImage([y - pixbefore + 1, y + pixafter - 1], x - pixbefore : x + pixafter, K) = red(K); %top and bottom edge
end
If I was doing a whole bunch of them, I would use a slightly different approach

6 comentarios

Walter Roberson
Walter Roberson el 19 de Jun. de 2015
Is the question about writing the rectangle into the image array, or is the question about drawing a rectangle on the graphics display on top of an existing image?
If the question is about drawing on the graphics display, see the command rectangle().
Biza Ferreira
Biza Ferreira el 19 de Jun. de 2015
Editada: Biza Ferreira el 19 de Jun. de 2015
is the question about drawing a rectangle on the graphics display on top of an existing image, I have done all you said and ... It gave me back an error
if true
% code
Undefined function 'red' for input arguments of type 'double'.
Error in impressoes (line 156)
BSKE(BIFU_XY(:,2) - pixbefore : BIFU_XY(:,2) + pixafter, [BIFU_XY(:,1) - pixbefore, BIFU_XY(:,1) + pixafter], K) = red(K) %left and right
edge
end
the code:
if true
% code
BSKE=imread('image.tif')
DIST=DistanciaEuclid(BIFU_XY);
SpuriousMinutae=DIST<D;
[i,j]=find(SpuriousMinutae);
BIFU_XY(i,:)=[];
linelength = 3;
pixbefore = floor((linelength - 1)/2)
pixafter = linelength - 1 - pixbefore
paintcolor = [255, 0, 0];
for K = 1 : length(paintcolor)
BSKE(BIFU_XY(:,2) - pixbefore : BIFU_XY(:,2) + pixafter, [BIFU_XY(:,1) - pixbefore, BIFU_XY(:,1) + pixafter], K) = red(K);
BSKE([BIFU_XY(:,2) - pixbefore + 1, BIFU_XY(:,2) + pixafter - 1], BIFU_XY(:,1) - pixbefore : BIFU_XY(:,1) + pixafter, K) = red(K);
end
end
Image Analyst
Image Analyst el 19 de Jun. de 2015
Try paintcolor(K) instead of red(K).
Right, I renamed a variable as I went.
linelen = 3;
pixbefore = floor((linelen - 1)/2);
pixafter = linelen - 1 - pixbefore;
paintcolor = [255, 0, 0]; %red
for K = 1 : length(paintcolor)
YourImage(y - pixbefore : y + pixafter, [x - pixbefore, x + pixafter], K) = paintcolor(K); %left and right edge
YourImage([y - pixbefore + 1, y + pixafter - 1], x - pixbefore : x + pixafter, K) = paintcolor(K); %top and bottom edge
end
For drawing on the graphics display, you should be using rectangle() unless you have the Computer Vision toolbox.
linelen = 3; %data units now, not pixels
paintcolor = [1 0 0]; %red
rectangle('Position', [x - linelen/2, y - linelen/2, linelen, linelen], 'EdgeColor', paintcolor);
This would be for a rectangle centered around the coordinates. If you want a rectangle whose bottom left is the given coordinates then
linelen = 3; %data units now, not pixels
paintcolor = [1 0 0]; %red
rectangle('Position', [x, y, linelen, linelen], 'EdgeColor', paintcolor);
Biza Ferreira
Biza Ferreira el 19 de Jun. de 2015
Editada: Biza Ferreira el 19 de Jun. de 2015
tanks for all

Iniciar sesión para comentar.

Más respuestas (2)

Dima Lisin
Dima Lisin el 19 de Jun. de 2015

0 votos

Hi Biza,
Try using the insertShape function in the Computer Vision System Toolbox.
Edwards Ramirez
Edwards Ramirez el 22 de En. de 2019
I had to write it on my own. Here the used function:
function imageReturn = drawRectangle(image, Xmin, Ymin, width, height)
imageReturn = image;
intensity = 255;
X1 = Ymin;
Y1 = Xmin;
X2 = Ymin + width;
Y2 = Xmin + height;
Xcenter = round((X1+X2)/2);
Ycenter = round((Y1+Y2)/2);
for i=X1:X2
imageReturn(i,Y1,1)=intensity;
end
for i=X1:X2
imageReturn(i,Y2,1)=intensity;
end
for i=Y1:Y2
imageReturn(X1,i,1)=intensity;
end
for i=Y1:Y2
imageReturn(X2,i,1)=intensity;
end
%Comment the following for cycles if you don't want the cross in the centroid
for i=Xcenter-3:Xcenter+3
imageReturn(i,Ycenter,1)=intensity;
end
for i=Ycenter-3:Ycenter+3
imageReturn(Xcenter,i,1)=intensity;
end
end

1 comentario

Image Analyst
Image Analyst el 22 de En. de 2019
Editada: Image Analyst el 22 de En. de 2019
Well, okay. Your code burns the rectangle into the image (although it's very confusing because you're setting x equal to y and y equal to x instead of just doing it like you should). The poster said he wants to "draw a rectangle over an image", which I take to mean in the graphical overlay over the image, not INTO the image itself. You could put a rectangle into the graphical overlay over the image with the rectangle() function. If you want to burn the rectangle into the image, one could use insertShape like Dima said in his answer (though it requires the Computer Vision System Toolbox).
By the way, your for loops could be vectorized, so
for i=X1:X2
imageReturn(i,Y1,1)=intensity;
end
would become
imageReturn(Ymin, XMin:(Xmin + width - 1), 1) = intensity;
Notice that I corrected your confusing indexing. It's a very common beginner mistake and that is thinking arrays are indexed as (x,y) instead of (y, x). I know you tried to correct it with your swapping of x and y but I personally think that just made it more confusing. Arrays are indexed as (row, column), so the first index is row, or Y, not X as you had it.
You might want to edit your code and correct that lest anyone every copy it and try to use it and run into problems that confuse them.

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 19 de Jun. de 2015

Editada:

el 22 de En. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by