How can i remove the crop mark in gui?
2 次查看(过去 30 天)
显示 更早的评论
Hi, i have a crop button in the GUI, and i can click the button and then to crop the image displayed in axes. How do i remove the crop mark if i want to recrop again? I want to clear the crop mark if there is any in the image once i click the button.
function BtnCrop_Callback(hObject, eventdata, handles)
handles.output = hObject;
handles.currentAxes = get(handles.Axes,'Children');
if isempty(handles.currentAxes) == false
promptMesg = sprintf('Are you sure to crop the image?');
button = questdlg(promptMesg, 'Crop the image','Yes','No','Yes');
if strcmpi(button,'Yes')
handles.k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint');
handles.finalRect = rbbox;
point2 = get(gca,'CurrentPoint');
point1 = point1(1,1:2);
point2 = point2(1,1:2);
p1 = min(point1,point2);
offset = abs(point1-point2);
handles.x = round([p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)]);
handles.y = round([p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)]);
handles.cropSuccess = true;
hold on
axis manual
plot(handles.x,handles.y) ;
% pause(2)
% delete(hPlot);
else
handles.cropSuccess = false;
end
else
errordlg('No image is selected!');
end
guidata(hObject,handles);
The delete(hPlot) i tried is not exactly what i want. What i want is every time i click the button, it will check is there any crop mark in the image, if there is, then it will be removed (handles.x and hanles.y will also be deleted); if not, then i can start the new cropping.
Thanks a lot.
0 个评论
采纳的回答
Geoff Hayes
2014-6-19
You almost have it - you just need to assign the handle of the crop area to the handles object and then store this updated (handles) application data so that it will be there the next time the user presses the button.
If the user has pressed the Crop button, chosen Yes, and selected an area within the image then the code
plot(handles.x,handles.y);
becomes
% save the handle to the rectangle that surrounds the area of the image
% that is to be cropped
handles.hCropRect = plot(handles.x,handles.y);
% store the updated app data
guidata(hObject,handles);
So we have a handle for the rectangle of the area to be cropped. You want this rectangle to be removed whenever the user presses the Crop button again, so we can do the following as the first line in the button callback (right before your above if statement, or as the first line within the if block)
% check to see if the handles object has the hCropRect field
if isfield(handles,'hCropRect');
% delete only if not empty
if ~isempty(handles.hCropRect)
delete(handles.hCropRect);
% clear the handle to denote that it has been deleted
handles.hCropRect = [];
% clear x and y
handles.x = [];
handles.y = [];
% store the updated app data
guidata(hObject,handles);
end
end
Try the above and see what happens!
I noticed that your code didn't have the guidata(hObject,handles) command - how were you able to access the handles.x, handles.y, and handles.finalRect outside of this function?
3 个评论
Image Analyst
2014-6-19
You will note, after running that code, that the image in the axes does not change. It will not get cropped.
更多回答(1 个)
Image Analyst
2014-6-19
I don't see any cropping going on. No indexing, no calling imcrop(), nothing. Using hPlot to get the handle to the line you plotted, and then deleting that handle will remove the line that you just drew from the image. It will not remove the x and y fields from handles like you said. The x and y fields will still be there, though the box in the overlay is gone.
You should probably put the delete(hPlot) before the call to plot:
if exist('hPlot', 'var')
delete(hPlot);
end
hPlot = plot(handles.x, handles.y)
croppedImage = imcrop(originalImage, handles.finalRect);
2 个评论
Image Analyst
2014-6-19
Explain how. You're just defining a box but nothing gets cropped . No image gets cropped, no matrix gets cropped, at least not in the code you showed.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Feature Detection and Extraction 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!