MATLAB Answers

I want to get the image I got in a function in another function

16 views (last 30 days)
this is the function I used to get the image
function selectImage_Callback(hObject, eventdata, handles)
[filename, pathname] = uigetfile({'*.jpg';'*.bmp';'*.png''*.jpeg'},'File Selector');
handles.myImage = strcat(pathname, filename);
axes(handles.imageAxes2);
imshow(handles.myImage)
% save the updated handles object
clear axes scale
axis off
guidata(hObject,handles);
and I'm trying to use the same image in another function
function classifyImage_Callback(hObject, eventdata, handles)
net = alexnet;
image = handles.myImage;
image = imresize(image,[227 227]);
label = classify(net,image);
set(handles.imageType, 'String' , label)
%

  2 Comments

kamrul islam sharek
kamrul islam sharek on 22 Sep 2019
I = imread('grey.jpg');
Points = detectSURFFeatures(I );
imshow(I);
hold on;
plot(Points.selectStrongest(10));
problem isError using
detectSURFFeatures>checkImage (line 128)
Input image must be a 2-D grayscale
image. You can use RGB2GRAY to convert
your image to a 2-D grayscale image.
Error in detectSURFFeatures (line 81)
checkImage(I);
Error in Object (line 2)
Points = detectSURFFeatures(I);
>> Object
Error: File: Object.m Line: 2 Column: 31
Invalid expression. Check for missing
multiplication operator, missing or
unbalanced delimiters, or other syntax
error. To construct matrices, use
brackets instead of parentheses.
Image Analyst
Image Analyst on 22 Sep 2019
Did you use rgb2gray() like it told you to? If not, why not?
rgbImage = imread('grey.jpg');
if ndims(rgbImage) > 1
% Need to convert to gray scale.
grayImage = rgb2gray(rgbImage);
else
grayImage = rgbImage; % It's already gray scale.
end
Points = detectSURFFeatures(grayImage);

Sign in to comment.

Accepted Answer

Alfonso
Alfonso on 9 Jun 2018
Edited: Alfonso on 11 Jun 2018
Try using the user data to save/load variables in different functions:
function selectImage_Callback(hObject, eventdata, handles)
[filename, pathname] = uigetfile({'*.jpg';'*.bmp';'*.png''*.jpeg'},'File Selector');
fullFileName = fullfile(pathname, filename);
handles.myImage = imread(fullFileName); % attending to the 2nd answer of post
axes(handles.imageAxes2);
imshow(handles.myImage)
% Save in user data
img = handles.myImage;
data_image= struct('img',img);
hObject.UserData = data_image;
% save the updated handles object
clear axes scale
axis off
guidata(hObject,handles);
Then in the second function, you load the user data of the first function
function classifyImage_Callback(hObject, eventdata, handles)
% Load the user data of the 1st function
h = findobj('Tag','selectImage');
data_image = h.UserData;
net = alexnet;
image = data_image.img;
image = imresize(image,[227 227]);
label = classify(net,image);
set(handles.imageType, 'String' , label)
In the second function in the findobj you should put the tag of your gui component, I wrote selectImage as it seems this is it.

  7 Comments

Show 4 older comments
Alfonso
Alfonso on 11 Jun 2018
About the error of trying to get the image of data_image, I wrote the tag of the second function in findobj where I should have written the tag of the 1st function; now it is corrected.
Glad you could finally solve the error.
Mohamed Amine Habch
Mohamed Amine Habch on 11 Jun 2018
yeah I figured you meant the first function, but I already tried it but didn't work, anyways the other parts of your solution really helped =) thank you!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 9 Jun 2018
This is wrong:
handles.myImage = strcat(pathname, filename);
You're just storing the filename string, not the actual image.
What you need to do is to call imread() with that filename string to get the actual image:
fullFileName = fullfile(pathname, filename);
handles.myImage = imread(fullFileName);

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!