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?
uint8
ImageTo 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.
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