一,读取图像
Mat src, gray_src;
src = imread("F:/opencv/InputImg/h4.jpg");
if (src.empty()) cout << "图像打开失败" << endl;
namedWindow("input", CV_WINDOW_AUTOSIZE);
imshow("input img", src);
二,反差图像
是图像为字节类型上午,大小是0-255,所以255减去所有的像素值,就实现了图像的反差
一,单通道反差
这个适用于只有一个通道的图片的反差,首先将读入的图片转为单通道
cvtColor(src, gray_src, CV_BGR2GRAY);
//改变为单通道
gray_src.at<uchar>(row, col)
读取每个像素值
cvtColor(src, gray_src, CV_BGR2GRAY); //改变为单通道
int width = gray_src.cols;
int height = gray_src.rows;
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;//反差图像
}
}
namedWindow("output", CV_WINDOW_AUTOSIZE);
imshow("反差图像", gray_src); //单通道反差
2,多通道反差
通过 int r = src.at<Vec3b>(row, col)[0];
读取每个通道的byte值,然后分别对每个通道进行反差
Mat dst;
dst.create(src.size(), src.type());
height = src.rows;
width = src.cols;
int channels = src.channels();
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
if (channels == 1) { //单通道
int gray = gray_src.at<uchar>(row, col); //读取像素值
gray_src.at<uchar>(row, col) = 255 - gray;//反差图像
}
else if (channels == 3) {
int r = src.at<Vec3b>(row, col)[0]; //vec3b 读取成3个 byte图像
int g= src.at<Vec3b>(row, col)[1];
int b = src.at<Vec3b>(row, col)[2];
dst.at<Vec3b>(row, col)[0] = 255 - r;
dst.at<Vec3b>(row, col)[1] = 255 - g;
dst.at<Vec3b>(row, col)[2] = 255 - b;
}
}
}
namedWindow("output", CV_WINDOW_AUTOSIZE);
imshow("多通道反差图像", dst); //多通道反差
三,调用系统api进行反差 bitwise_not
图像的反差同时也能够调用系统的api
bitwise_not(src, dst);
五,案例演示
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
Mat src, gray_src;
src = imread("F:/opencv/InputImg/h4.jpg");
if (src.empty()) cout << "图像打开失败" << endl;
namedWindow("input", CV_WINDOW_AUTOSIZE);
imshow("input img", src);
cvtColor(src, gray_src, CV_BGR2GRAY); //改变为单通道
int width = gray_src.cols;
int height = gray_src.rows;
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;//反差图像
}
}
namedWindow("output", CV_WINDOW_AUTOSIZE);
imshow("反差图像", gray_src); //单通道反差
//多通道反差
Mat dst;
dst.create(src.size(), src.type());
height = src.rows;
width = src.cols;
int channels = src.channels();
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
if (channels == 1) { //单通道
int gray = gray_src.at<uchar>(row, col); //读取像素值
gray_src.at<uchar>(row, col) = 255 - gray;//反差图像
}
else if (channels == 3) {
int r = src.at<Vec3b>(row, col)[0]; //vec3b 读取成3个 byte图像
int g= src.at<Vec3b>(row, col)[1];
int b = src.at<Vec3b>(row, col)[2];
dst.at<Vec3b>(row, col)[0] = 255 - r;
dst.at<Vec3b>(row, col)[1] = 255 - g;
dst.at<Vec3b>(row, col)[2] = 255 - b;
}
}
}
namedWindow("output", CV_WINDOW_AUTOSIZE);
imshow("多通道反差图像", dst); //多通道反差
//调用系统api 进行反差
bitwise_not(src, dst);
namedWindow("output", CV_WINDOW_AUTOSIZE);
imshow("多通道反差图像", dst); //多通道反差
waitKey(0);
}