【发布时间】:2014-10-24 08:02:58
【问题描述】:
源代码:http://pastebin.com/LyBbNCm6
网络摄像头的示例输出:
stackoverflow 上的第一篇文章,希望我做得对:
这个问题与 C++/OpenCV 有关。
具体来说: 我正在尝试使用 opencv 的 randu() 和 bitwise_xor() 来制作相当于“一次性垫”的图像。我确实了解对于这个问题有更高性能的解决方案,不会遭受相同的图像退化和帧速率问题。我还考虑了视频编解码器中的有损压缩可能遇到的问题。
为代码质量道歉:
-这是概念验证代码,所以我没有做任何事情使其面向对象/抓住机会将重复的代码转换为函数(对于对可读性的影响,我提前道歉)。
-我试图准确地逐行逐节地注释各种代码块的预期目的。存在一些代码是因为尝试简化代码和更有效地利用内存但由于未知原因而失败。
-我怀疑颜色空间转换为 HSV 是不必要的,但我有这段代码是从 GPU 加速的“inRange()”类型实现中遗留下来的,旨在分割色调“红色”。
最终目标是使用未压缩的随机噪声视频作为共享密钥的对称密钥视频加密。这是我为自己设计的一个编程练习。
我已经使用过 AES(CBC) 并且正在考虑为此目的对 Mat 进行序列化,但我真的希望能够将加密图像作为视频/图像数据(未压缩)查看。
从加密最佳实践和密钥管理的角度来看,来自已建立代码库(如“OpenSSH”)的非对称密钥方法可能是一个更好的决定(但这与我现在正在练习的编程练习不同)。例如:我考虑过通过 SSH 隧道传输 libav/ffmpeg 流。或者 VPN 中的各种其他选项,以及查看 OpenSSL。
在做奇怪的事情方面: 问题: 我实际上并不介意可以在“unxor”窗口中看到的图像伪影。
问题
我的问题实际上是关于“异或”的: “xor”输出显然有偏差,表明 bitwise_xor 和 mat 类型的图像没有按照我的预期做。
如果我不得不猜测:我会说最大强度像素会在异或没有产生预期效果的情况下产生偏差? 我不确定是否可以使用颜色深度或对垫型图像进行操作来获得所需的结果(不切实际的密钥管理但作为安全的-the-prng-is-image-obfuscation)。
如果任何有 OpenCV 经验的人可以就我可以使用哪些选项给我一些指导,或者指出我忽略了什么,我将不胜感激。
提前致谢,
A.布鲁尔
源代码
#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;
}
【问题讨论】:
-
欢迎来到 StackOverflow。我不确定你到底想要什么。你想摆脱
unxor-output 中的工件吗? -
谢谢,对墙体文字感到抱歉。我正在尝试制作相当于“一次性垫”的图像。我试图使“xor”中显示的输出垫在视觉/密码上与“随机”中显示的随机噪声垫无法区分(同时如果从图像中减去随机噪声键,则保留原始数据)。如果代码工作正常,“异或”窗口不应该有带有可识别边缘的明暗区域。如果执行正确,我相信颜色直方图应该几乎是均匀的,平均亮度应该是大约。范围的 1/2。
标签: c++ opencv image-processing encryption