#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src = imread("1.jpg");
if (src.empty())
{
cout << "could not load image" << endl;
return -1;
}
namedWindow("input", CV_WINDOW_AUTOSIZE);
imshow("input", src);
Mat gray;
cvtColor(src, gray, CV_BGR2GRAY);
int rows = gray.rows;
int cols = gray.cols;
Mat dst = Mat(src.size(), src.type());
int nc = src.channels();
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
if (nc == 1)
{
int gray_pixel = gray.at<uchar>(row, col);
gray.at<uchar>(row, col) = 255 - gray_pixel;
}
else if (nc == 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] = b;
dst.at<Vec3b>(row, col)[1] = 0;
dst.at<Vec3b>(row, col)[2] = 0;
//gray.at<uchar>(row, col) = min(r, min(b, g));
}
}
}
//位操作,按位取反,实现反相:bitwise_not(src, dst);
namedWindow("output", CV_WINDOW_AUTOSIZE);
imshow("output", dst);
waitKey(0);
return 0;
}
/*3通道的图像的像素定义+单通道的像素的定义*/
int b = src.at<Vec3b>(row, col)[0];
gray.at<uchar>(row, col);