#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
if (argc < 2) {
cout << "usage: " << argv[0] << " <image-name>" << endl;
return -1; }
Mat src, gray_src, gray_src2, dst, dst2, dst3;
src = imread(argv[1]);
if(src.empty()) {
cout << "could not load image." << endl;
return -1;
}
namedWindow("Src image", CV_WINDOW_AUTOSIZE);
imshow("Src image", src);
waitKey(0);
gray_src.create(src.size(), src.type());
gray_src2.create(src.size(), src.type());
dst.create(src.size(), src.type());
dst2.create(src.size(), src.type());
dst3.create(src.size(), src.type());
cvtColor(src, gray_src, CV_BGR2GRAY);
namedWindow("gray image", CV_WINDOW_AUTOSIZE);
imshow("gray image", gray_src);
waitKey(0);
int height, width;
height = src.rows;
width = src.cols;
for (int row = 0; row < height; ++row)
{
for (int col = 0; col < width; ++col)
{
int gray = gray_src.at<uchar>(row, col);
gray_src.at<uchar>(row, col) = 255 - gray; //灰度值反转
}
}
//读取RGB值
int nc = src.channels();
for (int row = 0; row < height; ++row)
{
for (int col = 0; col < width; ++col)
{
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] = 255 -b;
dst.at<Vec3b>(row, col)[1] = 255 -g;
dst.at<Vec3b>(row, col)[2] = 255 -r;
dst3.at<Vec3b>(row, col)[0] = 255 -b;
dst3.at<Vec3b>(row, col)[1] = 255 -g;
dst3.at<Vec3b>(row, col)[2] = 0; //实现红色背景效果
gray_src2.at<uchar>(row, col) = max(r, max(g, b)); //改变灰度图的灰度值
}
}
}
bitwise_not(src, dst2); //对src内的每个通道值取反,等价于上面两层for循环操作
namedWindow("reversal gray image", CV_WINDOW_AUTOSIZE);
imshow("reversal gray image", gray_src);
waitKey(0);
namedWindow("change value gray image", CV_WINDOW_AUTOSIZE);
imshow("change value gray image", gray_src2);
waitKey(0);
namedWindow("RGB reversal image", CV_WINDOW_AUTOSIZE);
imshow("RGB reversal image", dst);
waitKey(0);
namedWindow("RGB reversal image with bitwise_not", CV_WINDOW_AUTOSIZE);
imshow("RGB reversal image with bitwise_not", dst2);
waitKey(0);
namedWindow("RGB reversal image and red", CV_WINDOW_AUTOSIZE);
imshow("RGB reversal image and red", dst3);
waitKey(0);
return 0;
}