gpt4 book ai didi

opencv - 使用 DCT 嵌入水印

转载 作者:太空宇宙 更新时间:2023-11-03 21:55:25 37 4
gpt4 key购买 nike

我写下了一个openCV代码。我试图在512X512的图像中嵌入一个64X64像素的水印图像。我的代码有 5 个部分:

  1. 读两张图片(水印和我想要的原图嵌入水印)
  2. 将 2 个读取的图像调整为指定大小。(水印图像为 64X64和 512X512 原始图像)
  3. 将调整后的原始图像分成 8X8 block ,并使用DCT。
  4. 将水印的每个像素嵌入到原始图像的每个 block 中。
  5. 在每个 block 上应用逆 DCT。

我遇到的问题是所有三个 imshows 的结果都相同。谢谢你的帮助:)

这是我的代码:

int _tmain(int argc, _TCHAR* argv[])
{

int index=0;
int iindex=0;
vector<Mat> blocks(4096);


/////////////Part1:reading images


Mat originalImage;
originalImage = imread("C:\\MGC.jpg",CV_LOAD_IMAGE_GRAYSCALE);
Mat watermarkImage;
watermarkImage = imread("C:\\ivp_lg.bmp" , CV_LOAD_IMAGE_GRAYSCALE);

/// show original image
namedWindow("Original");
int x = 0; int y = 0;
moveWindow("Original", x, y);
imshow("Original", originalImage);
x += 100; y += 100;


//////Part 2:Leave originals alone, work on a copys. resize readed images


Mat dctImage = originalImage.clone();
Mat wmrk = watermarkImage.clone();
Mat tmp1(512, 512, CV_8UC1);
Mat tmp2(64, 64, CV_8UC1);
resize(dctImage, dctImage, tmp1.size());
resize(wmrk, wmrk , tmp2.size());


/////Part 3:break dctImage into 8X8 blocks and applying DCT on each block


for (int i = 0; i < 512; i += 8)
{
for (int j = 0; j < 512; j+= 8)
{
Mat block = dctImage(Rect(i, j, 8, 8));
block.convertTo(block, CV_32FC1);
dct(block,blocks[index]);
blocks[index].convertTo(blocks[index], CV_8UC1);
index++;

}
}

/// show transformed image
namedWindow("TransformedImage");
moveWindow("TransformedImage", x, y);
imshow("TransformedImage",dctImage );
x += 100; y += 100;


//////Part 4: embeding watermark. if corresponding pixel of watermark was 255 then element (5,5) in the block increase 200 otherwise do nothing


for(int idx=0 ; idx<4096 ; idx++)
{
int i=idx/64;
int j=idx%64;
float elem=(float) wmrk.at<uchar>(i,j);
if (elem>=128)
{

float tmp=(float) blocks[idx].at<uchar>(5,5);
float temp=tmp +200;
uchar ch=(uchar) temp;
blocks[idx].at<uchar>(5,5)=ch;
}
}


//////Part 5:applying iDCT on each block


for (int i = 0; i < 512; i += 8)
{
for (int j = 0; j < 512; j+= 8)
{
Mat block = dctImage(Rect(i, j, 8, 8));
block.convertTo(block, CV_32FC1);
idct(block,blocks[iindex]);
blocks[iindex].convertTo(blocks[iindex], CV_8UC1);
iindex++;

}
}

/// show watermarked image
namedWindow("WatermarkedImage");
moveWindow("WatermarkedImage", x, y);
imshow("WatermarkedImage",dctImage );



cvWaitKey(80000);

destroyAllWindows();

return 0;

}

最佳答案

@N_Kh 就我匆忙看到你的代码而言,你正在对矩阵 dctImage 执行 IMSHOW 命令,同时你分别对不同的矩阵和向量 block 和 block 执行操作。

关于opencv - 使用 DCT 嵌入水印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30326848/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com