简体   繁体   English

Matlab的循环图像

[英]Matlab For Loop Image

I am trying to open a .pgm image file in MATLAB, run a manipulation with a for loop and then save as another .pgm file. 我正在尝试在MATLAB中打开.pgm图像文件,使用for循环运行操作,然后另存为另一个.pgm文件。 Before doing the manipulation I was testing to see if I could recreate the image: 在进行操作之前,我正在测试以查看是否可以重新创建图像:

clear

picture = imread('Picture.pgm');

sizePic = size(picture);

sizeX = sizePic(1);
sizeY = sizePic(2);

newPicture = zeros(sizeX,sizeY);


for i = 1:sizeX
    for j = 1:sizeY
        newPicture(i,j) = picture(i,j);
    end
end

imwrite(newPicture, 'NewPicture.pgm');

However, the new image is almost all white with some black splotches (not the original). 但是,新图像几乎全是白色,带有一些黑色斑点(不是原始斑点)。 Shouldn't this just give me back the original image? 这不就应该还给我原始图像吗?

By default, picture created from imread(XXX.pgm) is either a uint8 or uint16 array, meaning the pixel values are in the range of [0 255] or [0 65535]. 默认情况下,从imread(XXX.pgm)创建的pictureuint8uint16数组,这意味着像素值在[0 255]或[0 65535]范围内。 On the other hand, newPicture created from zeros is a double array, the expected pixel value for which is only [0 1]. 另一方面,从zeros创建的newPicture是一个double newPicture数组,其预期像素值仅为[0 1]。 Any value greater than 1 will be interpreted as 1 (white) in the saved image. 在保存的图像中,任何大于1的值都将被解释为1(白色)。 When you assign a [0 255] value to such a double array, since most of the pixel values in picture is 1 and above, of course you will get mostly white pixels 当分配一个[0 255]的值,以这样的double阵列,因为大多数中的像素值的picture是1以上,当然,你会得到大部分是白色像素

When you work with images, always check the type of the image array. 使用图像时,请始终检查图像阵列的类型。 For example, it may be a good idea to always work with double type by explicitly converting the image returned by imread as such: pictures=im2double(imread(xxx)) . 例如,最好通过显式转换由imread返回的图像来始终使用double类型,例如: pictures=im2double(imread(xxx))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM