calculate the width and height of the rectangle in this image automatically?

10 次查看(过去 30 天)
How can I calculate the width and height of the rectangle in this image automatically? to use them in the next step

采纳的回答

Image Analyst
Image Analyst 2020-3-15
编辑:Image Analyst 2020-3-15
Seems to work for me:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
folder = pwd;
baseFileName = 'image.png';
fullFileName = fullfile(folder, baseFileName);
rgbImage = imread(fullFileName);
% Display the original image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Color Image\n"%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by Image Analyst', 'NumberTitle', 'Off')
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Get the yellow mask.
yellowMask = redChannel == 255 & greenChannel == 255 & blueChannel == 0;
subplot(2, 2, 2);
imshow(yellowMask);
grid on;
title('Yellow Mask', 'FontSize', fontSize, 'Interpreter', 'None');
yellowMask = imfill(yellowMask, 'holes');
yellowMask = bwareafilt(yellowMask, 1);
subplot(2, 2, 3);
imshow(yellowMask);
grid on;
title('Yellow Mask, Filled', 'FontSize', fontSize, 'Interpreter', 'None');
% Find the Centroid, Equivalent Circular Diameter, and Bounding Box.
props = regionprops(yellowMask, 'Centroid', 'EquivDiameter', 'BoundingBox');
fprintf('Yellow Box Width = %d.\nYellow Box Height = %d.\n', props.BoundingBox(3), props.BoundingBox(4));
viscircles(props.Centroid, props.EquivDiameter/2);
% Display the original image.
subplot(2, 2, 4);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('With red circle centered over yellow square');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
viscircles(props.Centroid, props.EquivDiameter/2);
  6 个评论
Dina Abd El-twab
Dina Abd El-twab 2020-3-15
编辑:Dina Abd El-twab 2020-3-15
It is ok for the yellow mask image , but for the image of the circle , how can i extract ( centerX & centerY) from the "centroid "that you obtainned by regionprop command?
Image Analyst
Image Analyst 2020-3-15
You have to segment out the circle first. For example maybe you can threshold it and get rid of blobs touching the border then take the largest blob. Something like
mask = grayImage < someValue;
mask = imclearborder(mask);
mask = bwareafilt(mask, 1);
props = regionprops(mask, 'Centroid')
xCenter = props.Centroid(1);
yCenter = props.Centroid(2);
hold on;
plot(xCenter, yCenter, 'r+', 'MarkerSize', 30);
See my visually interactive thresholding app: in my File Exchange

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Blue 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by