Matlab is opening a pgm image with different values using imread

假如想象 提交于 2019-12-11 22:02:37

问题


I have a pgm image with 1251 different pixel values, ranging from 0 to 1250. I know this because I can open this image file with kate and see the values.

But when I open the same file using Matlab's imread, it also returns me 1251 different pixel values, but these values are not consecutive. The minimum value is 0 and the maximum value is 65483.

I want to iterate through these values in a for loop so I need to read the original and consecutive values as they exist in the file. How to do that in Matlab?

EDIT: That's the image if someone wants to try.

image


回答1:


The values are scaled so that when you view the image it's not mostly black.

I tested that the scaling works with straight integer truncation by checking that:

[A] = imread( 'myfile.pgm', 'pgm' );
p = sort(unique(A(:));
q = uint16((0:1250) * 65535 / 1251)';
all(p == q)                             % returns 1

So, you can restore the image like this:

map = arrayfun( @(x) uint16(x * 1251 / 65536), 0:65535 );
B = arrayfun( @(x) map(x+1), A );


来源:https://stackoverflow.com/questions/19760566/matlab-is-opening-a-pgm-image-with-different-values-using-imread

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!