首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何通过在封面图像中随机选择像素来将图像嵌入到封面图像中?

如何通过在封面图像中随机选择像素来将图像嵌入到封面图像中?
EN

Stack Overflow用户
提问于 2021-03-31 01:20:57
回答 1查看 48关注 0票数 0
代码语言:javascript
复制
def embedding(hideImagePath,coverImagePath):
    img1 = cv2.imread(coverImagePath, 0)
    img2 = cv2.imread(hideImagePath, 0)
    for i in range (img2.shape[0]):
        for j in range(img2.shape[1]):
                #convert pixel to binary 
                pixels_cover = format(img1[i][j], '08b')
                pixels_hide = format(img2[i][j], '08b')
                #replace the last 2 LSB from cover image with 2 MSB from hide image
                stegoImage = pixels_cover[:6] + pixels_hide[:2]
                img1[i][j] = int(stegoImage, 2)
    cv2.imwrite('StegoImage.png', img1)

上面是我到目前为止所做的代码。它按顺序隐藏封面图像中的像素,但我需要它通过从封面图像中选择随机像素来将图像隐藏到封面图像。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-31 03:38:46

从概念上讲,您希望生成所有像素坐标,并以确定的方式对它们进行混洗,以便提取知道在哪里查找。

代码语言:javascript
复制
import itertools as it
import random

def permute_indices(shape, password):
    indices = list(it.product(*map(range, shape)))
    random.seed(password)
    random.shuffle(indices)
    return iter(indices)

例如

代码语言:javascript
复制
>>> idx = permute_indices((600, 800), 'hunter2')
>>> for _ in range(5):
    print(next(idx))

    
(411, 359)
(379, 680)
(110, 612)
(246, 388)
(232, 605)

对代码的调整

代码语言:javascript
复制
def embedding(hideImagePath, coverImagePath, password):
    img1 = cv2.imread(coverImagePath, 0)
    img2 = cv2.imread(hideImagePath, 0)
    indices = permute_indices(img1.shape, password)
    for i in range (img2.shape[0]):
        for j in range(img2.shape[1]):
                x, y = next(indices)
                #convert pixel to binary
                pixels_cover = format(img1[x][y], '08b')
                pixels_hide = format(img2[i][j], '08b')
                #replace the last 2 LSB from cover image with 2 MSB from hide image
                stegoImage = pixels_cover[:6] + pixels_hide[:2]
                img1[x][y] = int(stegoImage, 2)
    cv2.imwrite('StegoImage.png', img1)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66875573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档