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
  • 123,396
  • 14
  • 248
  • 352
Nathan Fellman
  • 108,984
  • 95
  • 246
  • 308

7 Answers7

25

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

Seanny123
  • 6,594
  • 11
  • 56
  • 106
Donnie
  • 41,533
  • 8
  • 62
  • 82
8
x = imread('bla.jpg');
k = rgb2gray(x);
figure(1),imshow(k);
Nathan Fellman
  • 108,984
  • 95
  • 246
  • 308
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
  • 108,984
  • 95
  • 246
  • 308
Ema
  • 21
  • 1
1

I=imread('yourimage.jpg');
p=rgb2gray(I)
Wai Ha Lee
  • 7,664
  • 52
  • 54
  • 80
  • 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
  • 70
  • 6
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);
Ali Shahzil
  • 134
  • 6