Senin, 28 April 2008

Convert RGB image to Grayscale Image

Actualy, there is a built up function in Matlab to convert RGB image to grayscale image. This function is RGB2GRAY. For example, we have RGB image (rgb.jpg) and grayscale image named by gray.jpg, so we have this code:

x=imread('rgb.jpg');
y=rgb2gray(x);
imwrite(y,'gray.jpg');


It's so easy. But...just for fun..i made user defined function in Matlab to do it. Very simple...conversion is done by get the average RGB component.

g=imread('lampu.jpg');
gb=double(g);
[b,kl,rgb]=size(gb);
gbrgb=zeros(b,kl);
for i=1:b
for j=1:kl
gbrgb(i,j)=round((gb(i,j,1)+gb(i,j,2)+gb(i,j,3))/3);
end
end
gbrgb1=uint8(gbrgb);
figure, imshow(g);
figure, imshow(gbrgb1);

Output from this code is :

Tidak ada komentar: