How do we choose circles and rectangles?

How do we choose circles and rectangles?
I want to determine the rectangles and circles in the picture with different colors and write numbers on them. For example, I want to see how many circles and how many rectangles there are. Can you please help with the code?

 Réponse acceptée

Image Analyst
Image Analyst le 26 Juin 2020

0 votes

Just call imbinarize() and bwferet().

7 commentaires

bugrahan ilgaz
bugrahan ilgaz le 26 Juin 2020
please solve and send me code? ı am beginner for matlab
bugrahan ilgaz
bugrahan ilgaz le 26 Juin 2020
and ı want to write numbers for every circle and rectangulars.
Image Analyst
Image Analyst le 27 Juin 2020
Modifié(e) : Image Analyst le 27 Juin 2020
bugrahan, I don't have the code. I'd have to write it just like you. I suggest you take a look at my Image Segmentation Tutorial in my File Exchange, as well as the two attached shape recognition demos.
And you said help with your code, not do it all and hand it over to you. Is this a homework assignment? Sounds like it. If so, See this Link. Certainly for homework I can't do it for you to hand in as your own.
Alright. Here's the full demo. Remember if it's homework you can't turn in my work as your own or you could get into trouble.
% Demo to find circles and non-circles.
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;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = pwd;
baseFileName = 'shapes.jpeg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the RGB image full size.
subplot(1, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Image : "%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.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% 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.
hFig1.Name = 'Demo by Image Analyst';
% Threshold the image to find the small dark black thing.
[blackMask, maskedRGBImage] = createMaskForBlack(rgbImage);
% Fill the blobs.
blackMask = imfill(blackMask, 'holes');
% Threshold the image to find the small dark black thing.
[blueMask, maskedRGBImage] = createMaskForBlueAndBrown(rgbImage);
% Fill the blobs.
blueMask = imfill(blueMask, 'holes');
% Get the overall mask
mask = blueMask | blackMask;
% Get the areas of everything. There will be some small noise blobs.
props = regionprops(mask, 'Area');
% Display the histogram of areas.
subplot(1, 2, 2);
allAreas = sort([props.Area], 'descend')
histogram(allAreas);
grid on;
title('Histogram of original areas', 'FontSize', fontSize, 'Interpreter', 'None');
% Take the blobs only if they're 400 pixels or larger.
mask = bwareaopen(mask, 400);
% Fill holes
mask = imfill(mask, 'holes');
% Display the binary image.
subplot(1, 2, 2);
imshow(mask, []);
axis('on', 'image');
caption = sprintf(' Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Get the major and minor axis lengths of everything.
props = regionprops(mask, 'Area', 'MajorAxisLength', 'MinorAxisLength', 'Centroid');
for k = 1 : length(props)
% For each blob.
x = props(k).Centroid(1);
y = props(k).Centroid(2);
blobAspectRatio = props(k).MajorAxisLength / props(k).MinorAxisLength;
fprintf('For blob #%d, the aspect ratio is %f.\n', blobAspectRatio);
if blobAspectRatio > 1.5
text(x, y, 'Rectangle', 'Color', 'r', 'FontSize', 15, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
else
text(x, y, 'Circle', 'Color', 'b', 'FontSize', 15, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
end
end
function [BW,maskedRGBImage] = createMaskForBlueAndBrown(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 26-Jun-2020
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 0.999;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.190;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
function [BW,maskedRGBImage] = createMaskForBlack(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 26-Jun-2020
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 0.999;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 0.490;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
bugrahan ilgaz
bugrahan ilgaz le 27 Juin 2020
you're perfect. this code is right for my thinking. but finally, wanna count them , how many circle in photo ? and also how many rectangle in photo? this one is missing
bugrahan ilgaz
bugrahan ilgaz le 27 Juin 2020
I take this final , but how do I show biggest number ?
To count them, simply count them:
numberOfCircles = 0;
numberOfRectangles = 0;
for k = 1 : length(props)
% For each blob.
x = props(k).Centroid(1);
y = props(k).Centroid(2);
blobAspectRatio = props(k).MajorAxisLength / props(k).MinorAxisLength;
fprintf('For blob #%d, the aspect ratio is %f.\n', blobAspectRatio);
if blobAspectRatio > 1.5
text(x, y, 'Rectangle', 'Color', 'r', 'FontSize', 15, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
numberOfRectangles = numberOfRectangles + 1;
else
text(x, y, 'Circle', 'Color', 'b', 'FontSize', 15, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
numberOfCircles = numberOfCircles + 1;
end
end
Exactly what do you mean by "how do I show biggest number"? First of all there are lots of numbers in the program. What collection of numbers are you comparing when you say "biggest"? Do you mean which count is higher, circles or rectangles? Do you mean which blob has the largest area? I have no idea - you need to be precise, not ambiguous. Secondly exactly what does "how do I show" mean??? Do you mean you want a popup message box telling the user? Do you want something printed to the command window? Do you want text overlaid on the image? Do you want it in the title above the image? Again I have no idea. Please be specific.
For your new image, I didn't see any that were missing. Where is the missing one? You say "this" one is missing but you didn't point to the one that "this" refers to. Can you point a red arrow to it? Is it a lot smaller than the rest? Because you can see where I filter away blobs less than 400 pixels -- that is well commented in the code. Was it a very tiny rectangle (smaller than 400 pixels) because that would not be counted. You can set a smaller number than 400 if you want but you don't want to get much smaller than the known smallest size of your objects or else you'll be counting noise. Also note that with this new image, the camera has shifted so that the black sliver in the lower left is no longer in this image like it was in your original image.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Image Processing and Computer Vision dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by