1 Mat imgMat = imread("swan.jpg", IMREAD_COLOR); 2 // cvtColor(imgMat, imgMat, COLOR_BGR2GRAY); // 转为灰度图 3 Mat m; 4 imgMat(Rect(1200,1200, 1000, 800)).copyTo(m); 5 6 int n = 1000 * 800 * 0.05; // 0.5%白噪点 7 8 for(int k = 0; k < n; k++) 9 { 10 int i = QRandomGenerator::global()->bounded(m.rows); 11 int j = QRandomGenerator::global()->bounded(m.cols); 12 13 if(m.channels() == 1) 14 m.at<uchar>(i, j) = 255; 15 else 16 { 17 m.at<Vec3b>(i, j)[0] = 255; 18 m.at<Vec3b>(i, j)[1] = 255; 19 m.at<Vec3b>(i, j)[2] = 255; 20 } 21 } 22 23 imshow("!", m);