1 #include <opencv2corecore.hpp> 2 #include <opencv2highguihighgui.hpp> 3 #include <opencv2imgprocimgproc.hpp> 4 #include <iostream> 5 6 7 void salt(cv::Mat& image, int n) 8 { 9 10 for(int k=0; k<n; k++) 11 { 12 13 int i = rand() % image.cols; 14 int j = rand() % image.rows; 15 16 // 灰度图像 17 if(image.channels() == 1) 18 { 19 cv::Mat_<uchar> img = image; 20 img(j,i) = 255; 21 }else{// 彩色图像 22 cv::Mat_<cv::Vec3b> img = image; 23 //image.at<cv::Vec3b>(j,i)[0] = 255; 24 //image.at<cv::Vec3b>(j,i)[1] = 255; 25 //image.at<cv::Vec3b>(j,i)[2] = 255; 26 27 img(j,i)[0] = 255; 28 img(j,i)[1] = 255; 29 img(j,i)[2] = 255; 30 } 31 32 } 33 34 } 35 36 37 int main(int argc, char** argv) 38 { 39 40 cv::Mat img = cv::imread("F:\images\boldt.jpg"); 41 cv::Mat img2 = img.clone(); 42 43 double duration; 44 duration = static_cast<double>(cv::getTickCount()); 45 46 salt(img, 3000); 47 48 duration = static_cast<double>(cv::getTickCount()) - duration; 49 duration /= cv::getTickFrequency(); 50 51 cv::namedWindow("original"); 52 cv::imshow("original", img); 53 cv::namedWindow("altered"); 54 cv::imshow("altered", img2); 55 56 std::cout << duration << std::endl; 57 58 cv::waitKey(0); 59 return 0; 60 61 }