我想将我的彩色图像转换为灰度图像并避免使用rgb2gray
命令。
答案 0 :(得分:2)
那么:
I_grey = mean(I_colour, 3);
您可能需要先将其转换为uint8
才能查看它:
I_grey = uint8(mean(I_colour, 3));
或者如果你想要真正准确,你应该找到加权平均值。有关加权选项,请参阅此问题的答案:Formula to determine brightness of RGB color
以下是此示例:
W = permute([0.3086, 0.6094, 0.0820], [3,1,2]);
I_grey = uint8(sum(bsxfun(@times, double(I_colour), W),3));
答案 1 :(得分:1)
以下是对丹的回答的一些修改以及回答你问题的其他内容。
代码 -
%// Load image
I_colour = imread('pic1.jpg');
%// Dan's method with the correct (correct if you can rely on MATLAB's paramters,
%// otherwise Dan's mentioned paramters could be correct, but I couuldn't verify)
%// parameters** as listed also in RGB2GRAY documentation and at -
%// http://www.mathworks.com/matlabcentral/answers/99136
W = permute([0.2989, 0.5870, 0.1140], [3,1,2]);
I_grey = sum(bsxfun(@times, double(I_colour), W),3);
%// MATLAB's in-built function
I_grey2 = double(rgb2gray(I_colour));
%// Error checking between our and MATLAB's methods
error = rms(abs(I_grey(:)-I_grey2(:)))
figure,
subplot(211),imshow(uint8(I_grey));
subplot(212),imshow(uint8(I_grey2));
Mathworks的家伙在 - http://www.mathworks.com/matlabcentral/answers/99136
上通过简单易懂的代码精心回答了这个问题答案 2 :(得分:0)
函数Error in kalmanbeta>MyLikelihoodFn (line 95)
F=p(1);
Error in kalmanbeta (line 50)
resultparam=fminsearch(MyLikelihoodFn,param)
消除色调和饱和度,并保留有关亮度(亮度)的信息。
因此,您可以使用以下公式将位于i和j的像素转换为灰度。
F = 0
img(i,j,1)是RED像素的值。
img(i,j,2)是绿色像素的值。
img(i,j,3)是蓝色像素的值。
grayScaleImage(i,j)是灰度范围内的像素值[0..255]
rgb2gray