I want to get the image I got in a function in another function
    조회 수: 6 (최근 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)

%
댓글 수: 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에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




