Problem with imwrite a color image

I am trying to save the output color image of a program as a jpg or png file. I have tried
outfile='image.jpg'
imwrite(im2uint16(Image),outfile,'BitDepth',12,'Mode','lossless','Quality',100)
When i tried to open this using Gimp or ImageMagick , the image could not open, it was not recognized. Can anyone suggest the right way to save a color image as a jpg using imwrite? Thank you.

4 Comments

Angela - what is the data type for your image, Image? Is it necessary to convert to 16-bit unsigned integers? Perhaps try
imwrite(Image, 'image.jpg', 'jpg');
So the image is a true color image and the data are in uint16. I tried your example and unfortunately it did not work, i got a message
UINT16 image data requires bitdepth specifically set to either 12 or 16.
I tried
imwrite(Image, 'image.jpg', 'jpg','Bitdepth',12);
and although it was saved, i could not open the image outside of Matlab.
I don't think you tried my code below, which DOES work.
Thank you for your answer. Please see below my comment underneath your answer. Although the code you suggested works and saves an image as an jp2, what i want is an image saved as jpg (or png) because jp2 images can not be opened by ImageMagick or Gimp.Thank you.

Sign in to comment.

 Accepted Answer

Photoshop doesn't recognize it either. But this works:
rgbImage = imread('peppers.png');
outfile='image.jp2' % JPEG 2000
imwrite(im2uint16(rgbImage),outfile,'Mode','lossless')
And I'd recommend you use PNG rather than JPG or JPEG2000 (jp2).
PNG has pretty much become the widespread de facto standard.

8 Comments

Thank you for the answer. I did this and the image that comes out is a *jp2 but i need a jpg image (png is acceptable too). I need images that i can visually inspect quickly and jp2 images are very large usually. Thank you again.
How are you inspecting them? In File Explorer or Finder?
JP2 and PNG can be slightly larger than lossy jpg, but it's worth it because they don't degrade the image. I'm not sure that lossless JPG is any smaller than lossless JP2 - can you give us the sizes?
And why would you want to degrade your image just so you could inspect it maybe a fraction of a second faster?
I'm not seeing a compelling reason why you need JPG images. Please explain that better, like with timing of the times it takes to "inspect" each file type.
Angela
Angela on 14 Aug 2018
Edited: Angela on 14 Aug 2018
So my images as jp2 are about 500MB. Right now in order to look at them i have to read them in Matlab and it takes a long time (around 2 minutes with imread) because i have several stacks of them. The images are going through some processing in my program so jpg or png would be sufficient to start with to check if the processing worked or not (removing artifacts) and it will save me a lot of time. Been able to click through them in the Image Viewer will allow me to do this process faster.
imwrite(im2uint16(rgbImage), 'newimage.png');
You can save each image twice, once as 16 bit png for serious work later, and once as 8 big jpeg for quick browsing. You can write your GUI so that when you are browsing a particular 8 big JPEG, that you could click a button to bring in the 16 bit png for closer examination.
Thank you for your suggestion. Unfortunately this gives me the message:
Error using writejpg>set_jpeg_props (line 183)
UINT16 image data requires bitdepth specifically set to either 12 or 16.
And when i tried to add the bitdepth the image, although it is saved, it is not accessible outside Matlab.
writejpg>set_jpeg_props would never be called for file extension .png .
If you go the dual-file route, then
imwrite(im2uint16(rgbImage), 'newimage.png'); %for archive quality
imwrite(im2uint8(rgbImage), 'newimage.jpg'); %for quick access that is not archive quality
Thank you. The png worked but unfortunately the jpg still needed the bitdepth. I tried to use the 'Quality' switch but it also did not seem to have saved a smaller image. So for now i went the dual route with png and a jpg with a 'lossy' option.

Sign in to comment.

More Answers (1)

JPEG does not support 16 bit images. Some JPEG libraries support 12 bit images.
JPEG XR supports 16 bit images, but MATLAB does not support JPEG XR.

8 Comments

So is there a way to save a colored image as a jpg? I tried 12 bit and i still can not see the image using gimp. Is there a way to save it maybe as a png image using imwrite?
All you need to do is use .png as the file extension when you imwrite()
Angela
Angela on 15 Aug 2018
Edited: Angela on 15 Aug 2018
Unfortunately the image that is created by png is as larg as the jp2. I wanted to save it as jpg so that i can make them smaller.
Then you will need to get JPEG to change the JPEG standard to permit higher bit depths, and then wait for MATLAB to pick up on the revised standard.
This is rather unlikely to happen.
The reason jpg is smaller is that it's smaller when you compress more and throw away information. The two lossless formats are bigger because no information is lost. In these days of terabyte solid state drives and 3 GHz computers why are you worried about saving a few kilobytes of drive space? You should be more worried about the fidelity of your image data. Don't save junk just to save a few kilobytes that really won't even matter or make a difference in anything.
The reason is that i have to go through many images fast to make sure that my code is working, my reasoning is that smaller images load faster so i can visually inspect them faster. I am not trying to save space, i am trying to save time. After the inspection of course i use the good quality images to continue the analysis but in the beginning i want to use the smaller images to quickly access my code.
.. So write two copies of the image like I suggested, one archive quality and the other reduced bit depth for speed.
Thank you for your help Walter Roberson, that is what i ended up doing.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Tags

Asked:

on 14 Aug 2018

Commented:

on 16 Aug 2018

Community Treasure Hunt

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

Start Hunting!