How to crop an irregular shape from the image?

My topic of the project is to detect and segment the optic disk. First of all, i want to remove the black background at four corners.
I have found the region of eye and labelled with red circle as shown in the figure. Now i am facing a problem which is how to crop the region?
Can someone please advise? Thank you.

 Réponse acceptée

OK, it seems you were unable to take my steps and code it up so I've done it for you.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'Screenshot (329).png';
folder = pwd;
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
%=======================================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Crop off screenshot stuff
rgbImage = rgbImage(132:938, 352:1566, :);
% Display image.
subplot(2, 2, 1);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('Original Color Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% 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.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Take one of the color channels, whichever one seems to have more contrast..
colorChannelToUse = 2;
grayImage = rgbImage(:, :, colorChannelToUse);
% Display the color segmentation mask image.
subplot(2, 2, 2);
imshow(grayImage, []);
caption = sprintf('Color Channel %d', colorChannelToUse)
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
axis('on', 'image');
drawnow;
%=======================================================================================
% Threshold the image to get the optics disk.
lowThreshold = 112;
highThreshold = 255;
% Use interactive File Exchange utility by Image Analyst to to the interactive thresholding.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
% [lowThreshold, highThreshold] = threshold(114, 255, grayImage);
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
% Now do clean up by hole filling, and getting rid of small blobs.
mask = imfill(mask, 'holes');
mask = bwareafilt(mask, 1); % Take largest blob only.
% mask = bwconvhull(mask);
% Display the color segmentation mask image.
subplot(2, 2, 3);
imshow(mask, []);
title('Final Mask', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
axis('on', 'image');
drawnow;
% Get x and y
boundary = bwboundaries(mask);
x = boundary{1}(:, 2);
y = boundary{1}(:, 1);
subplot(2, 2, 1);
% Display image with optics disc outlined over it.
subplot(2, 2, 4);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('With Boundary Outlining the Disc');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Display boundary
hold on;
plot(x, y, 'b-', 'LineWidth', 2);
%=======================================================================================
findBoundingCircle = false;
if findBoundingCircle
% Find the minimum bounding circle using John D'Errico's File Exchange
% https://www.mathworks.com/matlabcentral/fileexchange/34767-a-suite-of-minimal-bounding-objects?s_tid=srchtitle
% Get the minimum bounding circle.
hullflag = true;
[center,radius] = minboundcircle(x,y,hullflag)
% Show circle
viscircles(center, radius, 'color', 'r');
caption = sprintf('With Boundary and Bounding Circle');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
end
uiwait(helpdlg('Done!'));
If it works for you can you click the "Accept this answer" otherwise, say why it didn't work.

5 commentaires

ZWY
ZWY le 20 Mai 2022
Hi, thank you for you code.
This code works fine only for this image. However, my project is targeted on 80 images which the threshold value needs to be manually changed for every image.
Do you have any suggestions? Can i use fuzzy c mean?
Thank you.
Try the interactive threshold link given in the code. Or try the attached triangle threshold.
ZWY
ZWY le 21 Mai 2022
Could you provide the code for the interactive threshold? I could not find the link.
Thanks.
ZWY
ZWY le 22 Mai 2022
Thank you very much

Connectez-vous pour commenter.

Plus de réponses (2)

Tala
Tala le 19 Mai 2022
Modifié(e) : Tala le 19 Mai 2022

0 votes

Image analyst beautifully explains here
https://www.mathworks.com/matlabcentral/answers/1683749-how-to-crop-a-masked-area-polygon-shape-out-of-an-image#answer_930129
In order to create a circle mask, see this
https://www.mathworks.com/matlabcentral/fileexchange/47905-createcirclesmask-m

4 commentaires

ZWY
ZWY le 19 Mai 2022
Hi Tala, thank you for the reply.
Regarding the circle mask, i have tried it but the command window shows the error:
Unrecognized function or variable 'createCirclesMask'
Do you have any other suggestions?
Tala
Tala le 19 Mai 2022
Modifié(e) : Tala le 19 Mai 2022
I just had a chance to open your image on a PC. your image is not a complete circle,maybe thats why!
Also, I am not clear what do you mean by crop. Your image is already cropped since the back ground is zero.
https://www.mathworks.com/matlabcentral/fileexchange/47905-createcirclesmask-m
ZWY
ZWY le 20 Mai 2022
Hi Tala, you are right, I do not need to crop the image. Now i have known the problem and thank you for your time.
Thank Walter too!

Connectez-vous pour commenter.

Produits

Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by