# How can i apply a function to an image to make it find detected figure shapes ?

13 views (last 30 days)
Dmitry Surov on 7 May 2020
Commented: Image Analyst on 23 Oct 2020
Hi!
I've got image with diffirent types of shapes;
I've used imcontour function to find contours of shapes in image;
Now i've got like (contour?) of circles/squares/.... shape in image;
I know that function1 graph is "circle", function2 graph is "square".... (it's written down why "circle" and "square" are in commas)
I've got no ideas how to make MatLab work in following way: I've got a function, who's graph reminds "circle", let it be function1, i've got contours of a "circle" shape (done with imcontour) in image, so now i wanna to make MatLab to find and to count all contours of "circle" shapes in image, who's contour(shape) can be described with function1
And the problem is that those chapes, are not exactly circles/squares, they are like "transitional" circle/square form, like it's not a circle/square, only it's contour reminds of circle/square shape, and i have to work exactly with those shapes, so imfindcircles and boundingbox, is not what i'am looking for (cause they just draw "pure" circles/squares, even there where it's not a circle/square exactly) (as i understood)
Since i have no permission to give any photos, it would be great for me to get ANY help...
P.S. plz be patient, i'm kind of a MatLab lamer

Image Analyst on 7 May 2020
See my attached shape recognition demos.
Image Analyst on 23 Oct 2020
See this and adapt as needed:
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
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 = 22;
%--------------------------------------------------------------------------------------------------------
folder = pwd;
baseFileName = 'eight.tif';
% Get the full filename, with path prepended.
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;
else
% It found it somewhere.
folder = fileparts(which(fullFileNameOnSearchPath)); % Determine where folder is.
fullFileName = fullfile(folder, baseFileName);
end
end
% 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.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
% This image needs to be inverted so that the bolt is white
%--------------------------------------------------------------------------------------------------------
% SEGMENTATION OF IMAGE
% Fill Holes
% Extract only blobs larger than 1000.
subplot(2, 2, 2);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
drawnow;
% Get the centroids
% Get the boundaries
% Get the radius and angle for each boundary
% and add them to props structure.
for k = 1 : length(boundaries)
thisBoundary = boundaries{k};
props(k).x = thisBoundary(:, 2);
props(k).y = thisBoundary(:, 1);
props(k).xCentroid = props(k).Centroid(1);
props(k).yCentroid = props(k).Centroid(2);
subplot(2, 2, 2);
hold on;
plot(props(k).xCentroid, props(k).yCentroid, 'r+', 'MarkerSize', 50, 'LineWidth', 2);
% Get the radius and angle.
props(k).radius = sqrt((props(k).x - props(k).xCentroid) .^ 2 + (props(k).y - props(k).yCentroid) .^ 2);
numerator = props(k).y - props(k).yCentroid;
denominator = props(k).x - props(k).xCentroid;
props(k).angles = atand(numerator ./ denominator);
subplot(2, 2, 3);
grid on;
xlabel('Pixel Index', 'FontSize', fontSize);
hold on;
% Plot angles.
subplot(2, 2, 4);
plot(props(k).angles, '-', 'LineWidth', 2);
grid on;
xlabel('Pixel Index', 'FontSize', fontSize);
ylabel('Angle', 'FontSize', fontSize);
title('Angle', 'FontSize', fontSize);
hold on;
end
fprintf('Done running %s.m ...\n', mfilename); ### Categories

Find more on Lighting, Transparency, and Shading in Help Center and File Exchange

R2017b

### Community Treasure Hunt

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

Start Hunting!