I have now tried the direct converting algorithm, which is .299r+.587g+0.114b to convert my image to grayscale. I got the same result as the rbg2gray of course. That is, an image which is anything but black and white or grayscale. Can anyone determine why images keep on not getting converted properly? Do I need the Image Processing Toolbox to compute these kinds of things? I'm generally feeling stuck, and any help is appreciated.
Trouble with image importing
11 views (last 30 days)
Show older comments
In my project, which focuses on image processing techniques, I have a significant fundamental problem. When I try to import a black and white or grayscale image, matlab interprets it as a three-dimensional matrix, where I guess the third matrix is a color map (even though we aren't using color images). I have used the command rbgtogray, and while that solves the dimension problem, the result is always an image that is not gray. Is there something I'm missing?
5 Comments
Guillaume
on 17 Nov 2014
Your png image is already a greyscale image and should be loaded as such by matlab (i.e. a 256x256 uint8 matrix). jpg image always have three colour channels so it's loaded by matlab as a 256x256x3 uint8 matrix. The colour channels are all equal so it should appear grey.
You say the problem may be with the version you're using but you don't specify what it is. So which version are you using?
Most likely, it's to do with the way you load or display the images. So, once again, can you show the code you're using to load and display the images?
Accepted Answer
Guillaume
on 18 Nov 2014
Right, now we've got to the bottom of the problem:
You're using the wrong function to display your image. Instead of
image(I); %or I2
Use
imshow(I); % or I2
As per documentation of image, image creates an image graphics object by interpreting each element in a matrix as an index into the figure's colormap. Another option would be to change the figure colormap to greyscale with
colormap([0:255; 0:255; 0:255]' / 255);
4 Comments
Guillaume
on 18 Nov 2014
I've given you the solution twice now! Change the colormap of your figure.
pngimage = imread('kochflake.png'); %load as m*n*1 uint8
image(pngimage);
colormap([0:255; 0:255; 0:255]' / 255);
More Answers (1)
Image Analyst
on 18 Nov 2014
This works fine for me with your images. No weird colors whatsoever.
clc;
clearvars;
close all;
workspace;
fontSize = 33;
% Read in a color demo image.
folder = 'C:\Users\Mark\Documents\Temporary';
button = menu('Select image', 'treebranchresize.jpg', 'kochflake.png');
if button == 1
baseFileName = 'treebranchresize.jpg';
else
baseFileName = 'kochflake.png';
end
% baseFileName = 'treebranchresize.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(1, 2, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Make grayscale.
if numberOfColorBands > 1
grayImage = rgb2gray(rgbImage);
else
grayImage = rgbImage;
end
% Display the original color image.
subplot(1, 2, 2);
imshow(grayImage);
title('Grayscale Image', 'FontSize', fontSize);
1 Comment
Image Analyst
on 18 Nov 2014
If you don't have the Image ProcessingToolbox, simply just use image() and gray().
image(grayImage);
colormap(gray(256));
See Also
Categories
Find more on Blue 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!