English 中文(简体)
How can I convert a color image to grayscale in MATLAB?
原标题:

I am trying to implement an algorithm in computer vision and I want to try it on a set of pictures. The pictures are all in color, but I don t want to deal with that. I want to convert them to grayscale which is enough for testing the algorithm.

How can I convert a color image to grayscale?

I m reading it with:

x = imread( bla.jpg );

Is there any argument I can add to imread to read it as grayscale? Is there any way I change x to grayscale after reading it?

最佳回答

Use rgb2gray to strip hue and saturation (ie, convert to grayscale). Documentation

问题回答
x = imread( bla.jpg );
k = rgb2gray(x);
figure(1),imshow(k);

I found this link: http://blogs.mathworks.com/steve/2007/07/20/imoverlay-and-imagesc/ it works.

it says:

im=imread( your image );
m=mat2gray(im);
in=gray2ind(m,256);
rgb=ind2rgb(in,hot(256));
imshow(rgb);

you can using this code:

im=imread( your image );
k=rgb2gray(im);
imshow(k);

using to matlab

I=imread( yourimage.jpg );
p=rgb2gray(I)

Use the imread() and rgb2gray() functions to get a gray scale image.

Example:

I = imread( input.jpg );
J = rgb2gray(I);
figure, imshow(I), figure, imshow(J); 

If you have a color-map image, you must do like below:

[X,map] = imread( input.tif );
gm = rgb2gray(map);
imshow(X,gm);

The rgb2gray algorithm for your own implementation is :

f(R,G,B) = (0.2989 * R) + (0.5870 * G) + (0.1140 * B)

Color Image

Color Image

Gray Scale image

Gray Scale image

  bg = imread( C:UsersAli SahzilDesktopMedia.png );  // Add your image 
  redChannel = bg(:, :, 1);
  greenChannel = bg(:, :, 2);
  blueChannel = bg(:, :, 3);
  grayImage = .299*double(redChannel) + .587*double(greenChannel) 
  +.114*double(blueChannel);
  imshow(grayImage);




相关问题
Wrapping image around objects in web app

I m creating a web app in ASP.NET like this one: http://www.zazzle.com/cr/design/pt-mug I know how to do everything except wrapping an image around an object. It would be a simple task to do if I ...

Fast Image Manipulation using SSE instructions?

I am writing a graphics library in C and I would like to utilize SSE instructions to speed up some of the functions. How would I go about doing this? I am using the GCC compiler so I can rely on ...

How to joint some objects in digital image?

I m looking for some algorithm to joint objects, for example, combine an apple into a tree in digital image and some demo in Matlab. Please show me some materials of that. Thanks for reading and ...

PHP image generator/handler

I d like to create a PHP image generator that takes several query string parameters and generates an image on the fly. I ve done this before in ASP.NET via a handler but now I d like to in PHP. For ...

How can I convert a color image to grayscale in MATLAB?

I am trying to implement an algorithm in computer vision and I want to try it on a set of pictures. The pictures are all in color, but I don t want to deal with that. I want to convert them to ...

Python Tesseract can t recognize this font

I have this image: I want to read it to a string using python, which I didn t think would be that hard. I came upon tesseract, and then a wrapper for python scripts using tesseract. So I started ...

热门标签