Drawing a rectangle inside ROI

7 visualizaciones (últimos 30 días)
Sunetra Banerjee
Sunetra Banerjee el 13 de Jul. de 2021
Comentada: Sunetra Banerjee el 15 de Jul. de 2021
I have an image with several ROIs(polygon). I need to calculate the middle points of the two sides of the polygon and therefore I need to draw a rectangle inside the polygons and need the corner points' coordinated of each rectangle. Minimum bounding box did not work for my image as I need to fit is inside the ROIs. Could you please help me with this?
I have attached the image.
  2 comentarios
Image Analyst
Image Analyst el 14 de Jul. de 2021
Editada: Image Analyst el 14 de Jul. de 2021
Please attach a mat file with the coordinates of the polygons or ROI objects with the paper clip icon.
And is the background image of blobs used for anything, or does it basically not matter at all?
Those polygons don't seem to have anything to do with the blobs. I know you think that you need the middle of the sides of the polygons, but what do you really want to know? What do you want to know such that you think getting the middle of the sides will get it for you? If you had those points in yellow, then what? What's next?
Sunetra Banerjee
Sunetra Banerjee el 14 de Jul. de 2021
Hi, Let me clarify my objective for middle points of the ROI polygon. As you can see in the figure, each blob is titled in different ways (slopes) and my objective is to calculate the slope of these ROI blobs individually. The approach I was talking was approximating the blob to a largest inscribed rectangle and connecting the middle points of bilateral sides and calculating the slope from there.
The main output Matlab image is attached via paperclip.

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 14 de Jul. de 2021
Why not simply ask regionprops for the angle each is tilted at. Also ask it for the centroid and image if you want.
props = regionprops(binaryImage, 'Orientation', 'Centroid', 'Image');
allAngles = [props.Orientation] % Angles in degrees.
allSlopes = atand(allAngles) % Get slopes from angles.
I'm not sure what you want to do after that, but it seems like your polygons are not really needed.
  4 comentarios
Image Analyst
Image Analyst el 14 de Jul. de 2021
Editada: Image Analyst el 14 de Jul. de 2021
There is not one slope. There are 6, one for every blob. You must have run it on an image that had only one blob in it. Here is a full demo:
% Demo to find the orientation of a set of blobs and draw a line through the centroid at that angle.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
grayImage = imread('blob_orientations.PNG');
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Extract the red channel (so the magenta lines will be white).
grayImage = grayImage(:, :, 1);
end
%--------------------------------------------------------------------------------------------------------
% Binarize
mask = grayImage > 128;
%--------------------------------------------------------------------------------------------------------
% Display the image.
imshow(mask, []);
axis('on', 'image');
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
ylabel('Y, line, row', 'FontSize', fontSize);
xlabel('X, column', 'FontSize', fontSize);
% Measure the blobs. Find the centroid, bounding box, orientation (angle), and area.
props = regionprops(mask, 'Orientation', 'Centroid', 'BoundingBox', 'Area');
allAngles = [props.Orientation] % Angles in degrees.
allSlopes = atand(allAngles) % Optional: Get slopes from angles.
bb = vertcat(props.BoundingBox); % Get bounding box into a matrix where columns are [xLeft, yTop, width, height]
% Update title to show how many blobs we found.
numBlobs = numel(props)
caption = sprintf('Binary Image with %d Blobs', numBlobs);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Plot line at that angle going through image
hold on;
for k = 1 : numel(props)
% Plot bounding box of each blob in cyan.
rectangle('Position', bb(k, :), 'EdgeColor', 'c');
x = props(k).Centroid(1);
y = props(k).Centroid(2);
% Determine starting and stopping coordinates of the line's endpoints.
lineLength = 1.4 * bb(k, 3); % Whatever length you want.
% Use max and min to clip it to image boundaries.
x1 = x - (lineLength/2) * cosd(allAngles(k));
x2 = x + (lineLength/2) * cosd(allAngles(k));
y1 = y + (lineLength/2) * sind(allAngles(k));
y2 = y - (lineLength/2) * sind(allAngles(k));
% Draw the line in red.
line([x1, x2], [y1, y2], 'Color', 'r', 'LineWidth', 2);
% Draw the centroid in green.
plot(x, y, 'g.', 'MarkerSize', 30);
% Put area atop the bounding box
caption = sprintf(' Blob #%d Area\n = %d pixels', k, props(k).Area);
xt = bb(k, 1) + bb(k, 3); % Right side of bounding box.
yt = bb(k, 2) + bb(k, 4)/2; % Middle of bounding box.
text(xt, yt, caption, 'VerticalAlignment', 'middle', 'Color', 'y', 'FontSize', 16, 'FontWeight', 'bold');
end
If you want the blobs sorted from top to bottom instead of left to right, replace existing regionprops() line of code with this set of lines:
% Measure the blobs. Find the centroid, bounding box, orientation (angle), and area.
props = regionprops(mask, 'Orientation', 'Centroid', 'BoundingBox', 'Area');
% Now blobs are sorted from leftmost to rightmost.
% Optional: resort the blob labels from top to bottom instead of left to right.
bb = vertcat(props.BoundingBox); % Get bounding box into a matrix where columns are [xLeft, yTop, width, height]
[~, sortOrder] = sort(bb(:, 2), 'ascend');
props = props(sortOrder);
Sunetra Banerjee
Sunetra Banerjee el 15 de Jul. de 2021
Hi,
Thank you so much for your help. Now it's working.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by