I want to get the image I got in a function in another function
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Mohamed Amine Habch
el 9 de Jun. de 2018
Editada: Image Analyst
el 22 de Sept. de 2019
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/191142/image.png)
%
2 comentarios
kamrul islam sharek
el 22 de Sept. de 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
el 22 de Sept. de 2019
Editada: Image Analyst
el 22 de Sept. de 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);
Respuesta aceptada
Alfonso
el 9 de Jun. de 2018
Editada: Alfonso
el 11 de Jun. de 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 comentarios
Alfonso
el 11 de Jun. de 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.
Más respuestas (1)
Image Analyst
el 9 de Jun. de 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);
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!