Logistic map algorithm for image scrambling
hello , i have written code for image scrambling using logistic map. the code is given below. Similar algortihm works well in MATLAB but in OPENCV i am getting original image as it is. not a scrambled one. How to resolve the problem?
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\core\mat.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include<iostream>
#include<math.h>
#include<conio.h>
using namespace std;
using namespace cv;
class image
{
public:
Mat im,im1,im2,cs,im_enc;
int getim();
};
int image::getim()
{
im=imread("logo1.jpg",0);
if (im.empty())
{
cout << "Error : Image cannot be loaded..!!" << endl;
return -1;
}
im1=Mat::zeros(im.rows,im.cols,CV_8U);
threshold(im,im1,128,255,1);
cs=Mat::zeros(im.rows,im.cols,CV_8U);
im_enc=Mat::zeros(im.rows,im.cols,CV_8U);
float x_new,x_old=0.7;
for(int rc=1;rc<im1.rows+1;rc++)
{
for(int cc=1;cc<im1.cols+1;cc++)
{
for(int iter=1;iter<rc*cc+1;iter++)
{
x_new=x_old*4*(1-x_old); //Logistic map
x_old=x_new;
}
if(x_old>=0.5)
cs.at<uchar>(rc,cc)=255;
else
cs.at<uchar>(rc,cc)=0;
}
}
bitwise_xor(im1,cs,im_enc,noArray());
waitKey(4000);
cout<<""<<im_enc;
namedWindow("Scrambled Image",CV_WINDOW_AUTOSIZE);
imshow("Scrambled Image",im_enc);
waitKey(0);
return 0;
}
int main()
{
image my;
my.getim();
return 0;
}
it scrambles nicely here.
btw, rc<im1.rows+1 will overflow. (consider running a debug build, to get proper exceptions thrown)
why not start your iterations at 0 [which is standard everywhere besides matlab ...] and run to the length of the image. That will remove the error presented to you by @berak and make sure you will not run into buffer overflow problems ...
oohh, right. did not even see it was starting with 1. ;)
It is a classical mistake when people switch code from matlab to opencv! I even have this in the
things not to forget section
for my students :PThanks a lot Berak sir and Steven Sir for useful comments..
@Mahavir, just out of curiosity, what are you doing with the output ?
One guess could be he is making a scrambler-descrambler setup to make sure noone without the tool can see the true content of the image.
poor man's encryption ;)
@ berak Sir, i wanted to do scrambling of watermark b4 insertion in host image.