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

9 ビュー (過去 30 日間)
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
Image Analyst 2020 年 5 月 7 日
  13 件のコメント
Dmitry Surov
Dmitry Surov 2020 年 10 月 23 日
編集済み: Dmitry Surov 2020 年 10 月 23 日
1) I've produced my image to binary;
2) I've called regionprops() and got the centroid of the shape;
3) I've called bwboundaries to get the outline (not of the contour of the shape but like a square line inside which my shape is);
I compelety misunderstand your next step "Then I'd SUBSTRACT the centroid so you can SCAN the OUTLINE to get it in polar coordinates) - i've no idea, what you mean by this "SUBSTRACT" - can you give any examples of this ? (or maybe there are some examples in your demos that i missed) (or maybe there exist a buit in function in MatLab to do this operation).
About - how shape contour can be broken - well, there's no rule, that rules this situation, like it can be broken 2,3 or 5 times (just like 5 "gapes" in shape's contour). Image will contain no background grips at all. About clutter - well, it depends on the image, some have no clutter at all, some of them have lots of clutter, and it's really hard to get well-binarized image, cause no doubt - it will be binary - but the quality of the shapes in this situation "leaves much to be desired" - they (contour lines of the shape) can have lot's of "gaps", or even 2 shapes, situated near to each other in grayscale image, can turn into one shape in binary image.
Those shapes come from the picture of the silicon's surface, after it gets etched - so i have no control over the images - i only receive them to work with and that's all.
Thanks
Image Analyst
Image Analyst 2020 年 10 月 23 日
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;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
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
grayImage = imread(fullFileName);
% 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
mask = ~imbinarize(grayImage);
%--------------------------------------------------------------------------------------------------------
% SEGMENTATION OF IMAGE
% Fill Holes
mask = imfill(mask, 'holes');
% Extract only blobs larger than 1000.
mask = bwareaopen(mask, 1000, 4); % Connectivity of 4
% Display the mask image.
subplot(2, 2, 2);
imshow(mask);
caption = sprintf('Mask');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
drawnow;
% Get the centroids
props = regionprops(mask, 'Centroid');
% Get the boundaries
boundaries = bwboundaries(mask);
% 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);
% Plot radii.
subplot(2, 2, 3);
plot(props(k).radius, '-', 'LineWidth', 2);
grid on;
xlabel('Pixel Index', 'FontSize', fontSize);
ylabel('Radius', 'FontSize', fontSize);
title('Radius', '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);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by