Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

matlab - imshow/imwrite changes all pixel values to 1

I have a Matlab dataset saved with .mat that I'm trying to process in Octave GUI. The data consist of images and I want to save them in a JPG format (or any other image format), but I'm having this strange behavior when trying to displaying or writing the images. this is how part of the image displays as an array:

    91    90    91    88    93
    88    91    86    81    88
    93   100    90    85    91
    93   100    94    93    96
    87    87    87    87    89

But when I write the image

imwrite(img, 'D:image_test_1.jpg')

and read it again

img_read=imread('D:image_test_1.jpg')

I end up with this:

  1  1  1  1  1
  1  1  1  1  1
  1  1  1  1  1
  1  1  1  1  1
  1  1  1  1  1

I tried searching for the cause, but couldn't find a definitive answer or clarification to this problem. Even when I'm using imshow to display the image I end up with a blank image.

What happened to all the pixel values?

question from:https://stackoverflow.com/questions/65879604/imshow-imwrite-changes-all-pixel-values-to-1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Configuring as uint8 Image

To indicate that this image is using an 8-bit scale/format we can caste the array as an uint8() (unsigned 8-bit integer). This format will assume the intensity values range from 0 to 255 (typical JPG format). I think the reason for the array showing up as 1s is that Octave is trying to parse the array as a double ranging from 0 to 1. Therefore the results of the array are reaching the ceiling of 1 since all the intensity values of the Image/img array are out of range (maxed out). Alternatively we can convert the array to double using the im2double() function or diving the original array by 255.

8-Bit Image Result

Image = [91  90  91  88  93; 
         88  91  86  81  88;
         93  100 90  85  91;
         93  100 94  93  96;
         87  87  87  87  89];

Image = uint8(Image);

imwrite(Image, 'D:image_test_1.jpg')
imshow(imread('D:image_test_1.jpg'),'InitialMagnification','fit');

Ran using MATLAB R2019b


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...