How to crop an irregular shape from the image?

37 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Image Analyst
Image Analyst il 20 Mag 2022
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 Commenti

Accedi per commentare.

Più risposte (2)

Tala
Tala il 19 Mag 2022
Modificato: Tala il 19 Mag 2022
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 Commenti
Walter Roberson
Walter Roberson il 19 Mag 2022
https://www.mathworks.com/matlabcentral/fileexchange/47905-createcirclesmask-m
ZWY
ZWY il 20 Mag 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!

Accedi per commentare.


Image Analyst
Image Analyst il 19 Mag 2022

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by