1

I am trying to concatenate two 256x256 images and save them using imwrite. The saved image is supposed to be 256x512, but when I load the saved image, the size shows to be 343x434x3. How do I solve this?

the code I am using is:

new_name3 = strcat(f_name_image, '\', kk, '_', num2str(ff), '_pair.png');
pair = [orig_im  noisy_image]; %concatenating two 256x256 images
imagesc(pair)
f = getframe(gca);  
im = frame2im(f);
imwrite(im, new_name3);

Image

shaurov2253
  • 183
  • 1
  • 12

1 Answers1

2

Saving the image from the frame can be lossy without configuring additional options. To retain the pixel information save the concatenated image directly from the pair (here Image_Pair) array. Also, the third dimension in 343x434x3 represents the RGB colour channels of the image.

%Grabbing the two images%
Image_1 = imread('Image_1.jpeg');
Image_2 = imread('Image_2.jpeg');

%The file name for the concantenated images%
New_File_Name = "Image_3.jpeg";

%Concatenating the images%
Image_Pair = [Image_1 Image_2];

%Displaying the image pair%
imshow(Image_Pair);

%Saving the image to the "New_File_Name"%
imwrite(Image_Pair, New_File_Name);

%Loading the saved image to see if dimensions are consistent%
Saved_Image = imread('Image_3.jpeg');
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
MichaelTr7
  • 4,737
  • 2
  • 6
  • 21
  • Thank you for your reply. I actually tried saving the image without using 'getframe'. I get 256x512 image as expected, but the saved image is not same as the one shown on the matlab figure window. I think, while saving, it's losing the precision, which is why the saved image is messy. I tried to find out additional options for getframe so that I can save the image without loss but couldn't find it. Can you please help? – shaurov2253 Jul 29 '20 at 18:10
  • No problem, so I'm guessing you need the figure window as well. I'll get on that. The image shown in the figure window and saved image match when I tested it surprisingly. Any possibility that you could post the images you're using? – MichaelTr7 Jul 29 '20 at 18:14
  • I added one of the images I am using to the post. So this is my 'orig_im' in the code I mentioned. I just add few noise to it get a 'noisy_image'. I need to build dataset with concatenated paired images to train a deep learning model. I don't need the figure window, just the paired images. I think your images matched because both of their pixel values were in uint8. But I am trying to save the pixel values in double, which could be source of the problem. Thank you. – shaurov2253 Jul 29 '20 at 18:33
  • You want to save this image losslessly that looks like this correct? https://github.com/MichaelTr7/MATLAB-Multimedia-Functions/blob/master/README%20Images/Screen%20Shot%202020-07-29%20at%202.35.53%20PM.png – MichaelTr7 Jul 29 '20 at 18:37
  • Not quite sure how to save the image with the correct resolution after adjusting it using imagesc(). The only option I can think of is to write a function that transforms the pixels without using imagesc(). Possibly using another language may also be suitable. I'll let you know if I find a method of doing it. – MichaelTr7 Jul 29 '20 at 19:09
  • 1
    @shaurov2253: You should never go through the display for modifying and saving image data. You can apply the same transformations that `imagesc()` does: `img = img-min(img(:)); img = double(img)*255/max(img(:)); img = uint8(img);` – Cris Luengo Jul 29 '20 at 19:33
  • @CrisLuengo is correct, that this would be the best way. Working on the raw data and transforming it. – MichaelTr7 Jul 29 '20 at 19:35
  • @CrisLuengo: the final image i will have following your instruction is unit8. Can I make it double? Thank you. – shaurov2253 Jul 29 '20 at 19:59
  • @shaurov2253 im2double() might help convert the image if you want a range from [0,1]. Otherwise, you can scale this up by 255 afterwards. – MichaelTr7 Jul 29 '20 at 20:01
  • MichaelTr7 and CrisLuengo, Thank you very much for your help. – shaurov2253 Jul 29 '20 at 20:11
  • 1
    @shaurov2253 You cannot save a double-valued array to a JPEG file. JPEG is always 8-bit unsigned integer. If you want to save the floating-point values, you need to save using a format like TIFF (but floating-point TIFF files are not widely supported) or to a non-image file format. But your deep learning model doesn't need high-precision data, the 8-bit data is more than sufficient. Those tools typically normalize the data from the image files anyway. – Cris Luengo Jul 29 '20 at 20:15
  • @CrisLuengo : Thank you very much. One last question. Is TIFF the only format we that can deal with the double precession array? I was trying to save my images in PNG format. Would that work? – shaurov2253 Jul 29 '20 at 22:15
  • @shaurov2253: No, PNG won't work either. There are a few specialized image formats where you can store floating-point data, but all the most common formats don't. You're likely better off storing it as a MAT-file or something like that (in MATLAB, use `save filename img` to save your image as filename.mat). – Cris Luengo Jul 29 '20 at 22:19