Basic image manipulation using Java with text and image watermarking example ( OpenCV )

Now a days images are the one of the most important portion of research. In this post I will discuss about basic image read write method.

Topic to be covered in this post –

  • Open cv
  • Basic image read write function
  • Text watermarking


Basic image manipulation using Java with text and image watermarking example ( OpenCV )


OpenCV: Open Computer Vision
Widely used for image processing technology. We will use opencv putText and addWeighted functions for text and image watermarking respectively.\

Download Code


Imgcodecs.imread -> read an image

public static Mat imread(java.lang.String filename, int flags)
  • filename - Name of file to be loaded.
  • flags - Flags specifying the color type of a loaded image:
     >0 Return a 3-channel color image
     =0 Return a grayscale image
     <0 Return the loaded image as is

flag specified constant value:


imwrite: Saves an image to a specified file.

public static boolean imwrite(java.lang.String filename, Mat img)
  • filename - Name of the file.
  • img - an image


Core.putText -> render text on image

public static void putText(
      Mat img,
     java.lang.String text,
     Point org,
     int fontFace,
     double fontScale,
     Scalar color,
     int thickness,
     int lineType,
     boolean bottomLeftOrigin
  • img - Image.
  • text - Text string to be drawn.
  • org - Bottom-left corner of the text string in the image.
  • fontFace - Font type. One of 
    where each of the font ID's can be combined with 
    FONT_HERSHEY_ITALIC to get the slanted letters.
  • fontScale - Font scale factor that is multiplied by the 
    font-specific base size.
  • color - Text color.
  • thickness - Thickness of the lines used to draw a text.
  • lineType - Line type. See the line for details.
  • bottomLeftOrigin - When true, the image data origin is at the 
    bottom-left corner. Otherwise, it is at the top-left corner.


addWeighted: Computes the weighted sum of two arrays using an equation.

dst(I)= saturate(src1(I)* alpha + src2(I)* beta + gamma)

public static void addWeighted(
    Mat src1, 
    double alpha, 
    Mat src2, 
    double beta, 
    double gamma, 
    Mat dst)
  • src1 - First source array.
  • alpha - Weight for the first array elements.
  • src2 - Second source array of the same size and channel 
    number as src1.
  • beta - Weight for the second array elements.
  • gamma - Scalar added to each sum.
  • dst - Destination array that has the same size and number of 
    channels as the input arrays.

Text Watermarking

Example –

Input image –

Watermarked image-

You will find the watermarked image into your project directory.


Image Watermarking

Example –


Input image-

Watermarked logo-


Watermarked image-

You will find the watermarked image into your project directory.

I hope this post explained concepts of doing watermarking using java. If you still have any queries, please do let me know in the comments section below.

Happy Learning 🙂

Download Code


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s