我试图google很多,但找不到任何C ++代码来生成带有泊松和斑点噪声的噪声图像。虽然,我在python中得到了一个代码并试图将其转换为C ++。 https://stackoverflow.com/a/30624520/3465857 请看一下,我不确定它是否正确完成。
Mat& addPoissonNoiseIntoImage(Mat& src){
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
static Mat noisyImg = src.clone();
std::default_random_engine generator (seed);
std::vector<float> uniqueValsOfMat = BasicAlgo::getInstance()->uniqueValuesInMat(src);
unsigned long int val = uniqueValsOfMat.size();
float vals = pow(2, (ceil(log2(val))));
for(int i = 0; i < src.rows; i++)
{
for(int j = 0; j < src.cols; j++){
float pixelVal = (double)(src.at<uchar>(i,j));
pixelVal = pixelVal * vals;
std::poisson_distribution<int> distribution (pixelVal);
float generatedVal = distribution(generator) / float(vals);
noisyImg.at<uchar>(i,j) = generatedVal;
}
}
return noisyImg;
}
Mat& addSpeckleNoiseIntoImage(Mat& src, float mean, float std ){
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
static Mat noisyImg = src.clone();
std::default_random_engine generator (seed);
std::normal_distribution<double> distribution (mean,std);
for(int i = 0; i < src.rows; i++)
{
for(int j = 0; j < src.cols; j++){
float pixelVal = (double)(src.at<uchar>(i,j));
while(true){
float vals = distribution(generator);
pixelVal = pixelVal + pixelVal * vals;
if( (pixelVal > 0) && (pixelVal < 256) )
break; // you got the right pixel value, so break
}
noisyImg.at<uchar>(i,j) = pixelVal;
}
}
return noisyImg;
}
用于在Mat中找到唯一值:
std::vector<float> uniqueValuesInMat(const cv::Mat& rawData, bool sort = false)
{
Mat input;
rawData.convertTo(input, CV_32F);
if (input.channels() > 1 || input.type() != CV_32F)
{
std::cerr << "unique !!! Only works with CV_32F 1-channel Mat" << std::endl;
return std::vector<float>();
}
std::vector<float> out;
for (int y = 0; y < input.rows; ++y)
{
const float* row_ptr = input.ptr<float>(y);
for (int x = 0; x < input.cols; ++x)
{
float value = row_ptr[x];
if ( std::find(out.begin(), out.end(), value) == out.end() )
out.push_back(value);
}
}
if (sort)
std::sort(out.begin(), out.end());
return out;
}
我不确定是否正确地生成了泊松和斑点噪声的代码。请看看。