1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include <cv.h> // open cv general include file
#include <highgui.h> // open cv GUI include file
#include <iostream>
#include <fstream>
#include <cstring>
#include <sstream>
#include <stdlib.h>
using namespace cv; // OpenCV API is in the C++ "cv" namespace
using namespace std;
unsigned long
djb2(unsigned char *str)
{
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
int main(int argc, char** argv )
{
Mat encodedImg; // inputobject
Mat originalImg; //original object
Mat outputImg(Size(640,480),CV_8U);
Mat invertedImg;
Mat noisecarrierImg;
std::vector<int>params;
// check that command line arguments are provided and image reads in OK
if(argc != 4)
cout<<"invalid argument"<<endl;
//prompt the user for a character string password
string password;
cout <<"Set the password:"<<endl;
cin >> password;
//hash function: transform an ascii to a 64bit integer
unsigned long b=djb2((unsigned char *)password.c_str());
//random nunmber generator seed
RNG random(b);
//generate an array structure of boolean
if((encodedImg=imread(argv[1], CV_LOAD_IMAGE_COLOR)).empty())
{
cout<<"impossible to load file"<<endl;
return 0;
}
originalImg=imread(argv[2],CV_LOAD_IMAGE_COLOR);
bool* list=new bool [3*encodedImg.rows*encodedImg.cols];
for(int a=0; a<encodedImg.rows*encodedImg.cols*3; a++)
{
list[a] = false;
}
//generate noise carrier image
noisecarrierImg=encodedImg.clone();
for (int k=0; k<3; k++)
{
for(int i=0; i<encodedImg.rows; i++)
{
for(int j=0; j<encodedImg.cols; j++)
{
noisecarrierImg.at<Vec3b>(i,j)=originalImg.at<Vec3b>(i,j)[k]+random.gaussian(15.0);
}
}
}
//select a random location in the colored carrier image
for(int i=0; i<encodedImg.rows; i++)
{
for(int j=0; j<encodedImg.cols; j++)
{
int r;
int c;
int g;
do
{
c=random(encodedImg.cols);
r=random(encodedImg.rows);
g=random(3);
}
while(list[(r*encodedImg.cols + c)*3 + g] && encodedImg.at<Vec3b>(r,c)[g] == 255 );
list[(r*encodedImg.cols + c)*3 + g]=true;
outputImg.at<uchar>(i,j)=(encodedImg.at<Vec3b>(r,c)[g] -noisecarrierImg.at<Vec3b>(r,c)[g])*255);
}
}
invertedImg=outputImg.clone();
for(int i=0; i<outputImg.rows; i++)
{
for(int j=0; j<outputImg.cols; j++)
{
if(outputImg.at<uchar>(i,j)==0)
outputImg.at<uchar>(i,j)=255;
if(outputImg.at<uchar>(i,j)==1)
outputImg.at<uchar>(i,j)=0;
}
}
imwrite(argv[3], invertedImg, params);
} |
Partager