I want to get the image I got in a function in another function
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
kamrul islam sharek
2019-9-22
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
2019-9-22
编辑:Image Analyst
2019-9-22
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);
采纳的回答
Alfonso
2018-6-9
编辑:Alfonso
2018-6-11
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 个评论
Alfonso
2018-6-11
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.
更多回答(1 个)
Image Analyst
2018-6-9
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);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!