rgb2gray conversion in GUI

8 Ansichten (letzte 30 Tage)
Tomas Pechac
Tomas Pechac am 12 Mär. 2018
Kommentiert: Stephen23 am 13 Mär. 2018
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 Kommentare

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 13 Mär. 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

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by