opencv灰度反转
时间: 2023-11-06 20:01:13 浏览: 101
Opencv灰度变化
在OpenCV中,可以使用以下代码对图像进行灰度反转:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main() {
Mat image1, output_image, image1_gray;
image1 = imread("lena.png");
if (image1.empty()) {
cout << "读取错误" << endl;
return -1;
}
cvtColor(image1, image1_gray, COLOR_BGR2GRAY);
imshow(" image1_gray", image1_gray);
output_image = image1_gray.clone();
for (int i = 0; i < image1_gray.rows; i++) {
for (int j = 0; j < image1_gray.cols; j++) {
output_image.at<uchar>(i, j) = 255 - image1_gray.at<uchar>(i, j);
}
}
imshow(" output_image", output_image);
waitKey(0);
return 0;
}
阅读全文