像素操作就是更改图片的像素值
代码如下:
#include<opencv2/opencv.hpp> #include<iostream> using namespace cv; using namespace std; int main(int argc, char** argv) { cout << "hello world!" << endl; Mat src = imread("D:/OpenCVprj/image/test.png"); namedWindow("input image", CV_WINDOW_AUTOSIZE); imshow("input image", src); //pixel opration Mat src_gray; cvtColor(src, src_gray, CV_BGR2GRAY); int rows = src_gray.rows; int cols = src_gray.cols; int channel = src.channels(); namedWindow("src_gray", CV_WINDOW_AUTOSIZE); imshow("src_gray", src_gray); Mat dst; dst.create(src.size(), src.type()); for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { if (channel == 1) { int gray = src_gray.at<uchar>(row, col); dst.at<uchar>(row, col) = 255 - gray; } else if (channel == 3) { int b = src.at<Vec3b>(row, col)[0]; int g = src.at<Vec3b>(row, col)[1]; int r = src.at<Vec3b>(row, col)[2]; dst.at<Vec3b>(row, col)[0] = 255 - b; dst.at<Vec3b>(row, col)[1] = 255 - g; dst.at<Vec3b>(row, col)[2] = 255 - r; } } } namedWindow("dst", CV_WINDOW_AUTOSIZE); imshow("dst", dst); waitKey(0); return 0; }
结果:
该操作相当于bitwise_not(image, dst);
即把每个像素点的值 作 255 - pixel;
代码如下:
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
cout << "hello world!" << endl;
Mat src = imread("D:/OpenCVprj/image/test.png");
namedWindow("src", CV_WINDOW_AUTOSIZE);
imshow("src", src);
Mat dst;
bitwise_not(src, dst);
namedWindow("dst", CV_WINDOW_AUTOSIZE);
imshow("dst", dst);
waitKey(0);
return 0;
}
结果如下:
进行其它操作可看到不同效果
结果如下:
来源:https://www.cnblogs.com/haiboxiaobai/p/11220480.html