寻找精密光学标定板上的矩形(网友提问)

这道题目来源于网友问答,采用博客方式回答比较容易说明。

问题:
对于这样的图片
如何寻找上面的矩形
 
思路:
这个矩形的面积在所有的图像中是最大的(除去整个图形轮廓以外),可以尝试从这个方面入手,再加上一些鲁壮的方法
 
 
# include  "stdafx.h"
# include  <opencv2 /opencv.hpp >
# include  "GOCVHelper.h"
# include  <iostream >
using  namespace cv;
using  namespace std;
 
int main(  int argc,  const  char * * argv )
{
    Mat src  = imread( "card.png",IMREAD_COLOR);
    Mat gray;
     int imax  =  0;  //代表最大轮廓的序号
     int imaxcontour  =  - 1;  //代表最大轮廓的大小
    std : :vector <std : :vector <Point >>contours;    
    cvtColor(src,gray,COLOR_BGR2GRAY);
    threshold(gray,gray, 100, 255,THRESH_OTSU);
    bitwise_not(gray,gray); // 白色代表有数据
     //寻找轮廓
    findContours(gray,contours,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
     //冒泡排序,由大到小排序
    VP vptmp;
     for( int i = 1;i <contours.size();i ++){
         for( int j =contours.size() - 1;j > =i;j --){
             if( contourArea(contours[j]) > contourArea(contours[j - 1])){    
                 //swap
                vptmp  = contours[j - 1];
                contours[j - 1]  = contours[j];
                contours[j]  = vptmp;
            }
        }
    }
     //找到最后结果的时候,添加一些判断
     for ( int i  =  0;i <contours.size();i ++)
    {
         if (contourArea(contours[i])  < (src.rows  * src.cols) / 8 )
        {
            drawContours(src,contours,i,Scalar( 0, 0, 255), - 1);
             break;
        }
    }
    imshow( "结果",src);
    waitKey();
     return  0;
}
 
结果:
 
小结:
当然这里只是对最简单的模板图片进行了处理。如果在实际的摄像机拍摄的过程中,肯定会有其他的干扰,需要区别对待。

 

posted on   jsxyhelu  阅读(25)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示