calculate the width and height of the rectangle in this image automatically?
6 ビュー (過去 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
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 件のコメント
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 Center および File Exchange で Surface and Mesh Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!