OpenCV-Python Development Guide (6) --- Embedding and Extraction of Digital Watermark

Preface

When we explained bit-plane decomposition in the previous blog post, we mentioned that we can add watermark to an image by means of bit-plane decomposition. Numerical watermarking is the most used encryption method for image copyrights.

By hiding the binary image information in the bit-plane decomposition map of the least significant bit, it has extremely high concealment. Therefore, I kindly remind you programmers not to think that the images on the Internet can be used casually. The current encryption method really makes you hard to guard against. Even if you know the principle of watermarking who adds the value, I'm afraid you can't find out which of them is secret information. (Respect copyright, don’t have a fluke mentality)

Digital watermarking process

From the point of view of the bit plane, the process of digital watermarking is divided into the following steps:

(1) Embedding process: Replace the 0th bit plane of the carrier image with digital watermark information.

(2) Extraction process: Extract the least significant bit plane of the carrier image to obtain digital watermark information.

Code embedding and extracting digital watermark

After we know the principle, we can start writing code directly:

import cv2
import numpy as np

img = cv2.imread("4.jpg", 0)
watermark = cv2.imread("watermark.jpg", 0)
#因为水印图像就是让人不易察觉也不影响原图像,所以要将水印非0位全部替换位最小值1
w = watermark[:, :] > 0
watermark[w] = 1
#嵌入水印
r, c = img.shape
#生成元素都是254的数组
img254=np.ones((r,c),dtype=np.uint8)*254
#获取高7位平面
imgH7=cv2.bitwise_and(img,img254)
#将水印嵌入即可
water_img=cv2.bitwise_or(imgH7,watermark)
cv2.imshow("1",img)
cv2.imshow("2",watermark*255)
cv2.imshow("3",water_img)
#生成都是1的数组
img1=np.ones((r,c),dtype=np.uint8)
#提取水印
water_extract=cv2.bitwise_and(water_img,img1)

#将水印里面的1还原成255
w=water_extract[:,:]>0
water_extract[w]=255
cv2.imshow("4",water_extract)

cv2.waitKey()
cv2.destroyAllWindows()

As we mentioned earlier, the lowest bit plane of the image is 00000001, and the other 7 bit planes combined is 11111110, which is 254, so we created an array of all 254. Obtain the high 7-bit plane through it, and then merge it with the digital watermark map.

Similarly, to extract the digital watermark in the image, we only need to obtain the least significant bit plane, and then change its 1 value to 255 to display the original watermark image.

After running, the displayed effect is as shown in the figure below:
digital water mark

Guess you like

Origin blog.csdn.net/liyuanjinglyj/article/details/113774784