Trouble with image importing

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 comentarios

Brian
Brian el 13 de Nov. de 2014
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.
Guillaume
Guillaume el 13 de Nov. de 2014
Editada: Guillaume el 13 de Nov. de 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
Brian el 16 de Nov. de 2014
I have 2 main images to compare right now: treebranchresize.jpg and kochflake.png The tree branch, I resized with paint, and maybe that's why it has 3-dimensional matrices for color. The kochflake, I resized with other programs and got a 2 dimensioned matrix. Maybe the problem is the version I'm usig, because as soon as I load the Koch flake into matlab, it's a mess of blues and reds. As soon as I use the rgb2gray(treebranchresize) command, I get a similar mess of reds or blues (mostly red in this case)
Guillaume
Guillaume el 17 de Nov. de 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 el 17 de Nov. de 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

Iniciar sesión para comentar.

 Respuesta aceptada

Guillaume
Guillaume el 18 de Nov. de 2014

0 votos

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 comentarios

Brian
Brian el 18 de Nov. de 2014
imshow doesn't seem to work on my version. I get error message "Undefined function or method 'imshow' for input arguments of type 'uint8'"
Guillaume
Guillaume el 18 de Nov. de 2014
Oh yes, it was part of the image processing toolbox before r2014b, it's now part of base matlab.
image with a single channel image (such as your png) uses the intensities as indices into the figure colormap, which by default has colours. So you just need to change the colormap:
pngimage = imread('kochflake.png'); %load as m*n*1 uint8
image(pngimage);
colormap([0:255; 0:255; 0:255]' / 255);
Alternatively, image with an image with three channels, just use the rgb values of the image, so you could just duplicate the single png channel twice:
pngimage = imread('kochflake.png'); %load as m*n*1 uint8
image(repmat(pngimage, [1 1 3]);
For the jpg image, since it's already got three channels (with r=g=b), don't use rgb2gray on it.
jpgimage = imread('treebranchresize.jpg')
image(jpgimage);
Or if you do convert it to single channel image, then change the colormap.
Brian
Brian el 18 de Nov. de 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 el 18 de Nov. de 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);

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 18 de Nov. de 2014

0 votos

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 comentario

Image Analyst
Image Analyst el 18 de Nov. de 2014
If you don't have the Image ProcessingToolbox, simply just use image() and gray().
image(grayImage);
colormap(gray(256));

Iniciar sesión para comentar.

Categorías

Preguntada:

el 13 de Nov. de 2014

Comentada:

el 18 de Nov. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by