How can i save an image and read it again without change its value ?
Show older comments
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)
Image Analyst
on 2 Apr 2017
1 vote
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
budi agung
on 2 Apr 2017
Image Analyst
on 2 Apr 2017
Use save() to save it to a .mat file.
Walter Roberson
on 2 Apr 2017
0 votes
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
budi agung
on 5 Apr 2017
Walter Roberson
on 5 Apr 2017
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:
- find a way to write TIFF 6 double precision files -- which your display program probably would not be able to display anyhow; or
- use an integer-to-integer DWT instead of a double precision DWT; or
- 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.
Walter Roberson
on 21 Jan 2019
There is a File Exchange contribution that claims to be able to write double precision TIFF.
budi agung
on 5 Apr 2017
17 Comments
Image Analyst
on 5 Apr 2017
Walter Roberson
on 5 Apr 2017
"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."
budi agung
on 6 Apr 2017
Walter Roberson
on 6 Apr 2017
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.
Shoriful Islam Shuvo
on 6 Jul 2018
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
Walter Roberson
on 6 Jul 2018
The solution is to use one of the integer-to-integer wavelets.
Shoriful Islam Shuvo
on 7 Jul 2018
would you give me some instructions how to perform it in matlab?
Walter Roberson
on 7 Jul 2018
liftscheme = liftwave('haar','int2int');
[cA cH cV cD] = lwt2(YourImage, liftscheme);
You probably need to handle each color plane separately.
Shoriful Islam Shuvo
on 11 Jul 2018
Edited: Shoriful Islam Shuvo
on 11 Jul 2018
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?
Walter Roberson
on 11 Jul 2018
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));
Shoriful Islam Shuvo
on 11 Jul 2018
Edited: Shoriful Islam Shuvo
on 11 Jul 2018
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?
Walter Roberson
on 11 Jul 2018
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".
Shoriful Islam Shuvo
on 11 Jul 2018
Edited: Shoriful Islam Shuvo
on 11 Jul 2018
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?
Image Analyst
on 11 Jul 2018
See attached demos.
Walter Roberson
on 11 Jul 2018
"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
Shoriful Islam Shuvo
on 12 Jul 2018
Edited: Shoriful Islam Shuvo
on 12 Jul 2018
It is not mandatory for me to use mp4 video format.so i have tried uncompressed avi format and looks like it is working
Image Analyst
on 12 Jul 2018
The demos in my files attached to my prior comment above (and again, here) handle .avi. Did you try that?
Categories
Find more on Image Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!