Matlab is opening a pgm image with different value

2019-09-04 11:19发布

问题:

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 );


标签: matlab pgm