将彩色图像转换为灰度图像

matlab 用rgb2gray 将彩色图像转换为灰度图像

http://blog.sina.com.cn/s/blog_735f29100101b2c9.html

 

I=imread('F:\ZPB\ZPB.jpg');

x=rgb2gray(I);

 

subplot(121);

imshow(I);

title('原始图像');

 

subplot(122);

imshow(x);

title('灰度图像'); 

 

 

MATLAB中将彩色的图像转化为灰度图像

http://blog.csdn.net/wsywl/article/details/4749279

MyYuanLaiPic = imread('F:\ZPB\ZPB.jpg');%读取RGB格式的图像  

MyFirstGrayPic = rgb2gray(MyYuanLaiPic);%用已有的函数进行RGB到灰度图像的转换  

  

[rows , cols , colors] = size(MyYuanLaiPic);%得到原来图像的矩阵的参数  

MidGrayPic = zeros(rows , cols);%用得到的参数创建一个全零的矩阵,这个矩阵用来存储用下面的方法产生的灰度图像  

MidGrayPic = uint8(MidGrayPic);%将创建的全零矩阵转化为uint8格式,因为用上面的语句创建之后图像是double型的  

  

for i = 1:rows  

    for j = 1:cols  

        sum = 0;  

        for k = 1:colors  

            sum = sum + MyYuanLaiPic(i, j, k)/3;%进行转化的关键公式,sum每次都因为后面的数字而不能超过255

        end  

        MidGrayPic(i , j) = sum;  

    end  

end  

%imwrite(MidGrayPic , 'F:/ZPB/CZGC.png' , 'png');  

   

subplot(221); 

imshow(MyYuanLaiPic); 

title('原来的RGB图像'); 

 

subplot(222);  

imshow(MyFirstGrayPic);  

title('经过系统函数运算过的灰度图像');

   

subplot(223);  

imshow(MidGrayPic);  

title('转化之后的灰度图像');

 

相关链接:http://wenku.baidu.com/view/437d68300b4c2e3f5727636c.html

http://www.yesky.com/8/203008_2.shtml

 

 

原文地址:https://www.cnblogs.com/qxql2016/p/3820018.html