Skip to content

Commit d6a6ddc

Browse files
committed
Update ImageDataInceease.cpp
1 parent 0a48098 commit d6a6ddc

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

ImageDataIncrease.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,20 @@ Mat AddSaltNoise(const Mat &img, double SNR) {
7878
int NP = SP*(1 - SNR); //获得需要添加椒盐噪声的像素个数
7979
dst = img.clone();
8080
for (int i = 0; i < NP; i++) {
81-
int x = (int)(rand()*1.0 / RAND_MAX * (double)img.rows);
82-
int y = (int)(rand()*1.0 / RAND_MAX * (double)img.cols);
83-
int r = rand() % 2;
81+
int x = (int)(abs(rand()*1.0) / RAND_MAX * (double)img.rows);
82+
int y = (int)(abs(rand()*1.0) / RAND_MAX * (double)img.cols);
83+
if (x >= img.rows) x = img.rows - 1;
84+
if (y >= img.cols) y = img.cols - 1;
85+
int r = abs(rand()) % 2;
8486
if (r) {
85-
dst.at<uchar>(x, y) = 0;
87+
dst.at<Vec3b>(x, y)[0] = 0;
88+
dst.at<Vec3b>(x, y)[1] = 0;
89+
dst.at<Vec3b>(x, y)[2] = 0;
8690
}
8791
else {
88-
dst.at<uchar>(x, y) = 255;
92+
dst.at<Vec3b>(x, y)[0] = 255;
93+
dst.at<Vec3b>(x, y)[1] = 255;
94+
dst.at<Vec3b>(x, y)[2] = 255;
8995
}
9096
}
9197
return dst;

0 commit comments

Comments
 (0)