opencv HSV空间提取图像中红色,及判断每一个像素颜色

本文主要参考https://blog.csdn.net/zdyueguanyun/article/details/50739374

                     https://blog.csdn.net/liumoude6/article/details/78318053

                     https://it1995.blog.csdn.net/article/details/83056346

结合这三篇文章以及自己的一点思考,提取图像中的红色并把其它部分都置为白色代码如下:

void colorFilter(Mat srcImage, Mat &outImage)
{
	Mat srcImage_hsv;
	cvtColor(srcImage, srcImage_hsv,CV_BGR2HSV);
	

	int nl = srcImage.rows;
	int nc = srcImage.cols;

	for (int m = 0; m < nl; m++)
	{

		for (int n = 0; n < nc; n++)
		{
			//&& (srcImage_hsv.at<Vec3b>(m, n)[1]>43)
			//以下代码是提取红色部分
			if (!((((srcImage_hsv.at<Vec3b>(m, n)[0] >= 0) && (srcImage_hsv.at<Vec3b>(m, n)[0] <= 15)) || (srcImage_hsv.at<Vec3b>(m, n)[0] >= 125) && (srcImage_hsv.at<Vec3b>(m, n)[0] <= 180)) && (srcImage_hsv.at<Vec3b>(m, n)[2]>=46) && (srcImage_hsv.at<Vec3b>(m, n)[1]>=43)))			
			
			{
				srcImage.at<Vec3b>(m, n)[0] = 255;
				srcImage.at<Vec3b>(m, n)[1] = 255;
				srcImage.at<Vec3b>(m, n)[2] = 255;
			}
			
		}
		
	}
	outImage = srcImage.clone();
}

使用该代码对下图(图源来自网络)处理

其结果为:

参考第三个链接的内容,可以判断图像中每个像素的颜色

//区分图像颜色
void distinguishColor(Mat matSrc)
{
	Mat matHsv;
	cvtColor(matSrc, matHsv, COLOR_BGR2HSV);
	
	for (int i = 0; i < matSrc.rows; i++)
	{
		for (int j = 0; j < matSrc.cols; j++)
		{
			vector<int> colorVec;
			colorVec.push_back(matHsv.at<Vec3b>(i,j)[0]);
			colorVec.push_back(matHsv.at<Vec3b>(i,j)[1]);
			colorVec.push_back(matHsv.at<Vec3b>(i,j)[2]);
			

			if ((colorVec[0] >= 0 && colorVec[0] <= 180) && (colorVec[1] >= 0 && colorVec[1] <= 255) && (colorVec[2] >= 0 && colorVec[2] <= 46))
			{

				cout << "黑" << endl;
			}
			else if ((colorVec[0] >= 0 && colorVec[0] <= 180) && (colorVec[1] >= 0 && colorVec[1] <= 43) && (colorVec[2] >= 46 && colorVec[2] <= 220))
			{

				cout << "灰" << endl;
			}

			else if ((colorVec[0] >= 0 && colorVec[0] <= 180) && (colorVec[1] >= 0 && colorVec[1] <= 30) && (colorVec[2] >= 221 && colorVec[2] <= 255))
			{

				cout << "白" << endl;
			}
			else if (((colorVec[0] >= 0 && colorVec[0] <= 10) || (colorVec[0] >= 156 && colorVec[0] <= 180)) && (colorVec[1] >= 43 && colorVec[1] <= 255) && (colorVec[2] >= 46 && colorVec[2] <= 255))
			{
				
				cout << "红" << endl;
			}
			else if ((colorVec[0] >= 11 && colorVec[0] <= 25) && (colorVec[1] >= 43 && colorVec[1] <= 255) && (colorVec[2] >= 46 && colorVec[2] <= 255))
			{
				
				cout << "橙" << endl;
			}
			else if ((colorVec[0] >= 26 && colorVec[0] <= 34) && (colorVec[1] >= 43 && colorVec[1] <= 255) && (colorVec[2] >= 46 && colorVec[2] <= 255))
			{
				
				cout << "黄" << endl;
			}
			else if ((colorVec[0] >= 35 && colorVec[0] <= 77) && (colorVec[1] >= 43 && colorVec[1] <= 255) && (colorVec[2] >= 46 && colorVec[2] <= 255))
			{
				
				cout << "绿" << endl;
			}
			else if ((colorVec[0] >= 78 && colorVec[0] <= 99) && (colorVec[1] >= 43 && colorVec[1] <= 255) && (colorVec[2] >= 46 && colorVec[2] <= 255))
			{
				
				cout << "青" << endl;
			}
			else if ((colorVec[0] >= 100 && colorVec[0] <= 124) && (colorVec[1] >= 43 && colorVec[1] <= 255) && (colorVec[2] >= 46 && colorVec[2] <= 255))
			{
				
				cout << "蓝" << endl;
			}
			else if ((colorVec[0] >= 125 && colorVec[0] <= 155) && (colorVec[1] >= 43 && colorVec[1] <= 255) && (colorVec[2] >= 46 && colorVec[2] <= 255))
			{
				
				cout << "紫" << endl;
			}
			else
			{
				
				cout << "未知" << endl;
			}
		}
	}
	
}

 

文中若有错误或不妥之处,还望指出,以便共同学习!

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值