Home > English, Matlab > Similarity threshold computation for Cox watermarking

Similarity threshold computation for Cox watermarking

If you dive into watermarking field, you will have to deal for sure with Cox embedding technique.

Cox’s embedding works in the transform domain (e.g. DCT or DWT) while LSB works in the spatial domain. The idea behind this technique is the following:

1) take an image and a watermark (let’s say the watermark is 512*512 binary)

2) compute the DCT (DWT) of the image, and compute all it’s coefficients

3) sort the coefficients preserving their position

4) we know that most of energy is (approximately) inside the first 1000 coefficients so we could easily embed the watermark inside the first 1024 coefficients by exploiting on each of these coefficients one of the following embedding:

  • V'(i) = V'(i) * (1 + alpha*W(i))
  • V'(i) = V(i)+ alpha*W(i)
  • V'(i) = V(i) * e ^ (alpha*W(i))

where V(i) is the original value of the coefficient, V'(i) is the value of the coefficient after the embedding, W(i) is the watermark and alpha is a strength embedding factor.

5) Reorder the coefficients on the their original positions and perform an antitrasform

Since the code is pretty easy I will not post it here, but if you have troubles please just write me and I will make it available.

Ok, at this point we have a watermarked image…but what about the detection???

Well, you can’t recover the original watermark as you did for LSB watermarking, but you can assert whether your mark is inside the cover image or not. The procedure is the following:

1) take the watermarkded image

2) compute again the DCT (DWT) of the image, and compute all it’s coefficients

3) sort again the coefficients preserving their position

4) try to compute an approximate mark (Wap(i)) by inverting the formula you used in embedding

5) Check if the similarity between the original mark and Wap(i) is above a certain threshold, if so your watermark is present

To compute the threshold you have to run an experimental study; basically you must compute the similarity between many fake watermarks (e.g. 999) and the original watermark, than the similarity between your watermark and itself.

Similarity fuction is defined as: SIM(W,Wi) = (W * Wi) / sqrt(Wi * Wi)

When you have all the similarities, you may notice that you have many peaks you can see as noise, and a peak where you computed the similarity with itself. To set the threshold you just need to take the second peak and add it its 15%, so that you will rarely have false positive matching.

Here is the code to compute the similarity:

function [threshold,sim_detection] = threshold_computation
    %% generate 999 random fake (1,1024) watermarks
    for i=1:999
    %% load from a mat file the original (512,512) watermark
    %% (variable name is watermark as well)
    %% reshape original watermark to (1,1024)
    [r c]=size(wm);

    %% add reshaped watermark as the 1000 element of watermarks set

    %% for each watermark compute similarity detection and save result in a vector
    for i=1:1000
    %% sort values according to magnitude
    %% compute threshold

Let’s execute the code:

>> [real_threshold,threshold_vec] = threshold_computation;
>> plot(threshold_vec,'DisplayName','threshold_vec','YDataSource','threshold_vec');

Here is the result of the plot operation, in the abscissa we have the watermarks, while in the in ordinate we have the similarity computed for the given watermark (notice that in abscissa 1000 there is a peak of similarity due to the fact that for the computation we are using the real watermark).

That’s all foks! Stay tuned!

  1. Cleiton Mello
    May 31, 2010 at 10:25 am

    Dear Paolo,
    I found the information posted in the Similarity threshold computation for Cox watermarking page very useful.
    I’m actually going through the exercise of embedding a watermark by DCT.
    I’m a beginner in MatLab and am not quite sure of my results.
    I create a WM vector with “wVector = randn(n,1);”
    and change the AC coefficients of the original matrix by doing:
    for i= 2:n
    dctPlusWM(I(i)) = ((B(i)*(1+0.1*wVector(i))));
    ([B,I] = sort(original DCT matrix, ‘descend’)

    I’d apreciate if you could provide any feedback.



    • pdincau
      May 31, 2010 at 11:46 am


      the idea behind your implementation is right, but you should sort the dct coefficients before the embedding. After the sorting embed the wm in the strongest coeffs skipping the first, so that you will not effect the AC component. After the embedding just reorder the dct coefficients and go back by using idct.

      hope you found this helpful, otherwise just write me again!

      • Deepti
        May 15, 2013 at 5:11 pm

        Oh now i got your post on cox watermarking .. i think now my problem will get solved soon….but can you please help me how to sort the cofficients???

  2. saeef
    July 7, 2010 at 6:40 pm

    Would you like to upload complete code …. plz
    Thanks in advance ….

    • pdincau
      July 7, 2010 at 9:57 pm

      you mean cox embedding?

      • Deepti
        May 15, 2013 at 5:12 pm

        Or upload the code for cox embedding
        Thanks in advance

      • pdincau
        May 15, 2013 at 5:15 pm

        Read the code: Iused function sort. Remember to skip first coefficient.

        Don’t want to be unpolite here, but should read and google before asking.

  3. saeef
    July 8, 2010 at 7:48 am

    I am trying to implementing cox watermarking for audio.
    I am new to Matlab, so facing trouble.
    How you generate watermak?
    I wrote as:

    is it ok? or i have to do something additional?


    • pdincau
      July 8, 2010 at 8:10 am

      you can do something like:


      in this case i used the round() function so that the mark will be binary.
      Anyhow this solution is not good, because in the recovery part you will need the same watermark for the similarity compution.

      You have three solutions:
      1) go further as above, then store the watermark on disk as a .mat file (see save function). In recovery part you can read the file
      2) as above, but instead of saving the watermark, you can just hardcode it in recovery file
      3) generate the watermark using (for example):


      so that in the recovery function you will be able to generate exactly the same watermark

      • pdincau
        July 8, 2010 at 8:14 am

        ps, try this and if you are still in trouble i can write a post on this topic

  4. Pothik
    July 8, 2010 at 7:52 am

    I wrote for similarity:
    “function [Sim] = Similarity(Wmark, ExtractedWmark,N)
    [r,c] = size(Wmark);
    if (c ~= 1 && r ~= 1)
    error(‘Wmark should be a vector.’)
    [r,c] = size(ExtractedWmark);
    if (c ~= 1 && r ~= 1)
    error(‘ExtractedWmark should be a vector.’)
    for k=1:N
    Sim(k) = Wmark(k) * ExtractedWmark(k) / sqrt(ExtractedWmark(k) * ExtractedWmark(k));

    • pdincau
      July 8, 2010 at 8:28 am

      why do you do this? i mean, if you have the original wm and the extracted wm you can just do:

      SIM = w * w_ext’ / sqrt( w_ext * w_ext’ )

      I used a cicle in my code only because i was looking for a threshold… plus are you sure that you can do the vectorial product? as you can see i used the apostrophe character to do that.

  5. israkir
    November 7, 2011 at 7:22 am

    Don’t you it is strange that actually you are comparing the similarity of original embedded watermark with itself at index 1000?

    • pdincau
      November 21, 2011 at 1:03 pm


  6. phucmanh
    November 27, 2011 at 4:26 am

    Help me!
    Programming in matlab
    I’m making a Project on digital watermarking on digital images.
    I have to implementate it by DCT domain, embed text watermark in image to protected copyright.

    Would you please share with me.!!

    Thank you everyone, thank you verymuch !!!!!!!

    And now:
    matlab code project: digital signature on RSA (or DSS, ..) //on digital image.
    MD5 (or SHA,..) // on digital image

    Please contact me : manhphuc.cnt48@gmail.com

  7. sona
    February 26, 2012 at 10:29 am

    please post the code for Cox watermarking embedding and extracting

  8. Nora
    May 8, 2014 at 1:23 pm

    Dear Paolo,
    I found the information posted in the LSB watermarking page very useful.
    can you please help me with this question that how one should modify the algorithms sothat the watermark can survive attacks such as LSB truncation or scaling and rotation.

    Any help would be much appriciated

    • pdincau
      May 8, 2014 at 1:45 pm

      Hello, LSB is just a study algorithm. You may want ti search on IEEE for other algorithms or design your own strategy based on tour needs

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: