Reputation: 213
I've created one image editor window in MATLAB, which includes various operations including brightness, contrast, crop, rotate etc.., In this GUI, each operations has its own function call backs.
My problem is with linking one function with another. if i crop image, then if i change brightness my current code changes brightness of original image rather than cropped image. similarly at first i if change brightness and then if i crop, my code will crop original image rather than brightened image.
Below code is to change brightness.
function slider2_Callback(hObject, eventdata, handles)
fname = getappdata(0, 'fname');
[a, map] = imread(fname);
x = ind2rgb(a, map);
b = get(handles.slider2,'value');
j = imadjust(x,[],[],b);
axes(handles.axes1);
imshow(j);
Below code is to crop
function crop_Callback(hObject, eventdata, handles)
fname = getappdata(0, 'fname');
[a, map] = imread(fname);
x = ind2rgb(a, map);
new = imcrop(x);
axes(handles.axes1);
imshow(new);
Suppose say at first if i crop image, then next step if i change brightness of cropped image later some other operation on above edited image, How can i link one operation with another?
Upvotes: 0
Views: 1221
Reputation: 275
Instead of having global variable for the file name, you need to have a global variable for the image. So in each of your callbacks you can manipulate with your image without reading the image every time. Also, you should keep your changes at the end of each call back by setappdata
. Thus, your callbacks would be something like this:
function changeimage_Callback(hObject, eventdata, handles)
image = getappdata(0, 'image');
% manipulation on image
% show image
setappdata(0, 'image', image);
If you have one GUI, I think it would be more convenient if you do such a thing with handles
. That is, load your image in your GUI and keep it like this:
handles.image = imread(filename);
guidata(hObject, handles);
Then your callbacks would be like this:
function changeimage_Callback(hObject, eventdata, handles)
% manipulation on handles.image
% show handles.image
guidata(hObject, handles);
Upvotes: 1
Reputation: 2028
Instead of loading the original image file to perform your manipulation on, load the image from the figure.
In other words, replace
fname = getappdata(0, 'fname');
[a, map] = imread(fname);
x = ind2rgb(a, map);
with
x = getimage(handles.axes1);
I didn't test the code but it should fix your problem with a minimal amount of work.
Upvotes: 0
Reputation: 1499
I think it's because you are not updating your handles after changing them, hence the code is still referring to the old handle.
Try guidata(hObject, handles);
right after cropping.
Upvotes: 1