Trouble with image importing

10 次查看(过去 30 天)
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 个评论
Guillaume
Guillaume 2014-11-17
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 2014-11-17
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

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2014-11-18
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 个评论
Brian
Brian 2014-11-18
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 2014-11-18
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);

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2014-11-18
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 个评论
Image Analyst
Image Analyst 2014-11-18
If you don't have the Image ProcessingToolbox, simply just use image() and gray().
image(grayImage);
colormap(gray(256));

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by