Trouble with image importing

11 views (last 30 days)
Brian
Brian on 13 Nov 2014
Commented: Image Analyst on 18 Nov 2014
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
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
Brian on 17 Nov 2014
Sorry about not being specific enough. I'm still new to the community I currently only have access to Matlab 2009. Here is code I would use for my images. Either I import the image by double clicking its file location or I use >> I=imread('kochflake.png'); >> image(I)
When I do the following code with treebranch >> tree=imread('treebranchresize.jpg') >>I2=rgb2gray(tree); >>image(I2)
Without fail, when I try these codes, I don't get a grayscale image

Sign in to comment.

Accepted Answer

Guillaume
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
Brian
Brian on 18 Nov 2014
This code seems to convert a grayscale image into a color image. I need one that keeps the image grayscale and two-dimensional. Without the image processing toolbox if possible.
Guillaume
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);

Sign in to comment.

More Answers (1)

Image Analyst
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
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));

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!