Converting a rectangle position on an image to image matrix index

8 visualizaciones (últimos 30 días)
Hi, I have drawn an greyscale image from a matrix using imshow. I used Imrect to draw a rectangle on a region of the image. from Imrect i get the xmin, ymin hight and width of the rectangle. but what i really need is to know which pixels it contains e.g the image matrix indexes. how do i do this conversion? thanks

Respuesta aceptada

Matt J
Matt J el 31 de Oct. de 2012
Editada: Matt J el 31 de Oct. de 2012
If H is the handle returned by IMRECT, then you can use its createMask method to get a logical index mask of the region,
Indices=H.createMask;
  2 comentarios
Itzik Ben Shabat
Itzik Ben Shabat el 31 de Oct. de 2012
great! did this and its almost what i wanted. i wanted the indexes (rows and columns) if the selected area. is there a simple way to convert the index mask to the (row,column) format? thanks a lot, this helped!
Matt J
Matt J el 31 de Oct. de 2012
Editada: Matt J el 31 de Oct. de 2012
Yes. You use the FIND function
[I,J]=find(mask);
I hope you realize, however, that this conversion is not necessary for indexing the image. You could just use logical indexing:
values = Image(mask);

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 31 de Oct. de 2012
Here's a snippet from my code:
hBox = imrect;
roiPosition = wait(hBox);
roiPosition
% Erase all previous lines.
ClearLinesFromAxes(handles);
xCoords = [roiPosition(1), roiPosition(1)+roiPosition(3), roiPosition(1)+roiPosition(3), roiPosition(1), roiPosition(1)];
yCoords = [roiPosition(2), roiPosition(2), roiPosition(2)+roiPosition(4), roiPosition(2)+roiPosition(4), roiPosition(2)];
% Plot the mask as an outline over the image.
hold on;
plot(xCoords, yCoords, 'linewidth', 2);
Alternatively, if you want, you can use rbbox instead of imrect. Here's a snippet showing how to do that:
k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint'); % button down detected
finalRect = rbbox; % return figure units
point2 = get(gca,'CurrentPoint'); % button up detected
point1 = point1(1,1:2); % extract x and y
point2 = point2(1,1:2);
p1 = min(point1,point2); % calculate locations
offset = abs(point1-point2); % and dimensions
xCoords = [p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)];
yCoords = [p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)];
x1 = round(xCoords(1));
x2 = round(xCoords(2));
y1 = round(yCoords(5));
y2 = round(yCoords(3));
hold on
axis manual
plot(xCoords, yCoords); % redraw in dataspace units
croppedImage = imgOriginal(y1:y2,x1:x2,:);
  5 comentarios
Matt J
Matt J el 1 de Nov. de 2012
So I don't really understand what you were wondering about - I guess it never applies to me.
I just meant that, for example, when you use get(gca,'CurrentPoint') to obtain coordinates of the box, the output will depend on whether the axes is in matrix mode (axis ij) or xy mode (axis xy). Since the task of the OP is to obtain the pixel coordinates inside the box, you have to make sure the former is true. Conversely, imrect.createMask seems to take care of that for you innately.
Image Analyst
Image Analyst el 1 de Nov. de 2012
Well since I always have loaded an image in first, before calling plot() to plot the box, I guess it's always in pixel mode for me.

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with Image Processing Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by