OpenCV改变选定区域颜色

主函数Mat::at<_Tp>

  • public method _Tp& at<_Tp>(int row, int col)
  • in class Mat
Params
row: Index along the dimension 0
col: Index along the dimension 1
	Mat img = imread("E:\\20210416_145345_10000.jpg", 0);
	threshold(img, img, 200, 255, ADAPTIVE_THRESH_GAUSSIAN_C);
	cvtColor(img, img, COLOR_GRAY2BGR);
	for (int i = 0; i < img.rows; ++i)
	{
		for (int j = 0; j < img.cols; ++j)
		{
			const int c_point_b = img.at<Vec3b>(i, j)[0];//蓝
			const int c_point_g = img.at<Vec3b>(i, j)[1];//绿
			const int c_point_r = img.at<Vec3b>(i, j)[2];//红
			if (c_point_b < 100 & c_point_r < 100 & c_point_g < 100)
			{
				img.at<Vec3b>(i, j)[0] = 255;
				img.at<Vec3b>(i, j)[1] = 0;
				img.at<Vec3b>(i, j)[2] = 0;
			}
		}
	}
	imshow("img", img);
	waitKey(0);

效果图

posted @ 2021-04-29 18:04  callcall  阅读(678)  评论(0编辑  收藏  举报