preface
In a previous post on bitplane decomposition, we mentioned that you can use bitplane decomposition to add watermarks to images. Numerical watermarking is the most commonly used encryption method for image copyright.
By hiding binary image information in the bit-plane decomposition of the least significant bits, it has high concealment. Therefore, the friendship prompts each programmer, do not think that the image on the net can be used casually, now the encryption way is really so that you cannot guard against. Even if you know how to watermark a value, you probably won’t be able to figure out which one is secret. (Respect copyright, don’t take any chances)
Digital watermarking process
From the perspective of bit plane, the processing 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: the least significant bit plane of the carrier image is extracted to obtain digital watermark information.
Code to embed and extract digital watermarking
Now that we know the principles, we can start writing code:
import cv2
import numpy as np
img = cv2.imread("4.jpg".0)
watermark = cv2.imread("watermark.jpg".0)
# Because the watermark image is not easy to detect and does not affect the original image, it is necessary to replace all the non-0 bits of the watermark with the minimum value 1
w = watermark[:, :] > 0
watermark[w] = 1
# embed watermark
r, c = img.shape
Create an array of 254 elements
img254=np.ones((r,c),dtype=np.uint8)*254
Get the high 7 bit plane
imgH7=cv2.bitwise_and(img,img254)
# insert watermark
water_img=cv2.bitwise_or(imgH7,watermark)
cv2.imshow("1",img)
cv2.imshow("2",watermark*255)
cv2.imshow("3",water_img)
# generate arrays of all 1s
img1=np.ones((r,c),dtype=np.uint8)
# Extract watermark
water_extract=cv2.bitwise_and(water_img,img1)
# Restore 1 in watermark to 255
w=water_extract[:,:]>0
water_extract[w]=255
cv2.imshow("4",water_extract)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code
As we saw earlier, the least significant plane of the image is 00000001, and the other seven bits merge to 11111110, which is 254, so we create an array of all 254. Through it to obtain the high 7 bit plane, and then with the digital watermark image can be merged.
Similarly, to extract the digital watermark from the image, we only need to obtain the least significant bit plane, and then change its value of 1 to 255 to display the original watermark.
After running, the effect is shown as follows: