How can i automatically convert the input image to gray scale

5 次查看(过去 30 天)
I am dealing with three image types: color, B&W and gray. i am reading a bunch of images and performing enhancement but i want all the images to be in gray scale before i proceed to enhance. But i do not know what type of an image i am reading at an instant. so if i am reading anything other than gray scale i want to convert it to gray and if am reading a gray input i want to proceed directly to enhancement. How can i automate this ??

采纳的回答

Image Analyst
Image Analyst 2015-7-21
Well it's a little bit tricky to detect the most common types of images: grayscale, RGB, and indexed . Of course your "binary image" is most likely a grayscale uint8 image with gray levels of only 0 and either 1 or 25. See this snippet which handles all of those, and adapt as needed:
%=====================================================================
% Reads FullImageFileName from disk into the axesImage axes.
function imageArray = DisplayImage(handles, fullImageFileName)
% Read in image.
imageArray = []; % Initialize
try
[imageArray, colorMap] = imread(fullImageFileName);
% colorMap will have something for an indexed image (gray scale image with a stored colormap).
% colorMap will be empty for a true color RGB image or a monochrome gray scale image.
catch ME
% Will get here if imread() fails, like if the file is not an image file but a text file or Excel workbook or something.
errorMessage = sprintf('Error in program %s, function %s(), at line %d.\n\nError Message:\n%s', ...
mfilename, ME.stack(1).name, ME.stack(1).line, ME.message);
uiwait(warndlg(errorMessage));
return; % Skip the rest of this function
end
try
% Display image array in a window on the user interface.
%axes(handles.axesImage);
hold off; % IMPORTANT NOTE: hold needs to be off in order for the "fit" feature to work correctly.
% Here we actually display the image in the "axesImage" axes.
imshow(imageArray, 'InitialMagnification', 'fit', 'parent', handles.axesImage);
% Display a title above the image.
[folder, basefilename, extension] = fileparts(fullImageFileName);
extension = lower(extension);
% Display the title.
caption = [basefilename, extension];
title(handles.axesImage, caption, 'Interpreter', 'none', 'FontSize', 14);
[rows, columns, numberOfColorChannels] = size(imageArray);
% Get the file date
fileInfo = dir(fullImageFileName);
txtInfo = sprintf('%s\n\n%d lines (rows) vertically\n%d columns across\n%d color channels\n', ...
[basefilename extension], rows, columns, numberOfColorChannels);
% Tell user the type of image it is.
if numberOfColorChannels == 3
colorbar 'off'; % get rid of colorbar.
txtInfo = sprintf('%s\nThis is a true color, RGB image.', txtInfo);
elseif numberOfColorChannels == 1 && isempty(colorMap)
colorbar 'off'; % get rid of colorbar.
txtInfo = sprintf('%s\nThis is a gray scale image, with no stored color map.', txtInfo);
elseif numberOfColorChannels == 1 && ~isempty(colorMap)
txtInfo = sprintf('%s\nThis is an indexed image. It has one "value" channel with a stored color map that is used to pseudocolor it.', txtInfo);
colormap(colorMap);
whos colorMap;
%fprintf('About to apply the colormap...\n');
% Thanks to Jeff Mather at the Mathworks to helping to get this working for an indexed image.
colorbar('peer', handles.axesImage);
%fprintf('Done applying the colormap.\n');
end
catch ME
errorMessage = sprintf('Error in program %s, function %s(), at line %d.\n\nError Message:\n%s', ...
mfilename, ME.stack(1).name, ME.stack(1).line, ME.message);
uiwait(warngld(errorMessage));
end
uiwait(helpdlg(txtInfo));
return; % from DisplayImage
  1 个评论
Image Analyst
Image Analyst 2015-7-21
To convert an indexed image to gray scale, you have to convert to RGB first, then use rgb2gray - you can't just take the indexed image and ignore the colormap because it will look like garbage.
rgbImage = ind2rgb(imageArray, colorMap);
grayImage = rgb2gray(rgbImage);

请先登录,再进行评论。

更多回答(1 个)

Guillaume
Guillaume 2015-7-21
编辑:Guillaume 2015-7-21
A black & white image is just a grey image with just two levels of grey, so I'm not sure what you want to do in that case. Possibly nothing. A colour image will be a 3d matrix, whereas a grey image will be 2d. That's easy to differentiate with ndims
if ndims(img) == 3
%colour image, convert to greyscale any way you want
img = rgb2gray(img); %note that there are many other ways to convert a colour image to grey
elseif isequal(logical(img), img) %one way of recognising a bw image
%do whatever you want. Nothing?
end

类别

Help CenterFile Exchange 中查找有关 Orange 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by