(opencv+Qt)的QR码精确定位与识别完全解析(精度可达±0.1mm,±0.1°)

一、介绍

算法功能
对QR码进行x,y方向定位和旋转角度计算,并获取QR码的二进制内容
算法优势
1.计算速度快,可达4-7ms(使用cpu i7-8750)。
2.定位精度高,x,y方向精度为±1mm,转角精度为±0.1°(使用某宝几十元彩色相机,30w像素,噪声较为严重)。
3.采用自动阈值方法,对光照不敏感。
4.采用不规则四边形轮廓提取和网格划分,支持二维码翻转识别,最大翻转倾角可达45°。
5.对QR码的规模自动计算,可用于不同行列数的QR码。

先看几张效果图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、思路和代码(共10步)

阅读前注意!!!本算法使用C++11和Qt共同开发,某些数据类型(如字符串QString、链表QList)为Qt专属类型,可能需要稍加改动后才能供读者使用。
本文重在分享思路,若需要源码,请在评论中留言。

1.彩色图转灰度图+高斯滤波

在这里插入图片描述
也可以不进行滤波,如果使用滤波算法,核不推荐过大(此处采用Size(1, 1))。

Mat src_gray;
cvtColor(srcImg1, src_gray, CV_BGR2GRAY); //彩色图转灰度图
GaussianBlur(src_gray, src_gray, Size(1, 1),2, 2, BORDER_DEFAULT); //高斯滤波
imshow("1.彩色图转灰度图+高斯滤波", src_gray);

2.二值化(Otsu自动阈值)

在这里插入图片描述

Mat threshold_output;
threshold(src_gray, threshold_output, 0, 255, THRESH_BINARY|THRESH_OTSU); //Otsu 二值化
imshow("2.二值化(Otsu自动阈值)", threshold_output);

3.形态学滤波(开运算+闭运算)

开运算和开运算是基于几何运算的滤波器。
开运算(先腐蚀,后膨胀)能够除去孤立的小点,毛刺和小桥,而总的位置和形状不变。闭运算(先膨胀,后腐蚀)能够填平小湖(即小孔),弥合小裂缝,而总的位置和形状不变。
参数MORPH_ELLIPSE表示使用椭圆形运算核,可使处理后的边界较为圆滑。
在这里插入图片描述

Mat threshold_output_copy = threshold_output.clone();
Mat element = getStructuringElement(MORPH_ELLIPSE, Size(3, 3)); 
morphologyEx(threshold_output_copy, threshold_output_copy, MORPH_OPEN, element); //开运算
morphologyEx(threshold_output_copy, threshold_output_copy, MORPH_CLOSE, element); //闭运算
imshow("3.开运算+闭运算", threshold_output_copy);

4.边缘检测

使用Canny算子进行边缘检测,为下一步提取轮廓做准备。
在这里插入图片描述

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Mat canny_output;
Canny( threshold_output_copy, canny_output, 1, 3, 7,true );  //Canny检测
imshow("4.Canny边缘检测", canny_output);

5.轮廓提取

使用findContours函数对边缘提取后的图像进行轮廓提取。
在这里插入图片描述

Mat image=canny_output.clone();
findContours(image,contours,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point());
Mat Contours=Mat::zeros(image.size(),CV_8UC1);  
//绘制 //contours[i]代表的是第i个轮廓,contours[i].size()代表的是第i个轮廓上所有的像素点数
for(int i=0;i<contours.size();i++)
    for(int j=0;j<contours[i].size();j++){
        Point P=Point(contours[i][j].x,contours[i][j].y);
        Contours.at<uchar>(P)=255;
        }  
imshow("5.findContours轮廓提取",Contours); //轮廓

6.筛选出三层独立包含特点的轮廓

在这里插入图片描述

QList<vector<vector<Point>>> qrPointList;//每个节点表示一个回形点集
vector<vector<Point>> qrPoint;
Mat mat6=Contours.clone();
cvtColor(mat6, mat6, CV_GRAY2BGR);
int Parindex[3]={-1,-1,-1};
for (int i = 0; i < contours.size(); i++){
        if (hierarchy[i][3] != -1 && hierarchy[i][2] == -1){
            Parindex[0]=hierarchy[i][3];
            if (hierarchy[(Parindex[0])][3] != -1){
                Parindex[1]=hierarchy[(Parindex[0])][3];
                if (hierarchy[(Parindex[1])][3] != -1){
                    Parindex[2]=hierarchy[(Parindex[1])][3];
                    if (hierarchy[(Parindex[2])][3] != -1){
                        if(!(i-1==Parindex[0]&&Parindex[0]-1==Parindex[1]&&Parindex[1]-1==Parindex[2]))
                            continue; //都是独生轮廓
                        qrPoint.push_back(contours[i]);
                        qrPoint.push_back(contours[i-2]);
                        qrPoint.push_back(contours[i-4]);
                        for(int i=0;i<qrPoint.size();i++)
                            for(int j=0;j<qrPoint[i].size();j++)
                                circle(mat6,qrPoint[i][j],2,Scalar(0,255,0),-1);
                        qrPointList.push_back(qrPoint);
                        qrPoint.clear();
                    }
                }
            }
        }
}
imshow("6.检测出的三层轮廓",mat6); //轮廓

7.若筛选出的三层轮廓数目大于3,进一步筛选,最终只保留三个

此处代码可自行编写,我的思路为:
1.将每组三层轮廓分别拟合最小外接矩形,然后根据同心度、三层周长比例(3:5:7)、最小允许周长进行初步筛选。
2.对剩下的进行最终筛选,筛选标准为正确的QR码三个角元素应有一定的平行度,尺寸也相近。
注意!!!此段代码12,13行分别根据轮廓点集计算出最小外接多边形和最小外接矩形,由于镜头与QR码可能存在倾角,故后面使用最小外接四边形进行定位,精度更高。
要注意两者的区别,其中approxPolyDP的精度设定非常重要,精度不宜过高(此处选择精度为5,值越小精度越高),否则会使得拟合出的四边形边数过多。

QList<Point> pointList; //存储角点中心
    QList<RotatedRect> RectList; //存储角元素最外层矩形
    QList<vector<Point>> OutquadList; //存储最外层拟合四边形角点
    vector<bool> qrPointListEnable(qrPointList.size()); //筛选时使用
    for (int L=0;L<qrPointList.size(); L++)//遍历每个可能的图元
    {
        qrPoint=qrPointList.at(L);      
        vector<vector<Point>> contours_poly(qrPoint.size());
        vector<RotatedRect> minRect(qrPoint.size()); //存储了嵌套的最小外接矩形*****
        vector<Point2f> rect_center(qrPoint.size());
        for (int i = 0; i < qrPoint.size(); i++){
            approxPolyDP(Mat(qrPoint[i]), contours_poly[i], 5, true);//用指定精度逼近多边形曲线
            minRect[i] = minAreaRect(Mat(qrPoint[i])); //得到最小外接矩形
            rect_center[i]=minRect[i].center; //得到最小外接矩形中心
        }
        //根据同心度筛选
        for (int i = 0; i < minRect.size()-1; i++){
            Point P1=Point(rect_center[i].x,rect_center[i].y);
            Point P2=Point(rect_center[i+1].x,rect_center[i+1].y);
            float ConcenError_Set=(minRect[i].size.width+minRect[i].size.height)/12; //***最大允差设定***
            if( sqrt(pow(P1.x-P2.x,2)+pow(P1.y-P2.y,2)) > ConcenError_Set  ){
                qrPointListEnable[L]=false;
                break; }
            else
                qrPointListEnable[L]=true;
        }
        if(!qrPointListEnable[L])continue;

        //根据三层周长比例进行筛选(3:5:7)
        for (int i = 0; i < minRect.size()-1; i++) {
            float circum1=(minRect[i].size.width+minRect[i].size.height)*2;
            float circum2=(minRect[i+1].size.width+minRect[i+1].size.height)*2;
            if( circum1/circum2>=0.5 && circum1/circum2<=0.8 )  //***周长比例设定***
                qrPointListEnable[L]=true;
            else{
                qrPointListEnable[L]=false;
                break; }
        }
        if(!qrPointListEnable[L])continue;

        //周长不能过小
        for (int i = 0; i < minRect.size(); i++){
            float circum=(minRect[i].size.width+minRect[i].size.height)*2;
            float circum_Set=20;  //***有效周长最小值设定***
            if( circum >= circum_Set )
                qrPointListEnable[L]=true;
            else{
                qrPointListEnable[L]=false;
                break; }
        }
        if(!qrPointListEnable[L])continue;

        //筛选完毕!!!筛选出的个数可能为任意自然数
        for (int i = 0; i<qrPoint.size(); i++){
            Point2f rect_points[4];
            minRect[i].points(rect_points);
            if(i==2)
                RectList.push_back(minRect[i]);        //RectList赋值   筛选后的最外层外接矩形
            bool exsit=false;
            Point P=Point(rect_center[i].x,rect_center[i].y);
            for(int j=0;j<pointList.size();j++){
                if( fabs(pointList.at(j).x-P.x)<10 && fabs(pointList.at(j).y-P.y)<10 ){
                    exsit=true; break; }
            }
            if(!exsit||pointList.size()==0)
                pointList.append(P);                   //pointList赋值    筛选后的三层同心中心点
            if(i==2)
                OutquadList.append(contours_poly[i]);  //OutquadList赋值    最外层外接四边形
        }
    }

//8    //最终筛选,保留可能性最大的三个角点和轮廓
    if(RectList.size()>3){
        QList<float> RectSizeErrorList; //尺寸误差
        for(int i=0;i<RectList.size();i++){
            float RectSizeError=0;
            float RectSize1=( RectList.at(i).size.width + RectList.at(i).size.height )*2;
            for(int j=0;j<RectList.size();j++ && j!=i){
                float RectSize2=( RectList.at(j).size.width + RectList.at(j).size.height )*2;
                float Error= fabs( RectSize1 - RectSize2 );
                RectSizeError+=Error;
            }
            RectSizeErrorList.append(RectSizeError);
        }
        QList<float> RectAngleErrorList; //角度误差
        for(int i=0;i<RectList.size();i++){
            float RectAngleError=0;
            float RectAngle1=RectList.at(i).angle;
            for(int j=0;j<RectList.size();j++ && j!=i){
                float RectAngle2=RectList.at(j).angle;
                float Error= fabs( RectAngle1 - RectAngle2 );
                RectAngleError+=Error;
            }
            RectAngleErrorList.append(RectAngleError);
        }
        QList<float> RectErrorList; //综合误差
        for(int i=0;i<RectList.size();i++)
            RectErrorList.append(RectSizeErrorList.at(i)+RectAngleErrorList.at(i));
        for(int i=RectErrorList.size()-2;i>=0;i--) //根据综合误差 对 矩形链表 进行排序(从小到大)
            for(int j=0;j<=i;j++){
                if(RectErrorList.at(j+1)<RectErrorList.at(j)){
                    RectErrorList.swap(j+1,j);
                    RectList.swap(j+1,j);
                    pointList.swap(j+1,j);
                    OutquadList.swap(j+1,j);
                }
            }
        //剔除误识别点
        while(RectList.size()>3)   RectList.removeLast();
        while(pointList.size()>3)  pointList.removeLast();
        while(OutquadList.size()>3)  OutquadList.removeLast();
    }
    else if(RectList.size()<3)
    {
        std::string text = "NULL";
        available=false;
        int font_face = cv::FONT_HERSHEY_COMPLEX;
        Point origin;
        double font_scale = 1;
        int thickness = 2;
        origin.x = 50; origin.y = 40;
        cv::putText(srcImgF, text, origin, font_face, font_scale, cv::Scalar(0, 0, 255), thickness, 8, 0);
    }
/*************************************重要代码段******************************************
*至此已有     RectList:   type=QList<RotatedRect>     size=3 //存储角元素最外层最小外接矩形
*            pointList:  type=QList<Point>           size=3 //存储角点中心
*            OutquadList:type=QList<vector<Point>>   size=3 //存储最外层拟合四边形角点
***************************************************************************************/

8.对QR码三个角元中心点进行排序(左上0,右上1,左下2)

我的思路:将三个中心点连接为三角形,分别计算每个对应内角,最接近90度的即为左上角点,然后以左上角点为起点,其余两个点为终点作出两个向量,计算两向量夹角,通过夹角正负确定其余两个角点
在这里插入图片描述

//对角点和矩形进行位置排序(左上:1   右上:2   左下:3)
        QList<float> angleList;
        for(int i=0;i<pointList.size();i++) //计算每个点的内角
        {
            float angle=0;
            Point thispoint=pointList.at(i); //本点
            Point otherpoint[2]; //其余两个点
            if(i==0){
                otherpoint[0] = pointList.at(1);
                otherpoint[1] = pointList.at(2);}
            else if(i==1){
                otherpoint[0] = pointList.at(0);
                otherpoint[1] = pointList.at(2);}
            else{
                otherpoint[0] = pointList.at(0);
                otherpoint[1] = pointList.at(1);}
            float a=sqrt( pow(thispoint.x-otherpoint[1].x,2) + \
                          pow(thispoint.y-otherpoint[1].y,2) ); //边a(otherpoint[0]的对边)
            float b=sqrt( pow(otherpoint[0].x-otherpoint[1].x,2) + \
                          pow(otherpoint[0].y-otherpoint[1].y,2) ); //边b(thispoint的对边)
            float c=sqrt( pow(thispoint.x-otherpoint[0].x,2) + \
                          pow(thispoint.y-otherpoint[0].y,2) ); //边c(otherpoint[1]的对边)
            angle=acos( ( a*a + c*c -b*b ) / (2*a*c) )*180/M_PI;
            angleList.append(angle);
        }
        for(int i=angleList.size()-2;i>=0;i--) //确定0号点位置
            for(int j=0;j<=i;j++)
            {
                float error1=fabs(angleList.at(j)-90);
                float error2=fabs(angleList.at(j+1)-90);
                if(error2 < error1){
                    angleList.swap(j+1,j);
                    pointList.swap(j+1,j);
                    RectList.swap(j+1,j);}
            }
        float Angle=getAngelOfTwoVector(pointList.at(1),pointList.at(2),pointList.at(0)); //以0为中心,2到1的角度
        if(Angle<0) //确定1,2号点位置
            pointList.swap(1,2);

9.对QR码进行精确定位

第一步:获取三个角元共12个角点(通过之前的拟合四边形结果)
第二步:计算出第四个角元的四个角点
第三步:计算出四个中心点,进一步计算出QR码中心点
第四步:计算出QR码正方向,并绘制箭头进行表示。
在这里插入图片描述

//粗略计算QR码中心
        Point2f P0;
        P0.x = ( pointList.at(1).x + pointList.at(2).x ) / 2;
        P0.y = ( pointList.at(1).y + pointList.at(2).y ) / 2;

        //取出OutquadList的4*3个角点 到 cornerPointList
        vector<Point> cornerPointList;
        for(int i=0;i<OutquadList.size();i++){
            vector<Point> points(OutquadList.at(i).size());
            points=OutquadList.at(i);
            for(int j=0;j<points.size();j++)
                cornerPointList.push_back(points[j]);
        }
        //针对cornerPointList的防抖算法
        //antiShake(cornerPointList);
        
        //按一定规则对这12个点重新排序
        sortNeartofar(cornerPointList,0,12,pointList.at(0));
        sortNeartofar(cornerPointList,4,12,pointList.at(1));
        vector<Point> cornerPointList_0;
        vector<Point> cornerPointList_1;
        vector<Point> cornerPointList_2;
        for(int i=0;i<4;i++){
            cornerPointList_0.push_back(cornerPointList[i]);
            cornerPointList_1.push_back(cornerPointList[i+4]);
            cornerPointList_2.push_back(cornerPointList[i+8]);
        }
        Point P0_0=getFarestPoint(cornerPointList_0,P0);
        Point P0_3=getNearestPoint(cornerPointList_0,P0);
        Point P0_2=getFarestPoint(cornerPointList_0,pointList.at(1));
        Point P0_1=getNearestPoint(cornerPointList_0,pointList.at(1));
        Point P1_1=getFarestPoint(cornerPointList_1,P0);
        Point P1_2=getNearestPoint(cornerPointList_1,P0);
        Point P1_3=getFarestPoint(cornerPointList_1,pointList.at(0));
        Point P1_0=getNearestPoint(cornerPointList_1,pointList.at(0));
        Point P2_2=getFarestPoint(cornerPointList_2,P0);
        Point P2_1=getNearestPoint(cornerPointList_2,P0);
        Point P2_3=getFarestPoint(cornerPointList_2,pointList.at(0));
        Point P2_0=getNearestPoint(cornerPointList_2,pointList.at(0));
        Point P3_0=CrossPoint(P1_0,P1_2,P2_0,P2_1);
        Point P3_1=CrossPoint(P1_1,P1_3,P2_0,P2_1);
        Point P3_2=CrossPoint(P1_0,P1_2,P2_2,P2_3);
        Point P3_3=CrossPoint(P1_1,P1_3,P2_2,P2_3);
        circle(srcImgF, P0_0, 4,Scalar(0,255,255),4,1);
        circle(srcImgF, P0_1, 4,Scalar(0,255,255),4,1);
        circle(srcImgF, P0_2, 4,Scalar(0,255,255),4,1);
        circle(srcImgF, P0_3, 4,Scalar(0,255,255),4,1);
        circle(srcImgF, P1_0, 4,Scalar(0,255,255),4,1);
        circle(srcImgF, P1_1, 4,Scalar(0,255,255),4,1);
        circle(srcImgF, P1_2, 4,Scalar(0,255,255),4,1);
        circle(srcImgF, P1_3, 4,Scalar(0,255,255),4,1);
        circle(srcImgF, P2_0, 4,Scalar(0,255,255),4,1);
        circle(srcImgF, P2_1, 4,Scalar(0,255,255),4,1);
        circle(srcImgF, P2_2, 4,Scalar(0,255,255),4,1);
        circle(srcImgF, P2_3, 4,Scalar(0,255,255),4,1);
        circle(srcImgF, P3_0, 2,Scalar(0,255,255),4,1);
        circle(srcImgF, P3_1, 2,Scalar(0,255,255),4,1);
        circle(srcImgF, P3_2, 2,Scalar(0,255,255),4,1);
        circle(srcImgF, P3_3, 2,Scalar(0,255,255),4,1);

        //计算4个中心点
        Point2f P0_C,P1_C,P2_C,P3_C;
        P0_C.x=float(P0_0.x+P0_1.x+P0_2.x+P0_3.x)/float(4);
        P0_C.y=float(P0_0.y+P0_1.y+P0_2.y+P0_3.y)/float(4);
        P1_C.x=float(P1_0.x+P1_1.x+P1_2.x+P1_3.x)/float(4);
        P1_C.y=float(P1_0.y+P1_1.y+P1_2.y+P1_3.y)/float(4);
        P2_C.x=float(P2_0.x+P2_1.x+P2_2.x+P2_3.x)/float(4);
        P2_C.y=float(P2_0.y+P2_1.y+P2_2.y+P2_3.y)/float(4);
        P3_C.x=float(P3_0.x+P3_1.x+P3_2.x+P3_3.x)/float(4);
        P3_C.y=float(P3_0.y+P3_1.y+P3_2.y+P3_3.y)/float(4);

        //重新赋值pointLists, size变化:size=3 -> size=4
        QList<Point2f> poin2ftList;
        poin2ftList.clear();
        poin2ftList.append(P0_C);
        poin2ftList.append(P1_C);
        poin2ftList.append(P2_C);
        poin2ftList.append(P3_C);

        //重新计算中点
        P0.x = ( poin2ftList.at(0).x + poin2ftList.at(1).x\
               + poin2ftList.at(2).x + poin2ftList.at(3).x) / 4;
        P0.y = ( poin2ftList.at(0).y + poin2ftList.at(1).y\
               + poin2ftList.at(2).y + poin2ftList.at(3).y) / 4;

        //绘制三角形连线
        line(srcImgF,poin2ftList.at(0),poin2ftList.at(1),Scalar(0,255,255),1);
        line(srcImgF,poin2ftList.at(1),poin2ftList.at(2),Scalar(0,255,255),2);
        line(srcImgF,poin2ftList.at(0),poin2ftList.at(2),Scalar(0,255,255),1);
        line(srcImgF,poin2ftList.at(0),poin2ftList.at(3),Scalar(0,255,255),2);
        line(srcImgF,poin2ftList.at(1),poin2ftList.at(3),Scalar(0,255,255),1);
        line(srcImgF,poin2ftList.at(2),poin2ftList.at(3),Scalar(0,255,255),1);

        //计算屏幕中心点
        Point2f PScreen0;
        PScreen0.x = float(cols) / float(2);
        PScreen0.y = float(rows) / float(2);

        //绘制二维码正方向箭头
        Point2f P0_C_1_C;
        P0_C_1_C.x = ( poin2ftList.at(0).x + poin2ftList.at(1).x ) / float(2);
        P0_C_1_C.y = ( poin2ftList.at(0).y + poin2ftList.at(1).y ) / float(2);
        Point2f PFront;
        PFront.x = ( P0_C_1_C.x + P0_C_1_C.x-P0.x );
        PFront.y = ( P0_C_1_C.y + P0_C_1_C.y-P0.y );
        drawArrow( srcImgF, P0, PFront, 17, 15, Scalar(0,255,0), 4, 4);

        Point2f PX; //X轴正方向
        PX.x = P0.x+10;
        PX.y = P0.y;

        float side01=sqrt( pow(P0_0.x-P1_1.x,2) + pow(P0_0.y-P1_1.y,2) ); //边01
        float side12=sqrt( pow(P1_1.x-P2_2.x,2) + pow(P1_1.y-P2_2.y,2) ); //边12
        float side23=sqrt( pow(P2_2.x-P3_3.x,2) + pow(P2_2.y-P3_3.y,2) ); //边23
        float side30=sqrt( pow(P3_3.x-P0_0.x,2) + pow(P3_3.y-P0_0.y,2) ); //边30
        float QRMeatrue=QRrealSize*4/(side01+side12+side23+side30);

        QRX=(P0.x-PScreen0.x)*QRMeatrue; //QR码在x方向偏差(像素)
        QRY=(PScreen0.y-P0.y)*QRMeatrue; //QR码在y方向偏差(像素)
        QRAngle=getAngelOfTwoVector(P0_C_1_C,PX,P0); //QR码正方向相对于X轴正方向的夹角
float getAngelOfTwoVector(Point2f pt1, Point2f pt2, Point2f c)
{
    float theta = atan2(pt1.x - c.x, pt1.y - c.y) - atan2(pt2.x - c.x, pt2.y - c.y);
    if (theta > CV_PI)
        theta -= 2 * CV_PI;
    if (theta < -CV_PI)
        theta += 2 * CV_PI;
    theta = theta * 180.0 / CV_PI;
    return theta;
}

10.对QR码进行内容识别

思路:
第一步:对QR码内容区域分别进行网格化处理,定位到每个色块中心点
第二步:判断利用之前步骤中二值化后的图像,计算每个色块的灰度(灰度值0代表色块为黑色,灰度值255代表色块为白色),获取QR码的二进制内容。
第三步:对QR码二进制内容进行解码,得到真正的内容。
这里我实现了一个简易的冗余校验功能,即对二维码信息重复四次,分别存储在QR码四个不同的区域,以此来保证数据的稳定性。
在这里插入图片描述
用到的函数:
1.任意四边形网格化函数

//网格计算函数 //左上为P0,P0-P3顺时针或逆时针排列  //mode=1,2:返回网格角点,网格中点  //upex等为向外扩展参数
vector<Point> Thread_CameraBelow::Gridding(Point P0,Point P1,Point P2,Point P3,\
                       int rows,int cols,int mode,\
                       int upex,int downex,int leftex,int rightex)
{
    vector<Point> pointvector01; //在P0和P1方向上创建等距点
    vector<Point> pointvector12; //在P1和P2方向上创建等距点
    vector<Point> pointvector32; //在P3和P2方向上创建等距点
    vector<Point> pointvector03; //在P0和P3方向上创建等距点
    vector<Point> pointvector; //
    if(mode==1) //返回格式为网格角点
    {
        for(int j=0-leftex; j<cols+1+rightex; j++){
            Point point;
            point.x = P0.x+(P1.x-P0.x)*j/cols;
            point.y = P0.y+(P1.y-P0.y)*j/cols;
            pointvector01.push_back(point);
        }
        for(int j=0-upex; j<rows+1+downex; j++){
            Point point;
            point.x = P1.x+(P2.x-P1.x)*j/rows;
            point.y = P1.y+(P2.y-P1.y)*j/rows;
            pointvector12.push_back(point);}
        for(int j=0-leftex; j<cols+1+rightex; j++){
            Point point;
            point.x = P3.x+(P2.x-P3.x)*j/cols;
            point.y = P3.y+(P2.y-P3.y)*j/cols;
            pointvector32.push_back(point);
        }
        for(int j=0-upex; j<rows+1+downex; j++){
            Point point;
            point.x = P0.x+(P3.x-P0.x)*j/rows;
            point.y = P0.y+(P3.y-P0.y)*j/rows;
            pointvector03.push_back(point);
        }
        for(int i=0; i<rows+1+downex+upex; i++) //依次求得交叉点
            for(int j=0;j<cols+1+rightex+leftex;j++){
                Point point=CrossPoint(pointvector01.at(j),pointvector32.at(j),\
                                       pointvector03.at(i),pointvector12.at(i));
                pointvector.push_back(point);
            }
    }
    else if(mode==2)
    {
        for(int j=(0-leftex)*2; j<(cols+rightex)*2+1; j++){
            Point point;
            point.x = P0.x+(P1.x-P0.x)*j/(cols*2);
            point.y = P0.y+(P1.y-P0.y)*j/(cols*2);
            pointvector01.push_back(point);
        }
        for(int j=(0-upex)*2; j<(rows+downex)*2+1; j++){
            Point point;
            point.x = P1.x+(P2.x-P1.x)*j/(rows*2);
            point.y = P1.y+(P2.y-P1.y)*j/(rows*2);
            pointvector12.push_back(point);}
        for(int j=(0-leftex)*2; j<(cols+rightex)*2+1; j++){
            Point point;
            point.x = P3.x+(P2.x-P3.x)*j/(cols*2);
            point.y = P3.y+(P2.y-P3.y)*j/(cols*2);
            pointvector32.push_back(point);
        }
        for(int j=(0-upex)*2; j<(rows+downex)*2+1; j++){
            Point point;
            point.x = P0.x+(P3.x-P0.x)*j/(rows*2);
            point.y = P0.y+(P3.y-P0.y)*j/(rows*2);
            pointvector03.push_back(point);
        }
        for(int i=0; i<(rows+downex+upex)*2+1; i++) //依次求得交叉点
            for(int j=0;j<(cols+rightex+leftex)*2+1;j++){
                if( i%2==1 && j%2==1 ){
                    Point point=CrossPoint(pointvector01.at(j),pointvector32.at(j),\
                                           pointvector03.at(i),pointvector12.at(i));
                    pointvector.push_back(point);
                }
            }
    }
    return pointvector;
}

2.交点计算函数

//计算两直线交点
Point Thread_CameraBelow::CrossPoint(Point P1, Point P2, Point P3, Point P4)
{
    Point pt;
    double x1=P1.x,y1=P1.y;
    double x2=P2.x,y2=P2.y;
    double x3=P3.x,y3=P3.y;
    double x4=P4.x,y4=P4.y;
    double D = (x1-x2)*(y3-y4)-(y1-y2)*(x3-x4);
    if (D == 0){
        pt.x=0;
        pt.y=0;
    }
    else{
        pt.x = ((x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4))/D;
        pt.y = ((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4))/D;
    }
    return pt;
}

11.小结

在家隔离的几天,闲着无聊写出了这个QR码识别算法,还有一些有待改进的地方:如只考虑了图像翻转,而没有考虑图像畸变的影响等等,有一起学习的小伙伴可以联系我@_@1301951021。
最近武汉肺炎正当爆发期,形式是相当严峻,希望我们国家可以早日度过难关,雨过定会天晴!

2020.2.7于家中

发布了18 篇原创文章 · 获赞 5 · 访问量 6683

猜你喜欢

转载自blog.csdn.net/Sun_tian/article/details/104211859