【发布时间】:2021-07-09 11:38:31
【问题描述】:
首先,我将封面图像读取为灰度图像。
coverImage = cv2.imread(coverImagePath, 0)
然后我将封面图像传递给另一个函数以创建另一个较小的图像,那个较小的图像是我的秘密图像。
def makeSecretImage(coverImage):
copyImage = coverImage.copy()
secretImage = cv2.resize(copyImage, (100,100))
return secretImage
之后,我将这两个图像传递给我的嵌入函数,以将秘密图像嵌入到封面图像中。我的意图是将秘密图像的每个像素的每一位嵌入到封面图像的LSB中,并且随机选择封面图像的LSB。这个函数的最终输出是一个隐秘图像,它看起来与封面图像完全相同,因为它只是改变了像素的 LSB。
def embedding(coverImage,secretImage, password):
indices = permute_indices(coverImage.shape, password)
for i in range (secretImage.shape[0]):
for j in range(secretImage.shape[1]):
x,y = next(indices)
#convert pixel to binary
pixels_cover = format(coverImage[x][y], '08b')
pixels_hide = format(secretImage[i][j], '08b')
binary = list(pixels_hide)
#error : keep overwriting the last bit
for dataIndex in range(0,8):
stegoImage = pixels_cover[:7] + binary[dataIndex]
dataIndex += 1
coverImage[x][y] = int(stegoImage, 2)
cv2.imwrite('StegoImage.png', coverImage)
然后读取隐秘图像并将其传递给提取函数。在这里,我得到了所选像素的最后一位并返回到像素。然后将那些像素值放回黑色图像。但是我在这里遇到了错误,因为它从隐秘图像中读取了额外的像素。
def extracting(stgimg,secretImage, password):
Swidth, Sheight = stgimg.shape
Ewidth, Eheight = secretImage.shape
newPixel = []
pixelList = []
# create 2 blank images
OriImg = np.zeros((Swidth, Sheight, 1), np.uint8)
ExtractedImg = np.zeros((Ewidth, Eheight, 1), np.uint8)
indices = permute_indices(stgimg.shape, password)
for x in range(Swidth):
for y in range(Sheight):
a, b = next(indices)
stegopixel = format(stgimg[a][b], '08b')
bitValue = stegopixel[-1]
newPixel.append(bitValue)
for i in range(0, len(newPixel),8):
pixelBit = ''.join(newPixel[i:i+8])
pixelByte = int(pixelBit, 2)
pixelList.append(pixelByte)
img = np.array(pixelList, dtype=np.uint8).reshape(secretImage.shape)
cv2.imwrite('ExtractedImage.png', ExtractedImg)
【问题讨论】:
-
Numpy 使用高度作为第一个索引,所以你想要
np.zeros((Eheight, Ewidth... -
@Reti43 抱歉,我收到此错误“无法将大小为 13650 的数组重新整形为形状 (100,100)”。这是因为在 for 循环中它读取了不需要的像素吗?
-
您的嵌入和提取功能做了两个截然不同的事情。事实上
embedding似乎有错误。您是否打算将secretImage的每个像素的每一点都隐藏在coverImage中,然后再次将其提取回来? -
是的,我想把秘密图像的每个像素的每一点都隐藏到封面图像中,然后将秘密图像提取回来。
-
@Reti43 我已经编辑了问题。
标签: python opencv steganography