知识点
获取像素点:
Mat.at<Vec3b>(row,col) --三通道//获取像素点列表由bgr三个元素组成
Mat.at<uchar>(row,col)--单通道//获取像素点值
不同通道数量组成的图像像素点值的获取有所差异
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main() {
Mat src, gray_src;
src = imread("F:/test.jpg");
imshow("raw image", src);
cvtColor(src, gray_src, CV_BGR2GRAY);
imshow("gray image",gray_src);
//获取图像的像素
Mat dst;
dst.create(src.size(), src.type());
int height = src.rows;
int width = src.cols;
int ch = src.channels();
//实现色反
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
//单通道
if (ch == 1) {
int gray = gray_src.at<uchar>(row, col);
gray_src.at<uchar>(row, col) = 255 - gray;
}
//三通道
if (ch == 3) {
/*
[b,g,r][b,g,r][b,g,r]
[b,g,r][b,g,r][b,g,r]
[b,g,r][b,g,r][b,g,r]
像素点的三个通道值类似数组存储
指针的使用[]的使用
Vec3b---vector 3 B
动态数组三个元素,描述BGR颜色的类型
*/
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;
}
}
}
imshow("dst image", dst);
waitKey(0);
return 0;
}