How to crop automatically the interested region of an image?
4 views (last 30 days)
Show older comments
hi. i need to automatically crop the interested region of the image. The images are the scanned MRI images. i want to segmentation prostate and i need to remove other organs .i want to use level set method . plz help me to solve this project.
0 Comments
Accepted Answer
Image Analyst
on 21 Nov 2015
Edited: Image Analyst
on 21 Nov 2015
We have no idea what in that thing you want to segment out. So go here, find an algorithm, and code it up. If you still have questions, read this then come back.
Also see my Image Segmentation Tutorial where I "automatically" crop out things: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
3 Comments
Image Analyst
on 22 Nov 2015
If you want a box, you can use imrect() or rbbox(). Here is code for rbbox():
k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint'); % button down detected
finalRect = rbbox; % return figure units
point2 = get(gca,'CurrentPoint'); % button up detected
point1 = point1(1,1:2); % extract x and y
point2 = point2(1,1:2);
p1 = min(point1,point2); % calculate locations
offset = abs(point1-point2); % and dimensions
% Find the coordinates of the box.
xCoords = [p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)];
yCoords = [p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)];
x1 = round(xCoords(1));
x2 = round(xCoords(2));
y1 = round(yCoords(5));
y2 = round(yCoords(3));
hold on
axis manual
plot(xCoords, yCoords, 'b-'); % redraw in dataspace units
% Display the cropped image.
croppedImage = grayImage(y1:y2,x1:x2);
imshow(croppedImage);
axis on;
title('Region that you defined', 'FontSize', fontSize);
And here's the snippet for imrect():
hBox = imrect;
roiPosition = wait(hBox);
roiPosition % Echo coordinates to the command window.
xCoords = [roiPosition(1), roiPosition(1)+roiPosition(3), roiPosition(1)+roiPosition(3), roiPosition(1), roiPosition(1)];
yCoords = [roiPosition(2), roiPosition(2), roiPosition(2)+roiPosition(4), roiPosition(2)+roiPosition(4), roiPosition(2)];
% Plot the mask as an outline over the image.
hold on;
plot(xCoords, yCoords, 'linewidth', 2);
Both will let you draw a box that you can then use in imcrop(). You manually draw the box for both. If you need an irregular shaped region, I think even a human would have difficulty deciding the exact rows and columns to draw the box so it would be difficult to have the computer do it. For that you'll have to look at some of those papers in the link I gave you.
Akib Rahman
on 18 Jun 2018
@Image Analyst, please take a look at this question! https://www.mathworks.com/matlabcentral/answers/406090-how-to-crop-some-segmented-part-of-an-image-automatically
More Answers (1)
Khalladi Sofiane abdelkrim
on 27 Apr 2022
Hi , i have a folder of images containing the same road but different frames i want to extract just a specified Region of interest in forme of polygon and save it in another file . How i can implémenté that please ?
Thanks
1 Comment
Image Analyst
on 28 Apr 2022
Use drawpolygon() and save(). I'm sure there's an example in the documentation for it.
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!