Opencv第5讲---图像像素的算数操作


void QuickDemo::operators_demo(Mat& image)
{
	//Mat dst;
	//dst = image + Scalar(50, 50, 50);  //针对三通道的图
	//dst image + Scalar(50);          //针对单通道的图

	//dst = image - Scalar(50, 50, 50);  //针对三通道的图
	//dst image - Scalar(50);          //针对单通道的图

	Mat dst;
	Mat m = Mat::zeros(image.size(), image.type());
	m = Scalar(2, 2, 2);
	multiply(image, m, dst);        //乘法操作

	int w = image.cols;      //获得图像的宽
	int h = image.rows;      //获得图像的高
	int dims = image.channels();  //获得图像通道的数量

	for (int row = 0; row < h; row++)
	{
		uchar* current_row = image.ptr(row);    //获取当前行的指针
		for (int col = 0; col < w; col++)
		{
			Vec3b p1 = image.at(row, col);
			Vec3b p2 = m.at(row, col);
			dst.at(row, col)[0] = saturate_cast(p1[0] + p2[0]);     //将和的范围控制在0-255之间
			dst.at(row, col)[1] = saturate_cast(p1[1] + p2[1]);
			dst.at(row, col)[2] = saturate_cast(p1[2] + p2[2]);
		}
	}

	//add(image, m, dst);
	//subtract(image, m, dst);
	/*divide(image, m, dst);
	multiply(image, m, dst);*/


	imshow("加法操作", dst);
}