特定标签检测

5 篇文章 0 订阅

方法1

参考连接:
https://www.jaspr.me/extract-color-card-with-python-and-opencv/
参考这种方法检测的步骤是:

通过颜色分割 ——> canny——>轮廓筛选——> 确定目标(角点)——>矫正 ——> 编码解码

轮廓筛选部分包括: 面积、长宽比例、轮廓的层级筛选

可以看到 蓝色的是我绘制出的轮廓,绿色是我绘制出的四条边(也就是检测角点地方),可以看到对于倾斜的标签检测效果很不理想。

检测如下所示:

参考代码:

 for(int k = 0; k < (int)twocontours.size(); k++)    //查找轮廓
    {

        if (int(twocontours.at(k).size()) <= 3)//轮廓面积小于60的不处理
        {
            std::cout << "交点:" << int(twocontours.at(k).size()) << std::endl;
            drawContours(resizeImage, twocontours, k, Scalar(0,0,255), FILLED);
        } else {
//            RotatedRect rRect = fitEllipse(twocontours.at(k));//minAreaRect
            RotatedRect rRect = minAreaRect(twocontours.at(k));
            double area1 = contourArea(twocontours[k]);
            double majorAxis = rRect.size.height > rRect.size.width ? rRect.size.height : rRect.size.width;
            double minorAxis = rRect.size.height > rRect.size.width ? rRect.size.width : rRect.size.height;
            float rate = majorAxis / minorAxis;
//            if(area1 < 20000 || area1 >40000) continue;
            cout <<   area1 << endl;
            if (rate < 1.5 && rate > 0.5 )   //长短轴之比小于1.4的轮廓
            {
                //可能为二维码的区域,判断是否为二维码区域
                printf("rate: %f\n", rate);

                //获取旋转矩形的四个顶点
                cv::Point2f vertices[4];
                rRect.points(vertices);
                //逐条边绘制
                for (int j = 0; j < 4; j++)
                {
                    cv::line(resizeImage, vertices[j], vertices[(j + 1) % 4], cv::Scalar(0, 255, 0),3);
                }

                // iterate through all the top-level contours,
                // draw each connected component with its own random color
                int num = 0;
                int pre=0;
                while(hierarchy[k][2] != -1) {  //存在外轮廓
                    pre = k;
                    k = hierarchy[k][2];       //index
                    num = num + 1;
                    //超过4层则判断为定位点
                    if(num == 2 && hierarchy[k][0] == -1 && hierarchy[k][1] == -1) {
                        drawContours(resizeImage, twocontours, pre, Scalar(255, 0, 0), 2);//把轮廓涂成蓝色
                        printf("蓝色是二维码区域\n");
                        std::cout << "交点:" << int(twocontours.at(k).size()) << std::endl;


                    }
                }
            } else  //长短轴之比大于1.4的轮廓  不是二维码区域
            {
                drawContours(resizeImage, twocontours, k, Scalar(0, 0, 255), FILLED);//把轮廓涂成红色
            }
        }
    }

方法二

针对刚才的标签检测角点的问题,从网看到了另外一种方法,也就是利用霍夫直线检测的方法,但是这种方法对于多个标签检测也存在问题,不是很容易检测出。会出现把几个标签的直线拟合在一起了。
所以依然放弃了这种方法,

颜色分割 ——> canny——>轮廓检测——> 检测直线 ——>确定目标(角点)——>矫正 ——> 编码解码

参考连接:https://blog.csdn.net/qq_36917144/article/details/112694491

方法三

参考连接:
https://blog.csdn.net/zxw_xzr/article/details/77358815

这种方法是我实际使用的方法。采用了凸包检测和多边拟合的方法,检测效果比较理想,可以直接看效果图。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值