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

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

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

其结果为:

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

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

//区分图像颜色
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 colorVec;
			colorVec.push_back(matHsv.at(i,j)[0]);
			colorVec.push_back(matHsv.at(i,j)[1]);
			colorVec.push_back(matHsv.at(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;
			}
		}
	}
	
}

 

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

 

你可能感兴趣的:(opencv图像处理,opencv)