store image in a handle.

6 次查看(过去 30 天)
I'm creating a GUI with a bunch of call back functions, the idea is to load an image an do a series of image transformation to it, I would like to keep those changes to the image and pass it around from one call back function to the other, I looked around to see if there is a similar case, even though there was plenty I couldn't figure out what's wrong with my code.
if true
% function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
guidata(hObject, handles);
img = get(handles.edit1,'String');
I = imread(img);
guidata(handles.figure1,I);
imshow(I);
disp(handles.figure1);
% --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) guidata(hObject, handles); h =findobj(0,'tag','figure1'); imshow(h); end
  1 个评论
Stephen23
Stephen23 2018-10-10
编辑:Stephen23 2018-10-10
This first line of code inside the callback serves no purpose:
guidata(hObject, handles);
handles is supplied by GUIDE as the third input argument to your function. There is absolutely no point in saving it straight away, without having made any changes or addition to it. Totally pointless.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2018-10-10
编辑:Stephen23 2018-10-10
When you save I using guidata you replace the handles structure with I, and so handles does not exist any more (and hence none of its values or graphics handles exist either). Instead of throwing handles away, you should store your data as a field of the handles structure:
handles.I = imread(img);
guidata(hObject,handles);
This is exactly what all of the guidata examples show, which is why you should read the documentation.
  6 个评论
Mohamed Amine Henchir
I get this error when I use I = get(imh,'CData'): Error using matlab.ui.Figure/get Invalid or deleted object.
Error in get_circles>pushbutton2_Callback (line 189) I = get(h,'CData');
here is my code for the callback:
if true
function pushbutton2_Callback(hObject, eventdata, handles)
axes(handles.axes1);
imshow(handles.I);
h = imcontrast(handles.figure1);
waitfor(h)
I = get(h,'CData');
handles.I = I;
imshow(handles.I);
guidata(handles.figure1,handles);
guidata(hObject,handles);
end
the Idea was to get the image from the h handle and store it where the original image is to overwrite it.
and thank you so much for all the insights!
Stephen23
Stephen23 2018-10-11
编辑:Stephen23 2018-10-12
This will not work:
h = imcontrast(handles.figure1);
The first input to imcontrast is described in its help as "...the image specified by the handle h". You are giving it the handle to some figure object. A figure object is not an image object. You need to provide it with a handle to the image object, just like the help explains.
This will not work:
waitfor(h)
I = get(h,'CData');
When you read the waitfor help it states that it "...blocks statements from executing until the MATLAB® object closes (is deleted)". So the code will continue once the imcontrast dialog box is closed. But once an object has been deleted you cannot get any properties from it, so you cannot expect to get anything out of an object that no longer exists. Note that a figure object does not have the property 'CData', so this would never work anyway: you need to give it the handle to the image.
Summary: Provide the image handle to imcontrast, as its help requires. To get the image data from the image replace the h with the image handle which is returned by imshow.
imh = imshow(handles.I);
dbh = imcontrast(imh);
waitfor(dbh)
I = get(imh,'CData');
Tip: you should read the documentation for every function that you use. I also recommend that you obtain and use the graphics handles for every graphics object that you need to refer to:

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by