方法1:调用库函数
void ColorInvert::mat_operator(Mat& image)
{
Mat dst;
Mat m = Mat::zeros(image.size(), image.type());
m = Scalar(2, 2, 2);
multiply(image, m, dst);
m1 = Scalar(50,50, 50);
//divide(image, m, dst);
//add(image, m1, dst);
//subtract(image, m1, dst);
namedWindow("加法操作", WINDOW_FREERATIO);
imshow("加法操作", dst);
}
方法2:自己给三维数据赋值
void ColorInvert::mat_operator(Mat& image)
{
Mat dst = Mat::zeros(image.size(), image.type());
m = Scalar(50, 50, 50);
//namedWindow("加法操作", WINDOW_FREERATIO);
int w = image.cols;
int h = image.rows;
int dims = image.channels();
for (int row = 0; row < h; row++)
{
for (int col = 0; col < w; col++)
{
Vec3b p1 = image.at<Vec3b>(row,col);
Vec3b p2 = m.at<Vec3b>(row, col);
dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(p1[0]+ p2[0]); //saturate_cast 小于0,赋值为0,大于255,赋值为255。
dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(p1[1] + p2[1]);
dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(p1[2] + p2[2]);
}
}
}