Image information hiding and decryption (OpenCV)

Table of contents

1. The hidden meaning of the image:

Second, the principle of image hiding:

3. Sample pictures (note: the resolution of the two pictures must be the same, and there will be no error when running the following code):

4. The process of hiding information:

1) Read the source image (the information to be hidden in the text will be written) and the carrier image, and construct the image matrix.

2) Add watermark text to the source image as the text to be hidden.

3) Process the hidden carrier map, and turn all blue values ​​into even numbers, so as to add hidden information.

4) Read the source image, set the blue pixel value of the text pixel of the source image in the corresponding position of the carrier file to an odd number, and write the information to be hidden into the target carrier image.

5) Save the modified target vector map.

5. All codes for hiding information:

6. Image information hiding operation effect diagram:

 7. The process of decrypting information:

1) Read the carrier file and its size information.

2) Generate a blank image matrix in order to draw the decrypted text.

3) Draw the decrypted watermark text. If the blue value is odd, the pixel is text.

4) Show hidden information.

8. All codes for decrypting information:

Nine, information decryption operation effect diagram:

Ten, Note:


1. The hidden meaning of the image:

Information hiding is to prevent anyone other than the intended recipient from knowing the transmission event or the content of the information. The larger the size of the file relative to the hidden file, the easier it is to hide the latter. Therefore, digital images on the Internet and other media are widely used to hide information.

Second, the principle of image hiding:

First, extract the text image information from the source image, and record the position of the text image information pixel in the image matrix; then, preprocess the carrier file, and set all the blue pixel values ​​to even numbers; finally, set the recorded text The blue pixel value of the information pixel at the corresponding position of the carrier file is set to an odd number. Decrypting information is the reverse process of hiding information, and the process is relatively simple, that is, extracting pixels with odd blue pixel values ​​in the carrier file, and assigning uniform coloring to the positions corresponding to these pixels in the blank image.

3. Sample pictures ( note: the resolution of the two pictures must be the same, and there will be no error when running the following code ):

 

4. The process of hiding information:

1) Read the source image (the information to be hidden in the text will be written) and the carrier image, and construct the image matrix.

    img1 = cv2.imread(fn1)
    img2 = cv2.imread(fn2)
    w = img1.shape[1]
    h = img1.shape[0]

2) Add watermark text to the source image as the text to be hidden.

    # Add the information that needs to be hidden
    cv2.putText(img1,"Welcom to see my articles", (100,300), cv2.FONT_HERSHEY_PLAIN, 3.0, redcolor, thickness = 2) # The
    position of the text is at coordinates (100,300), using the font cv2 .FONT_HERSHEY_PLAIN with a font size of 3.0, a color of red, and a line weight of 2.
    cv2.putText(img1,"Would you like to be thumb up for my articles?", (200,500), cv2.FONT_HERSHEY_PLAIN, 2.0, redcolor, thickness = 2) cv2.putText(img1,"Please follow me if you like
    it !", (600,800), cv2.FONT_HERSHEY_PLAIN, 2, redcolor, thickness = 1)

3) Process the hidden carrier map, and turn all blue values ​​into even numbers, so as to add hidden information.

    #Process hidden carrier map
    # Turn all blue values ​​into even numbers
    for j in range(h):
        for i in range(w):
            if (img2[j,i,0]%2) == 1:
                img2[j ,i,0] = img2[j,i,0]-1

4) Read the source image, set the blue pixel value of the text pixel of the source image in the corresponding position of the carrier file to an odd number, and write the information to be hidden into the target carrier image.

    # Read the source image, write the information into the target image, and display
    for j in range(h):
        for i in range(w):
            if (img1[j,i,0], img1[j,i,1 ], img1[j,i,2]) == redcolor:
                img2[j,i,0] = img2[j,i,0]+1

5) Save the modified target vector map.

    cv2.imshow('img2-2', img2)
    cv2.imwrite(fn3, img2)

5. All codes for hiding information:

import cv2
import numpy as np
# 含有文字的图像
fn1 = r"C:\Users\LIHAO\Pictures\Saved Pictures\wallhaven-5g5le9.png"
# 载体文件
fn2 = r"C:\Users\LIHAO\Pictures\Saved Pictures\wallhaven-l8w95q.png"
# 包含隐藏信息的载体文件
fn3 = "secret.png"
redcolor = (0,0,255)
if __name__ == "__main__":
    # 图像大小
    img1 = cv2.imread(fn1)
    img2 = cv2.imread(fn2)
    w = img1.shape[1]
    h = img1.shape[0]
    # 加上需要隐藏的信息
    cv2.putText(img1,"Welcom to see my articles", (100,300), cv2.FONT_HERSHEY_PLAIN, 3.0, redcolor, thickness = 2)
    # 文本的位置在坐标(100,300),使用字体cv2.FONT_HERSHEY_PLAIN,字体大小为3.0,颜色为红色,线条粗细为2。
    cv2.putText(img1,"Would you like to be thumb up for my articles?", (200,500), cv2.FONT_HERSHEY_PLAIN, 2.0, redcolor, thickness = 2)
    cv2.putText(img1,"Please follow me if you like it!", (600,800), cv2.FONT_HERSHEY_PLAIN, 2, redcolor, thickness = 1)
    
    cv2.namedWindow('img1')
    cv2.imshow('img1', img1)
    cv2.namedWindow('img2-1')
    cv2.imshow('img2-1', img2)
    # 处理隐藏载体图
    # 将所有蓝色值变成偶数
    for j in range(h):
        for i in range(w):
            if (img2[j,i,0]%2) == 1:
                img2[j,i,0] = img2[j,i,0]-1
        print('.........正在运行..................')
        mirror_w = w/2
    # 读取源图,并将信息写入目标图,并显示
    for j in range(h):
        for i in range(w):
            if (img1[j,i,0], img1[j,i,1], img1[j,i,2]) == redcolor:
                img2[j,i,0] = img2[j,i,0]+1
        print('##########正在运行#################')
    # 保存修改后的目标图,并显示
    cv2.namedWindow('img2-2')
    cv2.imshow('img2-2', img2)
    cv2.imwrite(fn3, img2)
    cv2.waitKey()
    cv2.destroyAllWindows()

6. Image information hiding operation effect diagram:

 

        After running the above code to hide the information, observe the carrier image (img2-2) with naked eyes, and still cannot detect any changes compared with the previous (img2-1).

 7. The process of decrypting information:

        Decrypting information is the opposite of hiding information, it is the reverse process of hiding information.

1) Read the carrier file and its size information.

    img = cv2.imread(fn)
    w = img.shape[1]
    h = img.shape[0]

2) Generate a blank image matrix in order to draw the decrypted text.

    imginfo = np.zeros((h,w,3), np.uint8)

3) Draw the decrypted watermark text. If the blue value is odd, the pixel is text.

    for j in range(h):
        for i in range(w):
            if (img[j,i,0]%2) == 1:
                # If the blue value is odd, then the pixel is the text
                imginfo[j ,i,1] = 255

4) Show hidden information.

    cv2.imshow('info', imginfo)
    cv2.imwrite(fn, imginfo)

8. All codes for decrypting information:

import cv2
import numpy as np
fn = "secret.png"
if __name__ == '__main__':
    img = cv2.imread(fn)
    w = img.shape[1]
    h = img.shape[0]
    imginfo = np.zeros((h,w,3), np.uint8)
    for j in range(h):
        for i in range(w):
            if (img[j,i,0]%2) == 1:
                # 如果蓝色值为奇数,则该像素点为文字
                imginfo[j,i,1] = 255
        print('##############正在运行###################')
        
    cv2.imshow('info', imginfo)
    cv2.imwrite(fn, imginfo)
    cv2.waitKey()
    cv2.destroyAllWindows()

Nine, information decryption operation effect diagram:

Ten, Note:

If there is any infringement, please contact me in time to delete! ! ! 

Guess you like

Origin blog.csdn.net/weixin_51756038/article/details/130051053