📜  Java DIP-应用水印

📅  最后修改于: 2020-12-14 05:40:53             🧑  作者: Mango


在本章中,我们学习在图像上应用水印的两种方法。这些方式是-

  • 应用文字水印
  • 应用图像水印

应用文字水印

我们使用OpenCV函数putText将文本水印应用于图像。可以在Core软件包下找到。其语法如下-

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

该函数的参数如下所述-

Sr.No. Parameter & Description
1

Source

It is source image.

2

Text

It is the string text that would appear on the image.

3

Point

It is the point where text should appear on image.

4

fontFace

Font type. For example − FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_COMPLEX etc.

5

fontScale

It is font scale factor that is multiplied by the font-specific base size.

6

color

It is text color.

除了putText方法外,Core类还提供了其他方法。他们简要描述-

Sr.No. Method & Description
1

normalize(Mat src, Mat dst, double alpha, double beta, int norm_type)

It normalizes the norm or value range of an array.

2

perspectiveTransform(Mat src, Mat dst, Mat m)

It performs the perspective matrix transformation of vectors.

3

phase(Mat x, Mat y, Mat angle)

It calculates the rotation angle of 2D vectors.

4

rectangle(Mat img, Point pt1, Point pt2, Scalar color)

It draws a simple, thick, or filled up-right rectangle.

5

reduce(Mat src, Mat dst, int dim, int rtype, int dtype)

It reduces a matrix to a vector.

6

transform(Mat src, Mat dst, Mat m)

It performs the matrix transformation of every array element.

以下示例演示了如何使用Core类将文本水印应用于图像-

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());
      }
   }
}

输出

当您执行给定的代码时,将看到以下输出-

原始图片

应用WaterMark教程

文字水印图像

应用WaterMark教程

在图像上应用图像水印

我们将使用OpenCV函数addWeighted将图像水印应用于图像。可以在Core软件包下找到。其语法如下-

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

该函数的参数如下所述-

Sr.No. Parameter & Description
1

src1

It is first input array.

2

alpha

It is the weight of the first array elements.

3

src2

It is the second input array of the same size and channel number as src1.

4

beta

It is the weight of the second array elements.

5

gamma

It is the scalar added to each sum.

6

dst

It is the output array that has the same size and number of channels as the input arrays.

以下示例演示了如何使用Core类将图像水印应用于图像-

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());
      }
   }
}

输出

当您执行给定的代码时,将看到以下输出-

原始图片

应用WaterMark教程

水印图像

应用WaterMark教程

带水印的图像

应用WaterMark教程