How to segment 2 circles as left and right?

2 views (last 30 days)
Beren Ac
Beren Ac on 23 Oct 2021
Edited: Matt J on 23 Oct 2021
Hi, I would like to seperate 2 circles as left and right . I can seperate left circle but when i tried to do the same thing for right it didn't work properly. In other words the algorithm croped half circle. I tried Filled Image instead of bounding box but i gave an error for imcrop code.
Original Image:
expected left circle:
Unexpected right circle
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
grayImage = rgb2gray(imread('newwww.jpg'));
[ny1,nx1] = size(grayImage) ;
en = imsharpen(grayImage,'Radius',2,'Amount',1);
%
%
% B= imdiffusefilt(en);
B = double(imgaussfilt(en,1.4));
%
ed=edge(B ,'canny',0.3,0.5);
%
subplot(2, 2, 1);
imshow(ed);
title('Initial Canny Edge', 'FontSize', fontSize);
%
bw1=bwareaopen(ed,10);
se = strel('disk',4);
bw=imdilate(bw1,se);
subplot(2, 2, 2);
imshow(bw);
title('Next Mask', 'FontSize', fontSize);
%
BW2 = imfill(bw,'holes');
figure
imshow(BW2)
title('Filled Image')
%
[ny,nx] = size(BW2) ;
hold on
% % Center
C = round([nx ny]/2) ;
plot(C(1),C(2),'*r') % mid point/ center of image
% %%Draw line
%
% %
P3 = [C(1) C(1)] ;
P4 = [1 ny] ;
plot(P3,P4,'b')
hold off
leftHalf = BW2(:, 1:P3, :);
figure(); imshow(leftHalf);
rightHalf=BW2(:, P3+1:end, :);
figure(); imshow(rightHalf);
%expected result
props = regionprops(leftHalf, 'BoundingBox');
maskedImage = imcrop(grayImage, props.BoundingBox);
%
figure();
imshow(maskedImage, []);
% unexpected result
props1 = regionprops(rightHalf, 'BoundingBox');
maskedImage1 = imcrop(grayImage, props1.BoundingBox);
figure();
imshow(maskedImage1, []);

Answers (1)

Image Analyst
Image Analyst on 23 Oct 2021
Not sure who told you to do that useless stuff but I'd be careful taking advice from them anymore. Sharpening the image, then blurring the image, doing an edge detection, morphology, cropping, etc. -- all that stuff is unnecessary and just overly complicated. Beginners often, for some reason, think that just because you can see edges in the image that you need to do edge detection when just a simple thresholding would be much better. Edge detection is rarely necessary.
Try this:
% Demo by Image Analyst.
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 = [];
baseFileName = 'image.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 image.
subplot(2, 3, 1);
imshow(rgbImage, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
caption = sprintf('Original RGB Image : "%s"\n%d rows by %d columns', baseFileName, rows, columns);
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';
%-----------------------------------------------------------------------------------------------------------------------------------
% Get a mask of the two largest blobs.
mask = bwareafilt(rgbImage(:, :, 1) < 245, 2);
% Display the image.
subplot(2, 3, 4);
imshow(mask, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
%-----------------------------------------------------------------------------------------------------------------------------------
% Get the left mask
labeledImage = bwlabel(mask);
leftMask = ismember(labeledImage, 1);
% Display the image.
subplot(2, 3, 2);
imshow(leftMask, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
title('Left Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Mask the RGB image with the left mask
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
maskedLeftImage = bsxfun(@times, rgbImage, cast(leftMask, 'like', rgbImage));
% Display the image.
subplot(2, 3, 5);
imshow(maskedLeftImage);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
title('Left Masked RGB Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%-----------------------------------------------------------------------------------------------------------------------------------
% Get the right mask
labeledImage = bwlabel(mask);
rightMask = ismember(labeledImage, 2);
% Display the image.
subplot(2, 3, 3);
imshow(rightMask);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
title('Right Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Mask the RGB image with the right mask
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
maskedRightImage = bsxfun(@times, rgbImage, cast(rightMask, 'like', rgbImage));
% Display the image.
subplot(2, 3, 6);
imshow(maskedRightImage);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
title('Right Masked RGB Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
msgbox('Done!');

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!