store image in a handle.

8 views (last 30 days)
Mohamed Amine Henchir
Mohamed Amine Henchir on 9 Oct 2018
Edited: Stephen23 on 12 Oct 2018
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 Comment
Stephen23
Stephen23 on 10 Oct 2018
Edited: Stephen23 on 10 Oct 2018
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.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 10 Oct 2018
Edited: Stephen23 on 10 Oct 2018
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 Comments
Mohamed Amine Henchir
Mohamed Amine Henchir on 11 Oct 2018
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 on 11 Oct 2018
Edited: Stephen23 on 12 Oct 2018
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:

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!