将图像像素的LSB添加到视频帧的像素的LSB



我正在编写一个代码,将图像隐藏在视频中。同样,我将视频分解为帧,并提取每帧像素的RGB值。我还用python编写了一个代码,提取图像的RGB值。现在,我想把图像嵌入视频中。为此,我想使用LSB方法,根据该方法,视频帧像素的LSB被图像像素的LSB。RGB像素值为二进制形式(8位(,最后三位将被替换。我对如何进行没有任何见解。此外,我需要使用一种方法,通过这种方法,我可以在加密后解密视频。

处理视频的Python代码

import numpy as np
import cv2 as cv
from numpy import binary_repr
from PIL import Image
vidcap = cv.VideoCapture("video.mp4")
if not vidcap.isOpened():
print("Cannot open")
exit()
while True:
# Capture frame-by-frame
ret, frame = vidcap.read()
# -------------------------------------------------------------> step 2 - split
# if frame is read correctly, ret is True
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
# Our operations on the frame come here
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# Display the resulting frame
cv.imshow('frame', gray)
width, height, d= frame.shape
print("reshaped...")
row=int(width*height)
newframe = frame.reshape(row,3) #2D mein change kiya
newframe_list=newframe.tolist()
# print(type(newframe_list))
all_pixels=[] #empty list
# print(newframe_list)
for i in newframe_list:
all_pixels.extend(i)
for i in all_pixels:
x=np.binary_repr(all_pixels[i], width=8)
vidpix=x[5:]
print(x)
print("image pixels")
#kiwi wali image ke pixels iterate kr rhe
def img_pix():
img = Image.open("kiwi.jpg")
pixels = img.load()  # this is not a list, nor is it list()'able
w, h = img.size
all_img_pixels = []
for m in range(w):
for n in range(h):
cpixel = pixels[m, n]
all_img_pixels.append(cpixel)
for m in range(w):
for n in range(3):
z = np.binary_repr(all_img_pixels[m][n], width=8)
imgpix = z[6:]
print(z)

img_pix()
if cv.waitKey(1) == ord('q'):
break
# When everything done, release the capture
vidcap.release()
cv.destroyAllWindows()

处理图像的代码

import numpy as np
import cv2 as cv
from PIL import Image
from numpy import binary_repr
def img_pix():
img = Image.open("kiwi.jpg")
pixels = img.load() # this is not a list, nor is it list()'able
width, height = img.size
all_img_pixels = []
for m in range(width):
for n in range(height):
cpixel = pixels[m, n]
all_img_pixels.append(cpixel)

for m in range(width):
for n in range(3):
z=np.binary_repr(all_img_pixels[m][n], width=8)
imgpix=z[5:]
print(imgpix)
img_pix()

注意事项

如果你想在视频帧的LSB中嵌入一个秘密,你需要将这些视频帧保存为无损格式,否则这些帧的像素会被轻微修改,从而破坏你的秘密。如果你要在图像中嵌入一个秘密,然后将其保存为jpeg,你会遇到同样的问题。


首先,不要加载秘密图像的像素。只需加载图像本身的字节流。例如,压缩的jpeg文件可能是100kB,或者总共约100k字节。相同的图像,可能是1000x1000,将有100万像素(每个像素的RGB为x3(。这将需要30倍以上的隐藏容量。

你还没有"提取的";秘密图像和视频像素。您只是将它们打印到控制台上。但是,如果你确实在相关的列表/数组中收集了它们,那么你可以根据需要迭代尽可能多的视频帧像素,直到你嵌入了所有的秘密比特。在每个视频帧的每个RGB像素中嵌入一个位三元组的示例如下所示:

def load_secret(fname):
with open(fname, 'rb') as f:
data = f.read()
return data
secret_bytes = load_secret("kiwi.jpg")
bits = []
# As it's assumed you'll be embedding 3 bits in each pixel,
# we'll split each byte in three triplets.
for byte in secret_bytes:
for k in range(6, -1, -3):
bits.append((byte >> k) & 0x07)
# Now start reading your video frames and count how many
# triplets you have embedded so far.
index = 0
while True:
ret, frame = vidcap.read()
if index < len(bits):
# Assuming you want to embed in each RGB pixels,
# you can embed up to `width x height x 3` triplets.
size = np.prod(frame.shape)
bit_groups = np.array(bits[index:index+size], dtype=np.uint8)
# Flatten the frame for quick embedding
frame_flat = frame.flatten()
# Embed as many bit groups as necessary
frame_flat[:len(bit_groups)] = (frame_flat[:len(bit_groups)] & 0b11111000) | bit_groups
# Reshape it back
new_frame = np.reshape(frame_flat, frame.shape)
index += size
else:
new_frame = frame
# You can now write `new_frame` to a new video.

提取秘密是在每个视频帧的所有像素上迭代的过程,提取3个LSB并将三个三元组拼接成一个字节。

bits = []
while True:
ret, frame = vidcap.read()
flat_frame = frame.flatten()
bits.extend(flat_frame & 0x07)
# You need to decide how many triplets is enough to extract
bytestream = b''
for i in range(0, len(bits), 3):
bytestream += bytes([(bits[i] << 6) | (bits[i+1] << 3) | bits[i+2]])
# `bytestream` should now be equal to `secret_bytes`
with open('extracted_kiwi.jpg', 'wb') as f:
f.write(bytestream)

相关内容

  • 没有找到相关文章

最新更新