필터 지우기
필터 지우기

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
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에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by