1 | initial version |
A code to take a 2D mat and add noise to it:
void add_noise(Mat &mat, float scale)
{
for (int j = 0; j < mat.rows; j++)
{
for (int i = 0; i < mat.cols; i++)
{
float noise = static_cast<float>(rand() % 256);
noise /= 255.0f;
mat.at<float>(j, i) = (mat.at<float>(j, i) + noise*scale) / (1.0f + scale);
if (mat.at<float>(j, i) < 0)
mat.at<float>(j, i) = 0;
else if (mat.at<float>(j, i) > 1)
mat.at<float>(j, i) = 1;
}
}
}
2 | No.2 Revision |
A code to take a 2D mat of type CV_32FC1 and add noise to it:
void add_noise(Mat &mat, float scale)
{
for (int j = 0; j < mat.rows; j++)
{
for (int i = 0; i < mat.cols; i++)
{
float noise = static_cast<float>(rand() % 256);
noise /= 255.0f;
mat.at<float>(j, i) = (mat.at<float>(j, i) + noise*scale) / (1.0f + scale);
if (mat.at<float>(j, i) < 0)
mat.at<float>(j, i) = 0;
else if (mat.at<float>(j, i) > 1)
mat.at<float>(j, i) = 1;
}
}
}