How can i save an image and read it again without change its value ?

im doing watermarking image using DWT and got problem about the value change when i try to read the watermarked image
imwrite(Watermarkedimage_final ,'iwater.bmp');
x=imread('iwater.bmp');
x=im2double(x);
[F11,F22]= wfilters('haar', 'd');
[a b c d]=dwt2(x,'haar','d');
[aa bb cc dd]=dwt2(a,'haar','d');
[aaa bbb ccc ddd]=dwt2(aa,'haar','d');
recovered_image=imadjust(recovered_image);
imshow(uint8(recovered_image),[]);
imwrite(recovered_image ,'bmp.bmp');

Answers (3)

Watermarkedimage_final and the badly-named "x" will have the same values because BMP is a lossless format.
Likewise, if you use imread to read in the badly-named 'bmp.bmp' then the recalled image will have the same values as uint8(recovered_image).

2 Comments

ok thanks.
can i imwrite image in double data type ?
Use save() to save it to a .mat file.

Sign in to comment.

Sorry, No, MATLAB does not support any double precision floating point image formats. It supports single precision TIFF; see https://www.mathworks.com/matlabcentral/answers/7184-how-can-i-write-32-bit-floating-point-tifs-with-nans#comment_15023 .
TIFF 6.0 added support for IEEE double precision but MATLAB is not able to write those out.

3 Comments

The TIFF class can only handle single precision. The TIFF file format revision 6 and later can handle double precision, but imwrite() and the TIFF class cannot write version 6 TIFF files.
In other words, if you need to be able to write the watermarked data as an image file, you have three choices:
  1. find a way to write TIFF 6 double precision files -- which your display program probably would not be able to display anyhow; or
  2. use an integer-to-integer DWT instead of a double precision DWT; or
  3. modify the values in such as way that after writing to file and reading back in the changed data is reliably distinguishable from the original values and can be processed to extract the watermark.
LSB (Least Significant Bit) modifications of the double precision wavelet transform are not enough to be able make recoverable changes.
There is a File Exchange contribution that claims to be able to write double precision TIFF.

Sign in to comment.

this is my code
clc
close all
%host
cover=imresize(imread('host.jpg'),[256 256]);
%rgbimage=rgb2gray(rgbimage);
cover=im2double(cover);
[F1,F2]= wfilters('haar', 'd');
figure;imshow(cover);title('original color image');
[h_LL,h_LH,h_HL,h_HH]=dwt2(cover,'haar', 'd');
[h_LL2,h_LH2,h_HL2,h_HH2]=dwt2(h_LL,'haar', 'd');
[nRow nCol noDim] = size(h_LL2);
water=imresize(imread('watermark.jpg'),[nRow nCol]);
%water=rgb2gray(water);
water=im2double(water);
%watermarking
newhost_LL = h_LL2 +0.1*water;
%output
hasil1=idwt2(newhost_LL,h_LH2,h_HL2,h_HH2,'haar', 'd');
hasil1=idwt2(hasil1,h_LH,h_HL,h_HH,'haar', 'd');
figure;imshow(hasil1);title('Watermarked image');
imwrite(hasil1,'Watermarked.bmp','bmp');
%extracted
watermarked=imread('watermarked.bmp');
watermarked=im2double(watermarked);
[wm_LL,wm_LH,wm_HL,wm_HH]=dwt2(watermarked,'haar', 'd');
[wm_LL2,wm_LH2,wm_HL2,wm_HH2]=dwt2(wm_LL,'haar', 'd');
newwatermark_LL= (wm_LL2-h_LL2)/0.1;
figure;imshow(newwatermark_LL);title('Extracted watermark');
%imwrite(newwatermark_LL,'EeweWatermark.bmp');
i didn't got the same value the image that i write in .bmp and the image that i read in .bmp

17 Comments

Did you overlook my answer where I said to use save() to save it to a .mat file?
"For most formats:
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."
i need it to be save in image format like .bmp
Well, you cannot do that. The BMP File Format does not support floating point values. This is not a limitation of MATLAB: this is a limitation of .bmp images.
The image file formats handled by MATLAB that can theoretically support floating point values are only PNG files, and DICOM files, and TIFF files.
  • To write floating point values into PNG files, you need to store them as raw blocks of uninterpreted data, just binary blobs; image display programs would not be able to display the result. If I recall correctly, MATLAB does not provide a way to writing these kinds of blocks, but I am not certain of that.
  • To write floating point values into DICOM files, you need to use C.7.6.25 Double Floating Point Image Pixel Module, which looks like it might be new as of 2016. I am not sure if dicomwrite() is able to handle these at all.
  • You can write single precision TIFF using the Tiff class in MATLAB; I linked to that above
  • Double precision TIFF is too new to be available from MATLAB.
What you really need to do is what I outlined above https://www.mathworks.com/matlabcentral/answers/333229-how-can-i-save-an-image-and-read-it-again-without-change-its-value#comment_443383 -- use an integer-to-integer wavelet, or make larger changes to the values.
I am also working on water marking using DWT and have faced the same problem.if you have got any solution please share it with me.It will be very helpful for me. @budi agung
The solution is to use one of the integer-to-integer wavelets.
would you give me some instructions how to perform it in matlab?
liftscheme = liftwave('haar','int2int');
[cA cH cV cD] = lwt2(YourImage, liftscheme);
You probably need to handle each color plane separately.
thank you for your response.That was really helpful.Now I am seeking for inverse integer wavelet transform.I am using ilwt2.But I got several value that have turned into double.How can i solve this problem?
dimg = double(YourImage);
liftscheme = liftwave('haar','int2int');
[cA cH cV cD] = lwt2(dimg, liftscheme);
idx = randperm(numel(cA), 16);
cA(idx) = cA(idx) + 1;
reconstructed = cast( ilwt2(cA, cH, cV, cD, liftscheme), class(YourImage));
imwrite(reconstructed,'52.jpg');
>> a=imread('52.jpg');
>> a(20:30)
ans =
50 50 57 60 54 60 62 61 57 52 49
>> reconstructed(20:30)
ans =
50 53 56 56 57 62 68 63 55 53 53
>> I have used the code.but when i am writing the image using imwrite the values of the pixel of the written image and the reconstructed image is not exactly same.How can i get rid of it?
If you care about the exact values of the pixels written, do not write to JPEG files. By default, JPEG uses lossy compression which does not preserve exact values. Use png or tiff instead.
If writing to JPEG is mandatory, then you will need to do a discrete cosine transform of the image, modify the dct coefficients, and idct, and then you could write to a jpeg . To recover the values, you would need to dct and extract the data from the dct coefficients.
Note that combining dct and wavelet techniques is not typically done, because the dct is already a technique for reducing "energy".
I have solved the problem using bmp format.but now i want to insert it in a mp4 video without changing the pixel value.can you please help me?
"now i want to insert it in a mp4 video without changing the pixel value."
Sorry, that is difficult.
"mp4" is a container format, but typically when people refer to "mp4" they are referring to using the H.264 ("MPEG-4") codec, which is a lossy compression codec.
MATLAB's videowriter() supports a losslesscompression option, but only for Motion JPEG 2000 . Using vision.VideoFileWriter() does not help for this purpose: when it is asked to create .mp4 files, it uses H.264/MPEG-4
In order to write to .mp4 file with a lossless codec, you would need to either write your own routines, or find a C/C++ shared library to do the work, or call a different programming language that people have written appropriate tools for. For example you could use MATLAB to call a known Java Library https://stackoverflow.com/questions/11345343/png-image-files-to-video-lossless
It is not mandatory for me to use mp4 video format.so i have tried uncompressed avi format and looks like it is working
The demos in my files attached to my prior comment above (and again, here) handle .avi. Did you try that?

Sign in to comment.

Asked:

on 2 Apr 2017

Commented:

on 21 Jan 2019

Community Treasure Hunt

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

Start Hunting!