Trouble with image importing
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
Brian
on 13 Nov 2014
Whichever way you get it ( rgb2gray or your formula), an image with only one colour channel (i.e. a 2d matrix) is grayscale by convention. The pixel values represents intensities between black and white.
Most likely, you're displaying the image using a false colour scale. Can you show the code you're using to generate and display the image?
Brian
on 16 Nov 2014
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?
Brian
on 17 Nov 2014
Accepted Answer
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));
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!