1 #include "stdafx.h"
2 #include <algorithm>
3 #include <opencv2/core/core.hpp>
4 #include <opencv2/highgui/highgui.hpp>
5 #include <opencv2/imgproc/imgproc.hpp>
6 using namespace cv;
7
8 int _tmain(int argc, _TCHAR* argv[])
9 {
10 Mat img = imread("cat.jpg");
11 Mat dst1,dst2;
12 img.copyTo(dst1);
13 img.copyTo(dst2);
14 int row = img.rows;
15 int col = img.cols;
16 for (int i = 0; i < row;++i)
17 for (int j = 0; j < col; ++j){
18 int b = img.at<Vec3b>(i, j)[0];//blue
19 int g = img.at<Vec3b>(i, j)[1];//green
20 int r = img.at<Vec3b>(i, j)[2];//red
21 int maxrgb = max(max(r, g), b);
22 int minrgb = min(min(r, g), b);
23 dst1.at<Vec3b>(i, j)[0] = maxrgb + minrgb - b;
24 dst1.at<Vec3b>(i, j)[1] = maxrgb + minrgb - g;
25 dst1.at<Vec3b>(i, j)[2] = maxrgb + minrgb - r;
26 dst2.at<Vec3b>(i, j)[0] = 255 - b;
27 dst2.at<Vec3b>(i, j)[1] = 255 - g;
28 dst2.at<Vec3b>(i, j)[2] = 255 - r;
29 }
30 imshow("原图" , img );
31 imshow("补色", dst1);
32 imshow("反色", dst2);
33 waitKey();
34 return 0;
35 }