I'm working on watermarking and recently I try to developp the method found here for a blind color watermarking. For embedding the watermark every is Ok I got great results but in extraction part I don't know why I have a black watermark image and it doesn't look like the original one!! Here is the code I use for extraction:
function extracted_mark=extract_dyn_U(rgb_wateramrked,ix,iy);
% rgb_wateramrked=imread('Watermarked_rgbImage.jpg'); %%% read original image
img=rgb2ycbcr(rgb_wateramrked); %%%convert to ycbcr color space
imgy=img(:,:,1);
imgcb=img(:,:,2);
imgcr=img(:,:,3);
[ca1,ch1,cv1,cd1]=dwt2(imgy,'haar'); %%% Apply dwt 2level on ca
[ca2,ch2,cv2,cd2]=dwt2(ca1,'haar');
[m n]=size(ca2);
blocksize=m/32
%********************Extraction**********************
watermark1=zeros(32,32); % define a zero matrix with the original watermak size
for k=1:32
for j=1:32
L=zeros(4,4);
L=ca2(ix:ix+blocksize-1,iy:iy+blocksize-1);
[U,S,V]=svd(L);
if abs(U(2,1))>= abs(U(3,1))
watermark1(k,j)=1;
else
watermark1(k,j)=0;
end
end
end
imshow(watermark1);
extracted_mark=watermark1;
end
end
It just doesn't give me the right extracted watermark :( and in this work I tried with a binary image from the beginning, but I actually need to convert a color watermark from RGB to decimal then to binary so I can use it. and the issue I got too is when I will have an extracted watermark how to reconverted from binary to decimal then RGB!! thank you in advance
This is th original binary watermark (left) and the result after extraction (right). I expect to have an extracted watermark as the original one.
Related
I need help in converting a png image back to the original values calculated in MATLAB. I originally downloaded a RADAR image and used xlim & ylim to to plot a zoomed area, then I used the command line (saveas(gca, [opath 'VS',num2str(k),'L',num2str(LL)], 'png')) to save the image to a png file. Now, I am trying to take the png file and convert it back to the original pre-saved matrix. The problem is I do not get the exact values back, as I originally had. I have a couple of colleagues who have also tried and we cannot get the original numbers. Can anyone help? I can provide a pre-saved matrix, lat & lon file (1832 x 720), the plot routine that I use xlim & ylim, the save line, then the routine that takes the png file and tries to return it to the original pre-saved plotted size.
for i=1%:length(tt)
imgpath2=[imgpath tt(i).name];
map=colormap(jet(256));
RGB=imread(imgpath2);
S=rgb2ind(RGB,map);
S=double(S);
L=size(map,1);
out=interp1(1:L,linspace(min_org,max_org,L),S(:,:));
%Turns white spaces into NaNs
out(out==27.176470588235293)=NaN;
% pixels around campus
ave=nanmean(nanmean(out(cam_wid,cam_len)));
testR=out(cam_wid,cam_len);
testR2=10.^(testR./10);
br=nanmean(testR2(:));
rr(i)=(br/300)^(1/1);
end
I have grayscale satellite image which is processed from spectral data (band classifications). If i use jet colormap in imshow it will show absolute colormapped image. But if i try to imwrite in particular place it is saved like a bluish image. I saw one example in matlab central, but i didnt get. can anyone help me to write my image with colorscaled image.
Matlab central link: http://www.mathworks.in/matlabcentral/answers/25026-saving-grayscale-image-as-it-appears-in-jet-colormap-of-imagesc
there accepted answer link is : http://www.mathworks.com/matlabcentral/fileexchange/7943
I have tried many times, this will show colormaped images in plots (imshow) they didnt write anywhere with colormaped. Now i want to write my image with colormaped.
example code:
I= imread('image path');
imshow(I,'colormap',jet);
imwrite(I,'path','jpg'); /not working
or
imwrite(I,jet,'path','jpg'); /not working
Please help to solve this issue.
When you use imshow the colormap is always adjusted to the range of values in your image. imwrite however assumes your image has a value range of [0,1] if you are using single or double data types. Try to scale your image to the range [0,1] before saving.
If you provide a colormap in the call to imwrite, MATLAB assumes you are using an indexed image. Thus you will have to convert the image to the indexed format first. The following snippet worked for a test image I of mine:
% scale to [0,1]
I = I - min(I(:));
I = I ./ max(I(:));
% Create indexed image
[J,~] = gray2ind(I);
% Save image
imwrite(J,jet,'path','jpg');
Solution by hbaderts worked well for me, but later I found out that some images were still scaled slightly different way from imshow.
However, I might found a reason of an original problem. Just after Matlab starts, its default colormaps (including 'jet') are set to 64 colors (64x3). Then, if any image is shown with a colormap, for example if imshow('cameraman.tif'), colormap('jet') is executed, all default colormaps become 256x3 (can be verified with jetMap=jet; before and after). Then it might happen that an image was written with a colormap different from the one applied to image figure (for example, if a figure called after imwrite).
Finally I found this solution (no image pre-scaling needed):
% Create indexed image, explicitly using 256 colors
imInd=gray2ind(im,256);
% Convert indexed image to RGB using 256-colors jet map
jetRGB=ind2rgb(imInd,jet(256));
% Save image
imwrite(jetRGB,'jet.png');
The images I used have the same color scale now, both the saved one and the one shown in figure.
I am processing microscopy images (in Matlab) in the tiff format, normally uint8 or uint16. Basically I read them, put them in a cell array for processing and then export them in the tiff format either as an image sequence or a stack (using imwrite and either the 'overwrite' or 'append' writemode property of imwrite, respectively). Up to now everything works very well.
The problem I'm having is the following:
When I open the images with ImageJ, they are not in truecolor "RGB" color mode, but rather in composite mode. For example ImageJ reads the data as 8 bit, which it is, but does not open the image as a truecolor (Sorry for the bad choice of words I don't know the right terminology). Hence I have to manually combine the 3 channels together, which is bothersome for large datasets.
Here is a screen shot explaining. On the left is what I would like,i.e. what I obtain if I open the image directly with ImageJ, and on the right is what I currently have after saving images with Matlab and opening them with ImageJ, which I don't want.
The code I'm using to export the image sequence is the following. "FinalSequenceToExport" is the cell array containing the images.
for i = 1:SliceNumber
ExportedName = sprintf('%s%s%d.tiff',fileName,'Z',i);
imwrite(FinalSequenceToExport{i},ExportedName,'tif','WriteMode','overwrite','Compression','none');
end
If I ask Matlab the size of FinalSequenceToExport{1}, for instance, it gives 512 x 512 x 3.
If I open a given image in the command window and then save it with the same code as above, it does what I want and the resulting image opens as I want in ImageJ. Hence my guess would be that the problem arises from the use of the cell array but I don't understand how.
I hope I've been clear enough. If not please ask for more details.
Thanks for the help!
You need to specify the the 'ColorSpace'
Try this
imwrite(FinalSequenceToExport{i},ExportedName,...
'tif','WriteMode','overwrite','Compression','none', ...
'ColorSpace', 'rgb');
After revisiting this question I found the following to work, thanks to the hint from #Ashish:
imwrite(uint8(FinalSequenceToExport{i}/255),...);
I just needed to divide by 255 after converting to uint8.
I am writing a function that generates series of images. I am using the imwrite function to write each image to a file:
Ecc=sqrt(real(E(:,:,1)).^2+real(E(:,:,2)).^2+real(E(:,:,3)).^2+imag(E(:,:,1)).^2+imag(E(:,:,2)).^2+imag(E(:,:,3)).^2);
clf
Q=imagesc(nx/rad,ny/rad,Ecc);
if i==1
cl=caxis;
else
caxis(cl)
end
imwrite(Q,['Frame-',num2str(i),'.tif'],'tif');
But I am not getting the images. The files are generated just fine, but they are just white images with dimension 1x1. Any help please?
Thank you
Use imwrite on Ecc instead of Q. The output of imagesc (as I recall) is a handle to the figure, which is not what you want to write out. Write out Ecc instead.
Adding to what user3817401 has written.
Completly white images can result from data not being scaled prior to being sent to imwrite. Consider following:
Ecc = (Ecc - min(min(Ecc))) / (max(max(Ecc)) - min(min(Ecc)));
promply before imwrite. This will guarantee, that the image is in range 0-1 and should solve the problem.
The function imagesc returns a handle (you store it as Q), not scaled image data. Then, the function imwrite is interpreting Q as an image. Because it is a handle, it is just 1x1 and it's value is not meaningful as an image. Try scaling Ecc as desired and then writing that instead.
I used these codes to produce an RGB image with gray levels between 50 and 170.
a='C:\Users\sepideh\Desktop\IP_abadpour\S45C-113050518040.jpg';
b=imread(a);
b=b+50;
b(b>170)=170;
and you'll see when I call functions max and min, it is proved that the gray levels are between 50 and 170.
max(max(max(b)))
ans =
170
min(min(min(b)))
ans =
50
then I used imshow and imsave functions to save the image with the name "50to170"
c=imshow(b);
d=imsave(c);
Now I read the written image in this way:
a='C:\Users\sepideh\Desktop\IP_abadpour\50to170.jpg';
b=imread(a);
This time when I call max and min functions,I see:
max(max(max(b)))
ans =
235
min(min(min(b)))
ans =
16
I mean it seems that gray levels have been changed after using imshow and imsave functions!
Why does it happen?
Is it because of the format (.jpg) that I'm using when employing imsave function?
Instead of using imsave, use imwrite
b=imread(a);
b=b+50;
b(b>170)=170;
imwrite(b,'50to170.png','png')
Notice that I am saving it as a png file instead of a jpg to prevent compression. Bitmap also saves it without compression.
This method is a more direct way to save raw image matrices than using imshow and imsave.
If you want the same functionality of imsave (selecting where the file goes) check out "uiputfile" to get file name and location.
I tested it.
1.First of all you can't use imsave(b) because the function imsave expects its first input argument to be a valid handle to a single graphics object.So first you should show the image in form c=imshow(b) and then use c as an input for function imsave.
2.If you want to save the image without showing it first use function "imwrite" and pass b as the first input argument to it.
Note that if you don't want gray levels be changed after saving, you should use 'bmp' as the save format not 'jpg'.
Because 'jpg' does not support indexed images and "imwrite" converts indexed images to RGB before writing data to JPEG files so the gray levels might be changed.It's not related to the contrast stretching when showing the image in matlab.It's related to the format used to save the image.