How to extract objects in inner circle of this image?

2 vues (au cours des 30 derniers jours)
Love Matlab
Love Matlab le 29 Mai 2020
Commenté : Love Matlab le 1 Juin 2020
In the 1st image I have attached without the yellow outine. I need to get rid of the objects in the outer ring/circle . I only want to keep the ones in the inner circle.
Is it possible to do this?
I need to apply this to a series of images that have various diameters and # of objects.
OR
if it is easier. I need to get rid of everything inside and beyond the yellow outlined area.

Réponse acceptée

Image Analyst
Image Analyst le 29 Mai 2020
It's pretty easy. I suggest you first find all centroids and then find the average of the centroids, which will be at the center of the circle. Then use the center and the centroid to get a radius for each blob. Then use kmeans to divide the radii into two groups and take the inner group.
clc; % Clear the command window.
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 = 'image.jpeg';
% 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;
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 Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
%--------------------------------------------------------------------------------------------------------
% SEGMENTATION OF IMAGE
% Get a binary image
binaryImage = imbinarize(grayImage);
% Get rid of huge white frame surrounding the image the user posted.
binaryImage = imclearborder(binaryImage);
% Get rid of the small dots in the star by doing a hole fill.
binaryImage = imfill(binaryImage, 'holes');
subplot(2, 2, 2);
% figure
imshow(binaryImage, []);
impixelinfo;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
%--------------------------------------------------------------------------------------------------------
% MEASUREMENT OF CENTROIDS
% Measurement of locations and assignment of file names.
labeledImage = bwlabel(binaryImage);
props = regionprops(labeledImage, 'Centroid', 'BoundingBox');
xy = vertcat(props.Centroid);
x = xy(:, 1);
xCenter = mean(x)
y = xy(:, 2);
yCenter = mean(y)
for k = 1 : length(props)
deltax = x(k) - xCenter;
deltay = y(k) - yCenter;
radius(k) = sqrt(deltax^2 + deltay^2);
end
% Now use kmeans to classify them into two groups, and extract the smallest
radius = radius(:); % Reshape into column vector for kmeans().
[indexes, meanRadius] = kmeans(radius, 2);
if meanRadius(1) < meanRadius(2)
% Class 1 is a smaller circle.
keepers = find(indexes == 1);
else
% Class 2 is a smaller circle.
keepers = find(indexes == 2);
end
% Extract just those
innerBlobs = ismember(labeledImage, keepers);
subplot(2, 2, 3);
imshow(innerBlobs, []);
impixelinfo;
title('Inner Blobs', 'FontSize', fontSize, 'Interpreter', 'None');

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by