Applying Watermark Tutorial

Advertisements


In this chapter we will see two ways of applying watermark on images. These methods are listed below:

  1. Applying Text WaterMark on image
  2. Applying Image watermark on image

Applying Text WaterMark on image

We are going to use OpenCV function putText to apply text watermark to image. It can be found under Core package. Its syntax is given below:

Core.putText(source,Text,Point, fontFace ,fontScale ,color);

The parameters of this function along with their description are listed below:

Sr.NoParamaters & Description
1Source
The source image.
2Text
The string text that would appear on the image.
3Point
The point where text should appear on image.
4fontFace
Font type. For example: FONT_HERSHEY_SIMPLEX,FONT_HERSHEY_PLAIN,FONT_HERSHEY_COMPLEX e.t.c
5fontScale
Font scale factor that is multiplied by the font-specific base size.
6color
Text color.

Apart from the putText method, there are other methods provide by the Core class. They are listed below:

Sr.NoMethod & Description
1normalize(Mat src, Mat dst, double alpha, double beta, int norm_type)
Normalizes the norm or value range of an array.
2perspectiveTransform(Mat src, Mat dst, Mat m)
Performs the perspective matrix transformation of vectors.
3phase(Mat x, Mat y, Mat angle)
Calculates the rotation angle of 2D vectors.
4rectangle(Mat img, Point pt1, Point pt2, Scalar color)
Draws a simple, thick, or filled up-right rectangle.
5reduce(Mat src, Mat dst, int dim, int rtype, int dtype)
Reduces a matrix to a vector.
6transform(Mat src, Mat dst, Mat m)
Performs the matrix transformation of every array element.

Example

The following example demonstrates the use of Core class to apply text watermark to an image:

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

public class Main {
   public static void main( String[] args )
   {
      try{
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         Mat source = Highgui.imread("digital_image_processing.jpg",
         Highgui.CV_LOAD_IMAGE_COLOR);
         Mat destination = new Mat(source.rows(),source.cols(),
         source.type());  
         Core.putText(source, "Tutorialspoint.com", new Point
         (source.rows()/2,source.cols()/2), 
         Core.FONT_ITALIC,new Double(1),new Scalar(255));

         Highgui.imwrite("watermarked.jpg", source);
         } catch (Exception e) {
            System.out.println("Error: "+e.getMessage());
         }
   }
}

Output

Original Image

Applying WaterMark Tutorial

Text Watermarked Image

Applying WaterMark Tutorial

Applying Image watermark on image

We are going to use OpenCV function addWeighted to apply image watermark to image. It can be found under Core package. Its syntax is given below:

Core.addWeighted(InputArray src1, alpha, src2 (Watermark image), beta, gamma, OutputArray dst);

The parameters of this function along with their description are listed below:

Sr.NoParamaters & Description
1src1
First input array.
2alpha
The weight of the first array elements.
3src2
The second input array of the same size and channel number as src1.
4beta
The weight of the second array elements.
5gamma
Scalar added to each sum.
6dst
Output array that has the same size and number of channels as the input arrays.

Example

The following example demonstrates the use of Core class to apply image watermark to an image.

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

public class Main {
   public static void main( String[] args )
   {
      try{
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         Mat source = Highgui.imread("digital_image_processing.jpg",
         Highgui.CV_LOAD_IMAGE_COLOR);
         Mat waterMark=Highgui.imread("watermark.png",
         Highgui.CV_LOAD_IMAGE_COLOR);
         Rect ROI = new Rect(waterMark.rows()*4,waterMark.cols(), 
         waterMark.cols(),waterMark.rows());
         Core.addWeighted(source.submat(ROI), 0.8, waterMark, 
         0.2, 1, source.submat(ROI));
         Highgui.imwrite("watermarkedImage.jpg", source);
        } catch (Exception e) {
           System.out.println("Error: "+e.getMessage());
        }
   }
}

Output

Original Image

Applying WaterMark Tutorial

The image to be Watermarked

Applying WaterMark Tutorial

Watermarked Image

Applying WaterMark Tutorial

Advertisements
Advertisements