updating data within same GUI in MATLAB

こ雲淡風輕ζ 提交于 2020-01-06 20:23:52

问题


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?


回答1:


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.




回答2:


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);



回答3:


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.



来源:https://stackoverflow.com/questions/18064669/updating-data-within-same-gui-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!