#include "stdafx.h"
#include <opencv2\core\core.hpp>#include <opencv2\highgui\highgui.hpp>#include <iostream>using namespace std;
using namespace cv;
void AddNoise(Mat image, int numPts); // 加入雜訊
int _tmain(int argc, _TCHAR* argv[])
{char *path = "C:\\OpenCV\\images\\lena.jpg";
Mat image;
// 載入圖檔image = imread(path, CV_LOAD_IMAGE_COLOR);
// 檢查讀檔是否成功 if(! image.data ) { cout << "無法開啟或找不到圖檔" << std::endl ; return -1;}
namedWindow("original image", CV_WND_PROP_AUTOSIZE); imshow("original image", image); //------------------------------------- int numPts = 10000;AddNoise(image, numPts);
namedWindow("image with noise", CV_WND_PROP_AUTOSIZE); imshow("image with noise", image);waitKey(0);
getchar();
return 0;}
加入雜訊
void AddNoise(Mat image, int numPts)
{ int i, row, col;if(image.channels() == 3) // 彩色
{ for(i=0; i<numPts; i++) {row = rand() % image.rows;
col = rand() % image.cols;
image.at<cv::Vec3b>(row, col)[0] = 255;
image.at<cv::Vec3b>(row, col)[1] = 255;
image.at<cv::Vec3b>(row, col)[2] = 255;
}
}else // 灰階
{ for(i=0; i<numPts; i++) {row = rand() % image.rows;
col = rand() % image.cols;
image.at<cv::Vec3b>(row, col) = 255;
}
}
}
結果
全站熱搜


留言列表
