本文主要讲解opencv对图片最基本的处理,给图片添加椒盐噪点
实例代码
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
void salt(cv::Mat &image, int n) {
for (int k = 0; k < n; k++) {
int i = rand() % image.cols;
int j = rand() % image.rows;
if (image.channels() == 1) {//灰度图
image.at<uchar>(i, j) = 255;
}
else if (image.channels() == 3) {
image.at<cv::Vec3b>(j, i)[0] = 255;
image.at<cv::Vec3b>(j, i)[1] = 0;
image.at<cv::Vec3b>(j, i)[2] = 0;
}
}
}
int main()
{
Mat image = imread("D:\\4.jpg");
salt(image, 10000);
imshow("image", image);
waitKey(0);
return 0;
效果展示
原图
处理后