#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argv, char** argc) {
Mat src = imread("C:/Users/AA/Desktop/myc++/1.jpg");
if (src.empty()) {
printf("图像未读取");
return -1;
}
namedWindow("input", WINDOW_AUTOSIZE);
imshow("input", src);
Mat gary;
cvtColor(src, gary, CV_BGR2GRAY);
namedWindow("gary", WINDOW_AUTOSIZE);
imshow("gary", gary);
int height = gary.rows; //灰度图的行
int width = gary.cols;
//单通道
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
int g = gary.at<uchar>(row, col); //拿到灰度图的像素值
gary.at<uchar>(row, col) = 255 - g; //修改像素值,得到反差图像
//cout <<"(row,col):"<<row<<","<<col<<"=" <<g << endl;
}
}
namedWindow("修改图像", WINDOW_AUTOSIZE);
imshow("修改图像", gary);
//彩色图像
Mat rgb;
rgb.create(src.size(), src.type());
height = src.rows;
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 g = gary.at<uchar>(row, col);
gary.at<uchar>(row, col) = 255 - g;
}
else {
int b = src.at<Vec3b>(row, col)[0];
int g = src.at<Vec3b>(row, col)[1];
int r = src.at<Vec3b>(row, col)[2];
rgb.at<Vec3b>(row, col)[0] = 255 -b;
rgb.at<Vec3b>(row, col)[1] = 255 -g;
rgb.at<Vec3b>(row, col)[2] = 255-r;
}
}
}
imshow("rgb", rgb);
waitKey(0);
return 0;
}
效果图片