我目前正在使用代码:
i = imread('/usr/share/icons/matlab.png');
for k=1:1:m
for l=1:1:n
%a(k,l)=m*n;
a(k,l) = (.299*i(k,l,1))+(.587*i(k,l,2))+(.114*i(k,l,3));
end
end
imshow(a);
它只显示一个白屏。此外,新生成的尺寸为 n x m x 3,而它应该仅为 m x n x 1。
如果我使用 mat2gray,它会显示这样的图像
由于图像是 PNG,imread()
is returning an integer image ,强度值在 [0 255]
或等效范围内,具体取决于原始位深度。转换公式使 a
成为 double 图像,其强度预计在 [0 1]
范围内。由于 a
中的所有像素值可能都远大于 1,因此它们会被 imshow()
裁剪为 1(白色)。
最好的选择是explicitly convert the image format在你开始之前——这将负责正确地缩放事物:
i = imread('/usr/share/icons/matlab.png');
i = im2double(i);
a = .299*i(:,:,1) + .587*i(:,:,2) + .114*i(:,:,3); % no need for loops
imshow(a);
我是一名优秀的程序员,十分优秀!