10

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?

gnovice
  • 125,304
  • 15
  • 256
  • 359
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319

7 Answers7

25

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

Seanny123
  • 8,776
  • 13
  • 68
  • 124
Donnie
  • 45,732
  • 10
  • 64
  • 86
8
x = imread('bla.jpg');
k = rgb2gray(x);
figure(1),imshow(k);
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
s.lakshmi
  • 81
  • 1
  • >> k = rgb2gray(im); Undefined function 'rgb2gray' for input arguments of type 'uint8'. – ntg Dec 11 '13 at 19:08
2

you can using this code:

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

using to matlab

2

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);
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
Ema
  • 21
  • 1
1

I=imread('yourimage.jpg');
p=rgb2gray(I)
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • I know that the answer is simple, but code-only answers are discouraged here. Please add a little context, explain what `rgb2gray` does and maybe link to the documentation. – horchler Oct 14 '15 at 17:56
1

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)
Alimpk
  • 99
  • 7
0

Color Image

Color Image

Gray Scale image

Gray Scale image

  bg = imread('C:\Users\Ali Sahzil\Desktop\Media.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);
Syed Ali Shahzil
  • 1,079
  • 1
  • 11
  • 17