Chapter 5-Digital Watermark-2-Principle and Implementation

Digital watermark principle

According to the previous operation of image acquisition bit plane, it can be seen that the lowest bit plane has the least impact on the overall image. Therefore, the principle of digital watermark is to embed hidden information in the least significant bit of the image, that is, replace the lowest bit of the image with a digital watermark bit plane. , complete the digital embedding operation, extract the lowest bit plane from the image with embedded digital watermark, and then obtain the digital watermark and the decrypted image. Since the operation is on the lowest bit plane, the impact on the image can be ignored.

Digital watermark manufacturing

The implementation of digital watermarking is based on the specific implementation of the above principles. First, prepare the lena image (512*512) that needs to embed the digital watermark and the original image (512*512) to obtain the digital watermark.

The original image of the digital watermark is words written randomly on a pure white (255) background. The original image of the digital watermark is turned into a digital watermark image through the code.

The general steps are as follows:

  1. Digital raw images are displayed as grayscale images
  2. The key information is isolated in the grayscale image, specifically setting the background color to 0 (the pure white background 255 used here may actually be a certain range of color intervals)
  3. The grayscale image only has the key information of the watermark. Change the color value of this information to 1 and the grayscale image to the lowest bit plane to obtain the digital watermark.

code show as below:

import cv2 as cv
import numpy as np

# --------------------------制造数字水印
# step1 读取原始图
key = cv.imread("key.png", 0)
#  step2 得到数字水印灰度图 可以看到文字及背景白色
cv.imshow("key255", key)
# step3  之前提取位平面图的掩模操作,把白色255背景变成黑色0
key[key == 255] = 0
cv.imshow("key0", key)
# step4 再把文字变成1,这样就得到了数字水印
key[key > 0] = 1
cv.imshow("key", key)
cv.waitKey()
cv.destroyAllWindows()

The running effect is as follows:

Digital watermark embedding and extraction

After understanding the principle of digital watermarking, it will be very simple to analyze the embedding and extraction of numbers.

  1. First generate the digital watermark, just look at the above.
  2. Process the lowest bit plane of the carrier image and use a 254 matrix to perform an AND operation with the carrier image.
  3. The digital watermark can be added to the lowest bit plane of the carrier image, and the embedding of the digital watermark is completed.
  4. Generate a matrix with all 1s and perform an AND operation with the carrier image to obtain the lowest bit plane. What is obtained at this time is the watermark.
  5. The original image with the lowest bit plane lost can be obtained by subtracting the image embedded with the digital watermark.
  6. At this time, the watermark only has 1 and 0, and then it is converted into a binary image to display the image of the digital watermark, and the display is completed.

The specific implementation is as follows:

import cv2 as cv
import numpy as np

# --------------------------制造数字水印
# step1 读取原始图
key = cv.imread("key.png", 0)
#  step2 得到数字水印灰度图 可以看到文字及背景白色
cv.imshow("key255", key)
# step3  之前提取位平面图的掩模操作,把白色255背景变成黑色0
key[key == 255] = 0
cv.imshow("key0", key)
# step4 再把文字变成1,这样就得到了数字水印
key[key > 0] = 1
cv.imshow("key", key)
# -----------------------嵌入水印过程
# step1 生成值全为254的模板
lena = cv.imread("lena.jpg", 0)
cv.imshow("lena", lena)
r, c = lena.shape
mask_254 = np.ones((r, c), dtype=np.uint8) * 254
# step2 用254模板把lena的最低位
lena_high_bit = cv.bitwise_and(lena, mask_254)

# step3 去掉最低位的lena嵌入数字水印 这一步用或运算也行
# lena_water = cv.bitwise_xor(lena_high_bit, key)
lena_water = lena_high_bit + key

cv.imshow("lena_water", lena_water)

# -----------------------提取水印过程
# step4 生成各全为1的模板,用来把数字水印提取出来
mask_1 = np.ones((r, c), dtype=np.uint8)
# step5 嵌入水印的图像与1模板做与运算,把水印提取出来
key_after = cv.bitwise_and(lena_water, mask_1)
# step6 嵌入水印的图像减去水印得到的就是失去最低位的"原图" 这一步也可以用254的模板做与运算
# mask_254 = np.ones((r, c), dtype=np.uint8) * 254
# lena_after = cv.bitwise_and(lena_water, mask_254)
lena_after = lena_water - key_after
cv.imshow("lena_after", lena_after)

# step7 把数字水印转化为二值图,直接显示出来
key_after[key_after > 0] = 255
cv.imshow("key_after", key_after)

cv.waitKey()
cv.destroyAllWindows()

The running effect is as follows:

Summary: Knowing the principle of digital watermarking makes the overall implementation much easier. Basically, adding a digital watermark to the lowest bit of the image, and extracting the watermark means extracting the lowest bit plane of the image with the watermark embedded in it. The following visual watermarks and artistic text are generally regional operations on images and digital watermark images, and will not be discussed separately.

Guess you like

Origin blog.csdn.net/sunguanyong/article/details/130328494