单通道图像反差处理
//读取图像像素,获取反差图
Mat gray;
cvtColor(src, gray, CV_BGR2GRAY);
int height, width;
height = gray.rows;
width = gray.cols;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
int gray_value = gray.at<uchar>(row, col);
gray.at<uchar>(row, col) = 255 - gray_value;
}
}
多通道反差处理
//多通道反差图
Mat grayMul,invertImg;
cvtColor(src, gray, CV_BGR2GRAY);
invertImg = Mat::zeros(src.size(), src.type());
int height1, width1;
height1 = gray.rows;
width1 = gray.cols;
int channel = src.channels();
for (int row = 0; row < height1; row++)
{
for (int col = 0; col < width1; col++)
{
if (channel == 1)
{
int gray_value = gray.at<uchar>(row, col);
gray.at<uchar>(row, col) = 255 - gray_value;
}
else
{
int chan1 = src.at<Vec3b>(row, col)[0];
int chan2 = src.at<Vec3b>(row, col)[1];
int chan3 = src.at<Vec3b>(row, col)[2];
invertImg.at<Vec3b>(row, col)[0] = 255 - chan1;
invertImg.at<Vec3b>(row, col)[1] = 255 - chan2;
invertImg.at<Vec3b>(row, col)[2] = 255 - chan3;
}
}
}
opencv算子反差求法
//opencv反差图算法
bitwise_not(src, invertImg);//一个算子搞定
//Vec3b对应三通道的顺序是bgr的uchar类
//Vec3f对应float类型数据
//把cv_8uc1转到cv32f1:src.convertTo(dst,CV_32F)
//取三个数最小值思想min(a,min(b,c))