#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <opencv2/highgui.hpp>
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <fstream>
#define PIXEL_MAX 255.f
float get_float_0_1()
{
return ((float)rand())/RAND_MAX;
}
void normal(float rou, float *z1, float *z2)
{
float r = get_float_0_1();
float miu = get_float_0_1();
float tmp = sqrtf((-2)*logf(r));
float tmp1 = cosf(M_PI * miu);
float tmp2 = sinf(M_PI * miu);
*z1 = rou * tmp1 * tmp;
*z2 = rou * tmp2 * tmp;
return;
}
void update_pixel(float z1, float *out)
{
float tmp = z1 + *out;
if(tmp < 0.f)
*out = 0.f;
else if (tmp > PIXEL_MAX)
*out = PIXEL_MAX;
else
*out = tmp;
}
int main(int argc,char *argv[] )
{
float rou = atof(argv[2]);
cv::Mat srcImg = cv::imread(argv[1], cv::IMREAD_GRAYSCALE);
cv::Mat fImg;
cv::Mat dstImg;
cv::imwrite("in.bmp", srcImg);
srcImg.convertTo(fImg, CV_32F);
for(int i = 0; i < fImg.rows; i++)
{
float *pRow = fImg.ptr<float>(i);
for(int j = 0; j < fImg.cols-1; j++)
{
float z1, z2;
normal(rou, &z1, &z2);
update_pixel(z1, pRow+j);
update_pixel(z2, pRow+j+1);
}
}
fImg.convertTo(dstImg, CV_8U);
cv::imwrite("out.bmp", dstImg);
return 0;
}