使用DCT嵌入水印

时间:2015-05-19 13:19:37

标签: opencv watermark dct

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

  1. 阅读两张图片(我想要的水印和原始图片 在其中嵌入水印)
  2. 将2个已读取的图像调整为指定大小。(水印图像为64X64) 原始图像的512X512)
  3. 将原始大小调整后的图像分割为8X8块并将其转换为 DCT。
  4. 在每个原始图像块中嵌入水印的每个像素。
  5. 对每个块应用逆DCT。
  6. 我有这个问题,所有三个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;
    
    }
    

1 个答案:

答案 0 :(得分:-1)

@N_Kh据我所知,你的代码很快,你正在对不同的矩阵和向量块和块进行操作时,在Matrix dctImage上执行I​​MSHOW命令。