【问题标题】:howcan i put a list into 2d array with known size 283*283 using python我如何使用 python 将列表放入已知大小为 283*283 的二维数组中
【发布时间】:2020-02-16 16:41:47
【问题描述】:

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

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】:

将所有像素的所有 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 数组,则必须注意位的最终顺序。

总而言之,如果你不介意将秘密嵌入特定位面,请使用我建议的方法。它非常简短明了,不涉及任何无关的计算或对图像的修改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    相关资源
    最近更新 更多