Clear Filters
Clear Filters

Unable to display cropped image in the image axes properly

2 views (last 30 days)
I have a Matlab GUI where I load two sets of images in 2 separate image axes. I use a Slider to browse through the images simultaneously. Images displayed in image axes 1 are the original images. Images displayed in image axes 2 are the segmented images. I use a CROP button which would crop the image in image axes1 and display the resultant image in image axes 2 thus overriding the previously saved segmented image in image axes2. However, when I click the CROP button, the resultant image is displayed in Image axes 1 instead of being shown in image axes2. Please note that the CROP button uses the drawfreehand() function which crops the image displayed in image axes 1 manually. Any suggestions would be appreciated.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
corr_image = handles.InputImage;
M = MM.createMask();
guidata(hObject, handles);
h1 = msgbox('Finished correction images');
close (h1)

Accepted Answer

DGM on 23 Jul 2021
I'm not sure if you realize that you're starting an interactive imcrop() session.
When you do this, the current axes is axes1. Running imcrop(M) will start an interactive cropping session in axes1. Once the user completes the selection and commits, the cropped image will be passed to imshow(), which will display it in axes2. If you want to start the interactive selection in axes2, you can do that a number of ways:
If you intend to automatically crop the image to the extent of the ROI bounding box, that's a different story.
M = MM.createMask();
S = regionprops(M,'boundingbox');
% assuming there's only one object in the mask
M = imcrop(M,S(1).BoundingBox + [-1 -1 1 1]); % use a bit of padding
It's also not really clear why you want to display a cropped copy of the ROI mask instead of the source.
DGM on 24 Jul 2021
If you want to display a cropped copy of the ROI of the original image, you can do something like this:
clc; clf; clear variables
ip = imread('cameraman.tif');
% set up axes and handles vector
subplot(2,1,1); h(1) = gca;
subplot(2,1,2); h(2) = gca;
% put an image in axes1
subplot(2,1,1); imshow(255-ip);
M = MM.createMask();
S = regionprops(M,'boundingbox');
maskedimage = cast(double(ip).*M,class(ip)); % apply the mask to the image
maskedimage = imcrop(maskedimage,S.BoundingBox + [-1 -1 1 1]); % crop masked image
This is based on the second (automatic) example in my standalone test code, but can be extended to either.

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!