#include<opencv2\opencv.hpp>
#include<highgui.h>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
// read image
Mat image = imread("test.jpg");
// 对图像进行所有像素用 (255- 像素值)
Mat invertImage;
image.copyTo(invertImage);
// 获取图像宽、高
int channels = image.channels();
int rows = image.rows; //高---行
int col = image.cols;//宽---列
cout << channels << " "<<rows<<endl;
int cols = image.cols * channels;
cout << cols << endl;
if (image.isContinuous()) {
cols *= rows;
rows = 1;
}
// 每个像素点的每个通道255取反(0-255(黑-白))
uchar* p1;
uchar* p2;
for (int row = 0; row < rows; row++) {
p1 = image.ptr<uchar>(row);// 获取像素指针
p2 = invertImage.ptr<uchar>(row);
for (int col = 0; col < cols; col++) {
*p2 = 255 - *p1; // 取反
p2++;
p1++;
}
}
// create windows
namedWindow("My Test", CV_WINDOW_AUTOSIZE);
namedWindow("My Invert Image", CV_WINDOW_AUTOSIZE);
// display image
imshow("My Test", image);
imshow("My Invert Image", invertImage);
// 关闭
waitKey(0);
destroyWindow("My Test");
destroyWindow("My Invert Image");
return 0;
}