#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
void salt(cv::Mat &image, int n) {
int i,j;
for (int k=0; k<n; k++) {
// rand() is the MFC random number generator
i= rand()%image.cols;
j= rand()%image.rows;
if (image.channels() == 1) { // gray-level image
image.at<uchar>(j,i)= 255;
} else if (image.channels() == 3) { // color image
image.at<cv::Vec3b>(j,i)[0]= 255;
image.at<cv::Vec3b>(j,i)[1]= 255;
image.at<cv::Vec3b>(j,i)[2]= 255;
}
}
}
int main()
{
srand(cv::getTickCount()); // init random number generator
cv::Mat image= cv::imread("boldt.jpg",0);
salt(image,3000);
cv::namedWindow("Image");
cv::imshow("Image",image);
cv::imwrite("salted.bmp",image);
cv::waitKey(5000);
#include"iostream"
using namespace std;
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
void salt(Mat & image, int n)
{
for (int i = 0; i < n; i++)
{
int a = rand() % image.cols;
int b = rand() % image.rows;
if (image.channels() == 1)
{
image.at<uchar>(a, b) = 255;
}
else if (image.channels() == 3)
{
image.at<Vec3b>(a, b)[0] = 255; //b
image.at<Vec3b>(a, b)[1] = 255;//g
image.at<Vec3b>(a, b)[2] = 255;//r
}
}
}
int main()
{
Mat image = imread("boldt.jpg");
salt(image, 1000);
namedWindow("image");
imshow("Image", image);
waitKey(3000);
return 0;
}
联系客服