opencv HSV空间提取图像中红色
程序员文章站
2022-05-20 22:30:57
...
本文主要参考https://blog.csdn.net/zdyueguanyun/article/details/50739374
https://blog.csdn.net/liumoude6/article/details/78318053
结合这两篇文章以及自己的一点思考,提取图像中的红色并把其它部分都置为白色代码如下:
void colorFilter(Mat srcImage, Mat &outImage)
{
Mat srcImage_hsv;
cvtColor(srcImage, srcImage_hsv,CV_BGR2HSV);
//imwrite("E:\\img_compare\\多印章图片\\印章\\31\\hsv.jpg", srcImage_hsv);
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();
}
使用该代码对下图(图源来自网络)处理
其结果为:
文中若有错误或不妥之处,还望指出,以便共同学习!