rgb2gray conversion in GUI
11 views (last 30 days)
Show older comments
So I'm working on my semestrial work.I'm working on noise reduction filters and right now I'm working on GUI for theese filters. I allready have functional filter code and part of the code is conversion to grayscale. But when I put it together in GUI I've got an error message:
Error using rgb2gray>parse_inputs (line 77)
MAP must be a m x 3 array.
Error in rgb2gray (line 52)
[X, threeD] = parse_inputs(X);
Error in untitled>FILT_IMG_Callback (line 97)
A = rgb2gray (X);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in untitled (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)untitled('FILT_IMG_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
I understand that image have to be in 3 dimenesion to be grayscaled but my image is allready in 3 dimensions. Any idea how to figure that out? Here's whole code:
function LOAD_IMG_Callback(hObject, eventdata, handles)
% hObject handle to LOAD_IMG (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global X
X=uigetfile('*.*')
%filename=X;
%setappdata(0,'filename', filename);
X=imread(X);
axes(handles.axes1);
imshow(X);
%setappdata(0, 'A', X);
% --- Executes on button press in FILT_IMG.
function FILT_IMG_Callback(hObject, eventdata, handles)
% hObject handle to FILT_IMG (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global X
A = rgb2gray (X);
X = A;
axes(handles.axes1);
imshow(X);
5 Comments
Stephen23
on 13 Mar 2018
The documentation, which describes all ways to pass data between callbacks:
The wiki that explains this:
http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
And some of the many threads on this forum that answer your question:
Answers (1)
Image Analyst
on 13 Mar 2018
Don't have X be a global string, then pass it into uigetfile() and rewrite X with a totally different data type. Talk about bad programming practice! Try this:
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\wherever you want';
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage);
if numberOfColorChannels > 1
% It's not really gray scale like we want - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
else
grayImage = rgbImage; % It's not really RGB, it's gray already!
end
0 Comments
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!