OpenRV Mat bitwise_xor与randu()

时间:2014-10-22 19:42:20

标签: c++ opencv image-processing encryption

源代码:http://pastebin.com/LyBbNCm6

网络摄像头的输出示例: Example Output from Webcam

关于stackoverflow的第一篇文章,所以希望我这样做是正确的:

问题与C ++ / OpenCV有关。

具体做法是: 我正在尝试使用opencv的randu()和bitwise_xor()来做相当于“一次性填充”的图像。我确实理解这个问题有更高性能的解决方案,它不会遭受相同的图像质量下降。帧率问题。我还考虑了视频编解码器中有损压缩可能遇到的问题。

代码质量道歉:

- 这是概念代码的证明,所以我没有采取任何措施使其面向对象/抓住机会将重复的代码转换为函数(我提前道歉以了解对可读性的影响)。

- 我试图逐行准确地逐段评论各种代码块的预期目的是什么。有些代码存在,因为出于未知原因,尝试简化代码并更有效地利用内存是不成功的。

- 我怀疑色彩空间转换为HSV是不必要的,但我从GPU加速“inRange()”中留下了这个代码 - 用于将色调分段为“红色”的类型实现。

最终目标是使用随机噪声的未压缩视频作为共享密钥的对称密钥视频加密。这是我为自己想出的编程练习。

我已经玩过AES(CBC)并且正在考虑为此目的序列化Mat,但我真的希望能够将加密图像视为视频/图像数据(未压缩)。

来自已建立的代码库(如“OpenSSH”)的非对称密钥方法可能是从加密最佳实践和密钥管理角度更好的决策(但这是一个不同于我现在正在练习的编程练习)。举个例子:我想过通过SSH隧道管道一个libav / ffmpeg流。或VPN内的各种其他选项,以及OpenSSL。

在做事奇怪的事情上: 问题: 我实际上并不介意可以在“unxor”窗口中看到的图像伪像。

问题

我的问题实际上是关于“xor”: “xor”输出显然有偏差,表示bitwise_xor和mat类型图像没有按照我的预期进行。

如果我不得不猜测:我会说最大强度像素会产生偏差,而xor没有产生预期的效果? 我不确定是否可以使用颜色深度进行操作或对垫型图像进行操作以获得所需的结果(不切实际的密钥管理 - 但是作为安全 - 如-the-PRNG-是-图像模糊)。

如果有OpenCV经验的人可以给我一些关于我可以选择的指导或指出我忽略的内容,我们将不胜感激。

提前致谢,

一个。布鲁尔

源代码

#include <cv.hpp> //necessary because this code uses the OpenCV library
#include "/usr/local/include/opencv2/highgui/highgui.hpp" //necessary because this code uses the OpenCV library

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
// /* for the purpose of exiting via keyboard input////////////////////////////////////////////////
bool loop = true;
Size size(320,240);//the dst image size,e.g.100x100
// /* for the purpose of exiting via keyboard input////////////////////////////////////////////////

// /* storage(memory allocation) for images and data///////////////////////////////////////////////
Mat image, random;
// */ storage for images and data//////////////////////////////////////////////////////////////////

// /* declaration of char to hold input from the keyboard buffer///////////////////////////////////
char KeyStroke;
// */ declaration of char to hold input from the keyboard buffer///////////////////////////////////

//prepare image source/////////////////////////////////////////////////////////////////////////////
VideoCapture cap;//assigning camera and proportions
cap.open(0);//starting webcam feed
//prepare image source/////////////////////////////////////////////////////////////////////////////

/* video input specific////////////////////////////////////////////////////////////////////////////
//cap.set(3, 320);
//cap.set(4, 240);
//cap.set(5,5);
//cap.set(CV_CAP_PROP_FRAME_WIDTH,320);
//cap.set(CV_CAP_PROP_FRAME_HEIGHT,240);
*/// video input specific//////////////////////////////////////////////////////////////////////////

namedWindow("original",1);//creating window for unmodified image
namedWindow("random",1);//creating window for "random noise"
namedWindow("xor",1);//creating window for obfuscated output
namedWindow("unxor",1);//creating window for deobfuscated image



while(loop == true)
{

try
{

// /* //read "image" from webcam or video//////////////////////////////////////////////////////////
        cap>>image;
// */ //read "image" from webcam or video//////////////////////////////////////////////////////////

// /* //generate Mat full of random noise//////////////////////////////////////////////////////////
        random = Mat(240, 320, CV_8UC3);
        randu(random, Scalar::all(0), Scalar::all(255));
// */ //generate Mat full of random noise//////////////////////////////////////////////////////////

// /* //resize image///////////////////////////////////////////////////////////////////////////////
resize(image,image,size);//resize image
// */ //resize image///////////////////////////////////////////////////////////////////////////////

// /* //recycled code//////////////////////////////////////////////////////////////////////////////
KeyStroke = cvWaitKey(100); //get keyboard input

        if(KeyStroke==' '){
            break;
        }
// */ //recycled code//////////////////////////////////////////////////////////////////////////////

// /* //placeholder creation///////////////////////////////////////////////////////////////////////
Mat imageT, randomT, image2; //placeholders to preserve "image" and "random" in their original conditions for comparison
vector<Mat> channels; //bitwise_xor placeholder for "imageT"
vector<Mat> channels2; //bitwise_xor placeholder for "randomT"
vector<Mat> channels3; //bitwise_xor placeholder for "image2"
// */ //placeholder creation///////////////////////////////////////////////////////////////////////

// /* //prepare the input Mat(s) for bitwise_xor processing////////////////////////////////////////
cvtColor(image, imageT, CV_BGR2HSV);//cvtColor conversion of "image"(type: Mat) to HSV colorspace "imageT"(type: Mat)
cvtColor(random, randomT, CV_BGR2HSV);//cvtColor conversion of "random"(type: Mat) to HSV colorspace "randomT"(type: Mat)
cvtColor(image, image2, CV_BGR2HSV);//cvtColor conversion of "image"(type: Mat) to HSV colorspace "image2"(type: Mat)
// */ //prepare the input Mat(s) for bitwise_xor processing////////////////////////////////////////

// /* //prepare the necessary vector<mat> for adding the "random noise" to the output//////////////
split(imageT, channels);
Mat HueI(channels[0]); //Create "Hue" channel
Mat SatI(channels[1]); //Create "Sat" channel
Mat VeeI(channels[2]); //Create "Vee" channel
// */ //prepare the necessary vector<mat> for adding the "random noise" to the output//////////////


// /* //prepare the necessary vector<mat> for adding the "random noise" to the output//////////////
split(randomT, channels2);
Mat HueR(channels2[0]); //Create "Hue" channel
Mat SatR(channels2[1]); //Create "Sat" channel
Mat VeeR(channels2[2]); //Create "Vee" channel
// */ //prepare the necessary vector<mat> for adding the "random noise" to the output//////////////


// /* //prepare the necessary vector<mat> for holding the output from xor//////////////////////////
split(image2, channels3);
Mat Hue2(channels3[0]); //Create "Hue" channel
Mat Sat2(channels3[1]); //Create "Sat" channel
Mat Vee2(channels3[2]); //Create "Vee" channel
// */ //prepare the necessary vector<mat> for holding the output from xor//////////////////////////

// /* //xor "random noise" with the input mat to obfuscate the image from its original appearance//
bitwise_xor(HueI, HueR, Hue2);//xor "HueI"(type: Mat) from "channels[0]"(type: vector<Mat>)<-[composed of "imageT" split() output] with "HueR"(type: Mat)<-from "channels2[0]"(type: vector<Mat>)<-[composed of "randomT" split() output]
bitwise_xor(SatI, SatR, Sat2);//xor "SatI"(type: Mat) from "channels[0]"(type: vector<Mat>)<-[composed of "imageT" split() output] with "SatR"(type: Mat)<-from "channels2[1]"(type: vector<Mat>)<-[composed of "randomT" split() output]
bitwise_xor(VeeI, VeeR, Vee2);//xor "VeeI"(type: Mat) from "channels[0]"(type: vector<Mat>)<-[composed of "imageT" split() output] with "VeeR"(type: Mat)<-from "channels2[2]"(type: vector<Mat>)<-[composed of "randomT" split() output]
// */ //xor "random noise" with the input mat to obfuscate the image from its original appearance//

// /* //show the obfuscated output in window "xor"/////////////////////////////////////////////////
merge(channels3, image2);
cvtColor(image2, image2, CV_HSV2BGR);//GPU namespace cvtColor conversion of "oclImage"(type: Mat) to HSV colorspace "oclSrc_hsv"(type: Mat)
imshow("xor",image2);//show the obfuscated output in window "xor"
// */ //show the obfuscated output in window "xor"/////////////////////////////////////////////////

// /* //prepare the obfuscated output for removal of the "random noise"////////////////////////////
cvtColor(image2, image2, CV_BGR2HSV);//cvtColor conversion of "image2"(type: Mat) to HSV colorspace "image2"(type: Mat)
// */ //prepare the obfuscated output for removal of the "random noise"////////////////////////////

// /* //prepare the necessary vector<mat> for removing the "random noise" from the output//////////
split(image2, channels3);

Mat Hue3(channels3[0]); //Create "Hue" channel
Mat Sat3(channels3[1]); //Create "Sat" channel
Mat Vee3(channels3[2]); //Create "Vee" channel
// */ //prepare the necessary vector<mat> for removing the "random noise" from the output//////////

// /* //xor same "random noise" with the output mat to return the image to its original appearance/
bitwise_xor(Hue3, HueR, Hue3);//xor "Hue3"(type: Mat) from "channels3[0]"(type: vector<Mat>)<-[composed of "image2" split() output] with "HueR"(type: Mat)<-from "channels2[0]"(type: vector<Mat>)<-[composed of "randomT" split() output]

bitwise_xor(Sat3, SatR, Sat3);//xor "Sat3"(type: Mat) from "channels3[1]"(type: vector<Mat>)<-[composed of "image2" split() output] with "SatR"(type: Mat)<-from "channels2[1]"(type: vector<Mat>)<-[composed of "randomT" split() output]

bitwise_xor(Vee3, VeeR, Vee3);//xor "Vee3"(type: Mat) from "channels3[2]"(type: vector<Mat>)<-[composed of "image2" split() output] with "VeeR"(type: Mat)<-from "channels2[1]"(type: vector<Mat>)<-[composed of "randomT" split() output]
// /* //xor same "random noise" with the output mat to return the image to its original appearance/

// /* //show the deobfuscated output in window "unxor"/////////////////////////////////////////////
merge(channels3, image2); //recombine the 3x HSV Channels of "channel3"(type: vector<Mat>) into "image2"(type: Mat)
cvtColor(image2, image2, CV_HSV2BGR);//cvtColor conversion of "image2"(type: Mat) to BGR colorspace "image2"(type: Mat)
imshow("unxor",image2);//normal window show webcam or video
// */ //show the deobfuscated output in window "unxor"/////////////////////////////////////////////

// /* //show the original input in window "original" and "random noise" in window "random"/////////
cvtColor(imageT, image, CV_HSV2BGR);//cvtColor conversion of "randomT"(type: Mat) to BGR colorspace "random"(type: Mat)
cvtColor(randomT, random, CV_HSV2BGR);//cvtColor conversion of "randomT"(type: Mat) to BGR colorspace "random"(type: Mat)
imshow("original",image);//normal window show webcam or video
imshow("random",random);//normal window show webcam or video
// /* //show the original input in window "original" and "random noise" in window "random"/////////
}

// /* //recycled code//////////////////////////////////////////////////////////////////////////////
catch (Exception& e)//error checking
{
    const char* err_msg = e.what();
    std::cout << "exception caught: imshow:\n" << err_msg << std::endl;
}

char key = waitKey(33) & 0xFF;//checking for key press
if (key == 'q')//press q to quit
        {
                //cap.release();
                image.release();
                loop = false;
        }
// */ //recycled code///////////////////////////////////////////////////////////////////
}
return 0;
}

1 个答案:

答案 0 :(得分:1)

http://imgur.com/6dI7nLd

我想我现在有了一个有效的解决方案:

#include <cv.hpp>
#include "/usr/local/include/opencv2/highgui/highgui.hpp"

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{

bool loop = true;
Size size(320,240);

Mat image, image0, random;

char KeyStroke;

VideoCapture cap;
cap.open(0);

namedWindow("original",1);
namedWindow("random",1);
namedWindow("xor",1);
namedWindow("unxor",1);

while(loop == true)
{

try
{
cap>>image0;

random = Mat(240,320, CV_8UC3); //source: http://docs.opencv.org/doc/tutorials/core/mat_the_basic_image_container/mat_the_basic_image_container.html
randu(random, Scalar::all(0), Scalar::all(255)); //source: http://docs.opencv.org/doc/tutorials/core/mat_the_basic_image_container/mat_the_basic_image_container.html
imshow("random",random);

KeyStroke = cvWaitKey(100);

if(KeyStroke==' ')
{
  break;
}

vector<Mat> channels0;
vector<Mat> channels1;
vector<Mat> channels2;
vector<Mat> channels3;

Mat imageT, imageD;
imageT = Mat::zeros(240,320, CV_8UC3);
imageD = Mat::zeros(240,320, CV_8UC3);
resize(image0,image0,size,CV_INTER_NN);

image = image0;

split(image, channels0);
Mat BlueI(channels0[0]);
Mat GreenI(channels0[1]);
Mat RedI(channels0[2]);

split(random, channels1);
Mat BlueR(channels1[0]);
Mat GreenR(channels1[1]);
Mat RedR(channels1[2]);

split(imageT, channels2);
Mat Blue2(channels2[0]);
Mat Green2(channels2[1]);
Mat Red2(channels2[2]);

split(imageD, channels3);
Mat Blue3(channels3[0]);
Mat Green3(channels3[1]);
Mat Red3(channels3[2]);

bitwise_xor(BlueI, BlueR, Blue2);
bitwise_xor(GreenI, GreenR, Green2);
bitwise_xor(RedI, RedR, Red2);
merge(channels2, imageT);

bitwise_xor(Blue2, BlueR, Blue3);
bitwise_xor(Green2, GreenR, Green3);
bitwise_xor(Red2, RedR, Red3);
merge(channels3, imageD);

merge(channels2, imageT);
merge(channels1, random);
merge(channels0, image);

imshow("original",image0);
resize(imageT,imageT,size, CV_INTER_NN);
imshow("xor",imageT);
resize(imageD,imageD,size, CV_INTER_NN);
imshow("unxor",imageD);

}
catch (Exception& e)
{
    const char* err_msg = e.what();
    std::cout << "exception caught: imshow:\n" << err_msg << std::endl;
}

char key = waitKey(33) & 0xFF;
if (key == 'q')
        {
                image.release();
                loop = false;
        }
}
return 0;
}

我认为这是导致问题的HSV色彩空间。一旦我切换回使用BGR,一切都开始正常。当我对随机噪声Mat进行可视化时,我发现了一些可疑的东西,并注意到3个通道中的2个没有按预期运行。 http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html