#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat example = imread("D:\\VC项目\\img1.jpg");
if (!example.data)
{
cout << "图片路径错误!!!" << endl;
}
Mat out_ex1;
cvtColor(example, out_ex1, COLOR_BGR2GRAY);
Mat out_ex2 = example.clone();
Mat out_ex3 = example.clone();
int height = example.rows;
int weight = example.cols;
int gray;
int nc_example = example.channels();
//操作像素实现反相
for (int row = 0; row < height;row++)
{
for (int col = 0; col < weight; col++)
{
if (nc_example == 1)
{
gray = out_ex1.at<uchar>(row, col);
out_ex1.at<uchar>(row, col) = 255 - gray;
}
else
{
int out_ex2_b = out_ex2.at<Vec3b>(row, col)[0]; //Vec3b把rgb图像的每个通道读取成uchar类型
int out_ex2_g = out_ex2.at<Vec3b>(row, col)[1];//Vec3f把数据读成float类型
int out_ex2_r = out_ex2.at<Vec3b>(row, col)[2];
out_ex2.at<Vec3b>(row, col)[0] = 255 - out_ex2_b;
out_ex2.at<Vec3b>(row, col)[1] = 255 - out_ex2_g;
out_ex2.at<Vec3b>(row, col)[2] = 255 - out_ex2_r;
}
}
}
bitwise_not(example, out_ex3);//直接利用opencv中的函数实现反转
imshow("example", example);
imshow("out_ex1", out_ex1);
imshow("out_ex2", out_ex2);
imshow("out_ex3", out_ex3);
waitKey(0);
return 0;
}
c++ 利用源码实现彩色图像的反相,对像素值进行操作
最新推荐文章于 2022-10-22 00:08:16 发布