开发者

Matlab RGB Image to Gray Scale Manually

I have a RGB image and want to apply following formulas to it so I get another image. How can I do that? I know how to read/write 开发者_开发知识库image and I know how to loop and apply formulas but I don't know functions to extract number of rows and columns of image in a variable and image pixles values of 3 planes in 3-dimensional plane.

I = imread('myimage.jpg');

RGBImagePixles = [?, ?, ?] %of I

ROWS = ? %of I
COLUMNS = ? %of I

for r = 0 : ROWS
   for c = 0 : COLUMNS
      N[r, c] = RGBImagePixles[r,c,1] + RGBImagePixles[r,c,2] + RGBImagePixles[r,c,3]
   end
end

figure, imshow(N);


The output of imread is a 3 dimensional array, actually 3 matrices stacked along the 3rd dimension - so if your image is m pixels high and n pixels wide, you'd get a m x n x 3 array. so:

RGBImagePixles = I;
ROWS = size (I,1);
COLUMNS = size(I,2);

and you can replace the loop with:

N = sum(I, 3);

However, I am not sure that simple summation is what you need in order to produce a grayscale image.


[ROWS COLUMNS DIMS] = size(I);


I assume that RGB to grayscale is just an example, and your real goal is to learn how to manipulate the pixels of an image. Otherwise, you should just use:

ImageRGB = imread('yourfile.jpg')
ImageGray = rgb2gray(ImageRGB)

To do it by hand:

ImageRGB = imread('yourfile.jpg')
ImageGray = sum(ImageRGB,3) / (3*255)

You have to divide by 3*255 because matlab expects a grayscale image's values to be between 0 and 1, while the RGB values will between 0 and 255 in each channel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜