基于opencv数码管数字识别

int shiBieShuMaGuan(Mat &img)
{
    int num = 0;
    Mat roi,binary;
    //大小设置为模板大小
    resize(img,roi,Size(20,28));
    threshold(roi,binary,200,255,THRESH_BINARY);
//    imshow("ss",binary);

    int count[7] = {0};
    for(int i = 0; i < 7; i++){
//        cout<<rectShuMaGuan[i].size();
        Mat roi7 = binary(rectShuMaGuan[i]);
//        cout<<roi7.rows<<" "<<roi7.cols<<endl;
//        string s;
//        stringstream ss;
//        ss<<i;
//        ss>>s;
//        imshow(s,roi7);
        Mat_<unsigned char>::iterator itbegin = roi7.begin<uchar>();
        Mat_<unsigned char>::iterator itend = roi7.end<uchar>();
        for(; itbegin != itend; itbegin++){
            count[i] += *itbegin > 200 ? 1 : 0;
        }
//        cout<<count[i]<<endl;
    }
//    cout<<endl;
/************************************************************************
 * 数码管字符图像大小为 20cols * 28rows
 * 分为6个特征区域,检测区域中白色像素点个数,根据白色像素点个数判别
 *      ###11
 *      00#22
 *      00#22
 *      #666#
 *      55#33
 *      55#33
 *      44###
 * 每个字符表示一个4*4的小矩形
************************************************************************/
    if(count[1] > 5){
        if(count[4] > 5){
            if(count[6] > 5){
                if(count[3] > 5){
                    if(count[0] > 5){
                        if(count[2] > 5){
                            if(count[5] > 5){
                                num = 8;
                            }else{
                                num = 9;
                            }
                        }else{
                            if(count[5] > 5){
                                num = 6;
                            }else{
                                num = 5;
                            }
                        }
                    }else{
                        num = 3;
                    }
                }else{
                    num = 2;
                }
            }else{
                num = 0;
            }
        }else{
            num = 7;
        }
    }else {
        num = 4;
    }

    return num;
}

猜你喜欢

转载自blog.csdn.net/qq_34359028/article/details/78846902