![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/253314/0000%20Screenshot.png)
Cropping an image using imfreehand()
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
shivasmic
am 6 Dez. 2019
Kommentiert: shivasmic
am 10 Dez. 2019
I want to crop the ROI from an image using imfreehand() but I am not able to. How can I crop an using imfreehand(). Would cropping the ROI also affect the qualtiy of the ROI as a whole. If it would affect the ROI then what is the solution?
imshow('cameraman.png');
h = imfreehand();
position = wait(h);
What am I supposed to do after this to get the ROI without losing the quality? Thanks in advance.
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 6 Dez. 2019
Try this:
grayImage = imread('cameraman.tif');
imshow(grayImage);
subplot(1, 2, 1);
imshow(grayImage);
fontSize = 20;
title('Double click inside to accept it.', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
promptMessage = sprintf('Click and drag out a region.\nDouble click inside to quit.\nor Quit processing?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Draw', 'Quit', 'Draw');
if contains(buttonText, 'Quit')
return;
end
h = imfreehand();
position = wait(h)
% Make integers
position = round(position);
% Find cropping limits.
col1 = min(position(:, 1))
col2 = max(position(:, 1))
row1 = min(position(:, 2))
row2 = max(position(:, 2))
% Do the crop
croppedImage = grayImage(row1:row2, col1:col2);
% Show the cropped image.
subplot(1, 2, 2);
imshow(croppedImage);
title('Cropped Image', 'FontSize', fontSize);
![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/253314/0000%20Screenshot.png)
3 Kommentare
Image Analyst
am 7 Dez. 2019
The image is NOT deteriorated after cropping. You still have the original pixels. It only looks pixelated because it's magnified for display.
It is usually not necessary for you to extract/crop the subimage to do image analysis on it. I usually don't unless I need to do something like doing OCR and need to get one character or one band of text in an image.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Image Preview and Device Configuration finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!