When I read a pgm file on matlab and read it for some reason it changes the elements, how can i prevent that from happening? I can't attach the image but can someone help me in making sure the elements dont change when reading a pgm file, thank you

3 views (last 30 days)
myFolder = ''; % this has the path file for where the pgm files are stored
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.pgm');
pgmFiles = dir(filePattern);
n = 13;
I=zeros(1000,1000,1000);
for k = 1:length(pgmFiles)
baseFileName = pgmFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
A{k} = imread(fullFileName);
end
  2 Comments
T Vinai Datta Reddy
T Vinai Datta Reddy on 18 Apr 2019
I'm facing the same issue too.
For instance, when I try to display the first 15 values of the image in C, I get -
2 2 2 2 2 2 2 2 2 3 3 2 3 3 2
And in Matlab,
img(1:15)
the values are -
265 265 265 265 265 265 265 265 265 397 397 265 397 397 265
Observe that the pattern is same.
Anyway, I'm unable to upload the image file here since .pgm is an unsopperted format, so here you go -
Image Analyst
Image Analyst on 19 Apr 2019
I downloaded it and brought it into Photoshop and it looks like garbage.
flower_grad.png
It actually looks better in MATLAB!
0001 Screenshot.png
The image is a 16 bit (2 byte) image. The values MATLAB gives are 100% reasonable and believable. I suspect in C you are assuming it's a uint8 image and reading it a byte at a time, thus giving the incorrect (low) values, instead of reading two bytes (a pair of bytes) into a uint16 variable.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 30 Jan 2017
I don't believe it is changing the values. For you to prove it is, you'll have to upload a file that we can read in with MATLAB, and some other program, such as Photoshop, where we can see that the color or graylevel at a certain location is not the same in the two different programs.
  4 Comments
Image Analyst
Image Analyst on 1 Feb 2017
Syed, we can't really do anything more unless you attach the image and say what alternate method (other than MATLAB) you're using to check the values. Just attach an image with non-proprietary content if you can.

Sign in to comment.

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!