how save image in jpeg format by keeping pixel value?

i have an rgb image in double format and i want to save it as jpeg so i convert it to uint8 and use imwrite to save it but it changes pixel value . how can i save it by keeping values beacuse i need former values?

1 件のコメント

sb icse
sb icse 2018 年 11 月 23 日
Have you found out the solution to your question?If yes then can you share the code and thank you in advance.

サインインしてコメントする。

回答 (2 件)

Walter Roberson
Walter Roberson 2016 年 6 月 21 日

1 投票

imwrite(YourImage, 'YourFile.jpg', 'mode', 'lossless')
This will use the seldom-used JPEG lossless compression.
I would advise that unless you have a strict requirement to use JPEG that instead you use one of the lossless image formats such as PNG or TIFF. When you use JPEG, it is all too likely that at some point someone (you) will forget (or not know) that it is important that the pixels remain unchanged when read back in.
Note: if you are trying to do watermarking or steganography, then you should pretty much assume that the image will be converted to lossy JPEG at some point, and you need the information to survive even so. One of the several ways to handle that is to do your own DCT, embed the information in the DCT coefficients, and do an inverse DCT and imwrite() that. Then when the JPEG library does its DCT, the result it is going to end up with will be the result that holds the embedded information. For recovery afterwards, DCT the loaded image and extract the bits.

7 件のコメント

Jack Moriss
Jack Moriss 2016 年 6 月 22 日
thank you for your answer.but i use dwt(and now i cannot change it) on one of the channels and itself converted 2 double so after applying idwt the image is double and even when i save it in jpeg or tiff or png formats the result are the same and the pixels have changed. i dont know where is my mistake
Walter Roberson
Walter Roberson 2016 年 6 月 22 日
Are you doing your calculation on the data range 0 to 255, or on the data range 0 to 1? From the documentation for imwrite:
  • If A is of data type uint8, then imwrite outputs 8-bit values.
  • If A is of data type uint16 and the output file format supports 16-bit data (JPEG, PNG, and TIFF), then imwrite outputs 16-bit values. If the output file format does not support 16-bit data, then imwrite returns an error.
  • If A is a grayscale or RGB color image of data type double or single, then imwrite assumes that the dynamic range is [0,1] and automatically scales the data by 255 before writing it to the file as 8-bit values. If the data in A is single, convert A to double before writing to a GIF or TIFF file.
So if you are getting a range up to 255 for your idwt then you should uint8() the data before imwrite()
Jack Moriss
Jack Moriss 2016 年 6 月 22 日
I got confused i think the range is [0 255] i dont know how to specify the range.maybe the problem occure befor saving. because if i dont want to save image and calling it from program when it is double it is ok and the extracted values are correct but when i convert it to uint8 and work with that, it changes the values.
Doanh Ho
Doanh Ho 2018 年 7 月 12 日
Hi Walter Roberson, I really appreciate your answer. But what do you means "do your own DCT", can you clearly explain it?
Walter Roberson
Walter Roberson 2018 年 7 月 12 日
When I wrote about doing your own dct, I was referring to either coding dct yourself or calling dct(), in order to get dct coefficients that you could modify and then do an inverse dct with.
When you imwrite() to a jpeg file, then some layer will do dct on the data you pass in, but you will not have access to the results of the dct and so will not be able to modify the coefficients.
Doanh Ho
Doanh Ho 2018 年 7 月 13 日
Thank you Walter, I want to embed bits to the DC Coefficient. I do dct() on R value, quantize the dctmaxtrix, modifiy the DC coefficient to embed a bit and then do an inverse dct to get back the new R value. Then I save the image with new R value.
image.save(output_file, format='JPEG', subsampling=0, quality=95)
After saving jpeg, it compresses and when I read the image back, I cannot got the new R value. I think I'm wrong when I do image.save() by this way. Because it compressed the image one more time and change RGB value. Could you pleased give me some guides to get the right value after idct() and get the new R value. How to save the new R value? Thank you so much.
Walter Roberson
Walter Roberson 2018 年 7 月 13 日
image.save(output_file, format='JPEG', subsampling=0, quality=95)
is not MATLAB code.

サインインしてコメントする。

Muhammad Usman Saleem
Muhammad Usman Saleem 2016 年 6 月 21 日

0 投票

try this
save('myimage.gpeg','originalImage');

1 件のコメント

Walter Roberson
Walter Roberson 2016 年 6 月 21 日
編集済み: Walter Roberson 2016 年 6 月 21 日
That would result in something that was in .mat file format but with a .gpeg file extension, and which would not be readable by any image viewer.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeImages についてさらに検索

タグ

質問済み:

2016 年 6 月 21 日

コメント済み:

2018 年 11 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by