rgb2gray conversion in GUI

조회 수: 10 (최근 30일)
Tomas Pechac
Tomas Pechac 2018년 3월 12일
댓글: Stephen23 2018년 3월 13일
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

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2018년 3월 13일
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

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by