Add watermark to an image in C++

In this tutorial, we will learn how to add watermark to an image in C++. For doing so we are going to use OpenCV. Before proceeding further let’s first understand what is a watermark.

Watermark: A watermark is a pattern, text or logo that is added to another image to make it more difficult for the original image to be copied or used without permission.

Mostly, websites that sell digital images make use of watermarks. They apply watermarks on low-resolution copies of images, so the user can preview them and when the user purchases the image they provide a high-resolution watermark-free copy of that image to the user.




Main image:

Main image

Watermark image:

watermark

Watermarked image:

final watermarked image in C++

Note:

  1. OpenCV C++ must be installed and linked to Visual Studio on your PC. (if you don’t know how to do it then click here.)
  2. The resolution of both files must be the same.
  3. If the image files that you want to add are in the directory of the project then just enter the name of the files with extension otherwise enter the full path of the files while entering the name of the image files in the below program.

Program to add watermark to an image using OpenCV in C++

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

int main()
{
  Mat main_image,watermark, watermarked_image;
  string file;
  double alpha,beta;

  /* Enter path of the file if file is not in the directory of the project */
  cout << "\nEnter name of a main image file with extension: ";

  /* cin function cannot read space. Hence we have used getline function here. */
  getline(cin, file);

  /* imread() is used to read an image */
  main_image = imread(file);

  /* Checking if main_image object contains an image */
  if (main_image.data==0)
  {
    cout << "\n\nError..!!\n";
    cout << "\nCheck=>";
    cout << "\n1. Desired image is in the folder of the project";
    cout << "\n2. You have mentioned extension of an image file\n";
    exit(0);
  }

  cout << "\nEnter name of a watermark file with extension: ";
  getline(cin, file);

  watermark = imread(file);

  /* Checking if watermark object contains an image */
  if (watermark.data == 0)
  {
    cout << "\n\nError..!!\n";
    cout << "\nCheck=>";
    cout << "\n1. Desired image is in the folder of the project";
    cout << "\n2. You have mentioned extension of an image file\n";
    exit(0);
  }

  /* Checking if both files have equal resolution or not */
  if (main_image.rows != watermark.rows || main_image.cols != watermark.cols)
  {
    cout << "\n\nError..!!\n";
    cout << "\nResolution of both images is not same\n";
    exit(0);
  }

  /* alpha denotes the opacity of the watermark and beta denotes the opacity of the main_image. You can choose alpha and beta values as per your choice but make sure that the sum of alpha and beta is 1. Here we have used alpha as 0.1 and beta as 0.9 */
  alpha = 0.1;
  beta = 1 - alpha;

  /* Here we are merging 'main_image' and 'watermark' and output will be stored in 'watermarked_image' */
  addWeighted(watermark, alpha, main_image, beta, 0.0, watermarked_image);

  /* namedWindow() is used to create a window with the mentioned name. In our case, window name is 'Watermarked image' */
  namedWindow("Watermarked image", WINDOW_AUTOSIZE);

  /* imshow() is used to display an image */
  imshow("Watermarked image", watermarked_image);

  /* Press esc to exit */
  if (waitKey(0) == 27)
  {
    exit(0);
  }

  /* when everything done, close all windows */
  destroyAllWindows();

  return 0;
}

Input/Output:

Add watermark to an image in C++

You may also read:

  1. Play Music in C++
  2. Morris Preorder Tree Traversal in C++
  3. Topological Sort in C++


Leave a Reply

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