![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1005495/image.png)
How to crop an irregular shape from the image?
18 次查看(过去 30 天)
显示 更早的评论
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.![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1003335/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1003335/image.png)
0 个评论
采纳的回答
Image Analyst
2022-5-20
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!'));
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1005495/image.png)
If it works for you can you click the "Accept this answer" otherwise, say why it didn't work.
5 个评论
更多回答(2 个)
Tala
2022-5-19
编辑:Tala
2022-5-19
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 个评论
Walter Roberson
2022-5-19
https://www.mathworks.com/matlabcentral/fileexchange/47905-createcirclesmask-m
Image Analyst
2022-5-19
You don't need to get rid of the black background. That is not the first step, nor even anything you need to do.
Your first step should be to go here:
and look at the sections:
- 20.5 Retinal Images, Analysis of Eye, etc.
- 20.5.1 Eye, Cornea, Corneal Images
- 20.5.2 Optic Disc Location, Optic Disc Detection
- 20.5.3 Retinal Images, Angiography, Blood Vessels in the Eye
- 20.5.3.1 Retinal Microaneurysms, Detection
- 20.5.4 Glaucoma Retinopathy, Retinal Analysis Application
- 20.5.5 Cataracts, Detection, Analysis, Surgery
- 20.5.6 Diabetic Retinopathy, Retinal Analysis Application
- 20.5.7 Macular Degeneration Detection, Retinal Analysis Application
- 20.5.8 Retinal Images, Optical Coherence Tomography, OCT
to see how it's done in section 20.5.2.
One simplistic approach would be to use the blue channel and threshold to find the optics disc. Then I'd use bwconvhull() to get its convex hull.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!