In this chapter we learn two ways of applying watermark on images. These ways are −

  • Applying Text Watermark
  • Applying Image watermark

Applying Text Watermark

We 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 are described below −

Sr.No.Parameter & Description
1SourceIt is source image.
2TextIt is the string text that would appear on the image.
3PointIt is the point where text should appear on image.
4fontFaceFont type. For example − FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_COMPLEX etc.
5fontScaleIt is font scale factor that is multiplied by the font-specific base size.
6colorIt is text color.

Apart from the putText method, there are other methods provided by the Core class. They are described briefly −

Sr.No.Method & Description
1normalize(Mat src, Mat dst, double alpha, double beta, int norm_type)It normalizes the norm or value range of an array.
2perspectiveTransform(Mat src, Mat dst, Mat m)It performs the perspective matrix transformation of vectors.
3phase(Mat x, Mat y, Mat angle)It calculates the rotation angle of 2D vectors.
4rectangle(Mat img, Point pt1, Point pt2, Scalar color)It draws a simple, thick, or filled up-right rectangle.
5reduce(Mat src, Mat dst, int dim, int rtype, int dtype)It reduces a matrix to a vector.
6transform(Mat src, Mat dst, Mat m)It 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

When you execute the given code, the following output is seen −

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 are described below −

Sr.No.Parameter & Description
1src1It is first input array.
2alphaIt is the weight of the first array elements.
3src2It is the second input array of the same size and channel number as src1.
4betaIt is the weight of the second array elements.
5gammaIt is the scalar added to each sum.
6dstIt is the 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

When you execute the given code, the following output is seen −

Original Image

Applying WaterMark Tutorial

The Watermark Image

Applying WaterMark Tutorial

Watermarked Image

Applying WaterMark Tutorial

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *