我如何使用python将列表放入大小为283 * 283的2d数组中

问题描述 投票:0回答:1

我想使用LSB(最低有效位)将字符串隐藏(不可见水印)到图像(283 * 283)中算法。用户给出隐藏的消息(字符串),然后将所有字符的ascii代码(以2为底)放入列表中,现在我想将此列表作为2d数组,其大小与我的图片相同,然后可以使用“&”和“ |” '运算符。

import cv2 as cv

#read image:

img=cv.imread('C:/Users/pc/Desktop/cameraman.jpg',0)
cv.imshow("ax bedoon ramz",img)
cv.waitKey()

#make least significant bit of each pixel 0 :

img_r=img&0b11111110
img_w=img_r.copy()

#take message and make sure it can hide in 283*283 image :

while True:
    txt=input('chi maikhay ghayem koni ? (max = 10000 character) : ')
    if len(txt)>10000:
        print('out of range characters ! ! ! ')
    else :
        break

#put characters ascii code in list :

ch_ascii_base2 = [bin(ord(i))[2:] for i in txt]

result=[]
for ch in ch_ascii_base2:
    for val in ch:
        result.append(bin(int(val))[2:])
python opencv image-processing steganography
1个回答
0
投票

将所有像素的所有LSB归零是没有意义的,因为如果您的秘密比图像的大小小得多,那么您就无缘无故地修改了约50%的剩余像素。

我只是获得消息的比特流,将图像展平,然后将消息隐藏在适合该消息的那个数组的切片中。然后将其重塑回2D。

string = 'Hello world'

# Getting the bits from each character with bitwise operations is better
# than using intermediate strings with `bin` or string formats
for byte in map(ord, string):
    bits.extend((byte >> i) & 1 for i in range(7, -1, -1))

flat = img.flatten()
flat[:len(bits)] = (flat[:len(bits)] & 0xfe) | bits
stego = flat.reshape(img.shape)

如果图像为RGB,则像素的顺序为(0,0,R),(0,0,G),(0,0,B),(0,1,R)等。如果您想先将秘密嵌入到例如蓝色通道中,然后提取该颜色平面,并通过上述过程将尽可能多的位嵌入其中,然后再移至另一个通道。这有点令人费解,但并不是很难。

如果您坚持将位流转换为与图像大小相同的2D数组,只需计算图像有多少像素,有多少位,然后在位流中添加那么多1或0。然后使用np.reshape()。同样,如果结果是3D数组,则必须注意位的最终顺序。

总而言之,如果您不希望将机密嵌入特定平面中,请使用我建议的方法。它非常简短明了,不涉及任何多余的计算或图像修改。

© www.soinside.com 2019 - 2024. All rights reserved.