Sharing data between functions in GUIDE

12 ビュー (過去 30 日間)
Eugene Zaverukha
Eugene Zaverukha 2016 年 5 月 9 日
コメント済み: Image Analyst 2016 年 5 月 9 日
Hello, I'm writing a program that lets the user upload an image and play with the graphics colors. I wrote the callback functions for slider buttons that change the color to red, blue and green. I want to be able to move the slider buttons up and down, change the colors and then be able to come back to the original image by returning the sliders to the same position, however after a bit of changing the colors the image isn't able to reset back to original and I'd like to fix it. I use setappdata and getappdata functions to store my picture axes data and share it between the callback functions but I guess somewhere along I messed up.
This is my script for the load image and the slider buttons:
function load_image_Callback(hObject, eventdata, handles) [filename, pathname] = uigetfile('*.jpg','Select a Picture to Load'); %Select image file if filename==0 %Test to see image selected msgbox('Please select an image!','error','error'); return end f=fullfile(pathname, filename); %Get the data for the image file x=imread(f); %Read the image data image(x); %Display image guidata(hObject, handles); %update handles setappdata(handles.pic, 'img',x); %Share image data in pic handle
function red_Callback(hObject, eventdata, handles) % hObject handle to red (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) r=get(hObject, 'Value'); %Acquire value for the slider x=getappdata(handles.pic,'img'); %Get image data from pic hand;es s=size(x); %Size image
while row <= s(1) x(row, :, 1) = x(row, :, 1)+128*r; row = row + 1; end image(x); %Display the new image
guidata(hObject, handles); %Update data setappdata(handles.pic, 'img',x); %Share data in handle
function green_Callback(hObject, eventdata, handles) % hObject handle to green (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) x=getappdata(handles.pic,'img'); %Acquire data from the image handle g=get(hObject, 'Value'); %Value for green slider s=size(x); %size the image % Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider row=1; while row <= s(1) x(row, :, 2) =x(row, :, 2) + 128*g; row = row + 1; end image(x); %Display the new image % --- Executes during object creation, after setting all properties. guidata(hObject, handles); %Update handles setappdata(handles.pic, 'img',x); %Set data for new image
function blue_Callback(hObject, eventdata, handles) % hObject handle to blue (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) x=getappdata(handles.pic,'img'); b=get(hObject, 'Value'); %Get value for blue color s=size(x); %Size image % Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider row=1; while row <= s(1) x(row, :, 3) =x(row, :, 3) + 128*b; row = row + 1; end image(x); %Display new image % Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider guidata(hObject, handles); %Update handles setappdata(handles.pic, 'img',x); %Share handles

回答 (2 件)

Image Analyst
Image Analyst 2016 年 5 月 9 日
"I guess somewhere along I messed up" is pretty vague. Plus you didn't attach the m-file and .fig file. We'd need both, along with an explanation of "messed up" and instructions of the steps you made, if we're to run your program and reproduce the messed up situation.

Eugene Zaverukha
Eugene Zaverukha 2016 年 5 月 9 日
My apologies for the confusion.
Here's the breakdown of my logic: I first wrote the script for my 'load image' callback function where the user is able to select a .jpg file and load it onto the axes titled 'pic'. The file data is stored as my variable x. I use the setappdata function to store the data for my image so it can be used in other callback functions.
function load_image_Callback(hObject, eventdata, handles) [filename, pathname] = uigetfile('*.jpg','Select a Picture to Load'); if filename==0 msgbox('Please select an image!','error','error'); return end f=fullfile(pathname, filename); x=imread(f); image(x); guidata(hObject, handles); setappdata(handles.pic, 'img',x);
*My next callback function is to exit the program and since its very straightforward and not relevant to the issue I will skip that.
I assigned 3 slider buttons to callback functions to change the color to red r, green g and blue b. I use function getappdata to acquire my image data. By adjusting the slider, the user sets the value for the color using a while loop and the new value gets stored into the image data x. I then update it by using function setappdata again. I repeat the same steps for green and blue functions. I have attached the .m and .fig files and would appreciate any help.*
function red_Callback(hObject, eventdata, handles) % hObject handle to red (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) r=get(hObject, 'Value'); x=getappdata(handles.pic,'img'); s=size(x); % Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider row=1; while row <= s(1) x(row, :, 1) = x(row, :, 1)+128*r; row = row + 1; end image(x);
function green_Callback(hObject, eventdata, handles) % hObject handle to green (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) x=getappdata(handles.pic,'img'); g=get(hObject, 'Value'); s=size(x); % Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider row=1; while row <= s(1) x(row, :, 2) =x(row, :, 2) + 128*g; row = row + 1; end image(x); % --- Executes during object creation, after setting all properties. guidata(hObject, handles); setappdata(handles.pic, 'img',x);
function blue_Callback(hObject, eventdata, handles) % hObject handle to blue (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) x=getappdata(handles.pic,'img'); b=get(hObject, 'Value'); s=size(x); % Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider row=1; while row <= s(1) x(row, :, 3) =x(row, :, 3) + 128*b; row = row + 1; end image(x); % Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider guidata(hObject, handles); setappdata(handles.pic, 'img',x);
  1 件のコメント
Image Analyst
Image Analyst 2016 年 5 月 9 日
You probably should have edited your original up top, rather than put this as an official "Answer" to your question. But before you do that, read this link so we can read what you post.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by