Android智能识别 - 银行卡数字识别

作者: 你也不知道 | 来源:发表于2020-07-07 13:03 被阅读0次

    接着上一篇文章 《Android智能识别 - 银行卡区域裁剪》 来说,上一次我们已经截取到了银行卡的数字区域,那么这次我们基于上次截取到的卡号区域,来进行数字识别。

    有了上面这一块区域之后,我们首先要做的肯定需要转为灰度然后进行二值化。

        // 转为灰度
        Mat gray;
        cvtColor(mat, gray, COLOR_BGRA2GRAY);
        Mat binary;
        // 可以用 THRESH_OTSU , 也可以手动指定
        threshold(gray, binary, 39, 255, THRESH_BINARY);
        // 进行降噪
        Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
        morphologyEx(binary, binary, MORPH_CLOSE, kernel);
        imwrite("/storage/emulated/0/ocr/card_number_binary.jpg", binary);
    

    上面的效果看上去还是有很多的干扰,我们对其进行轮廓发现,把那些不需要的干扰进行填充。

      vector<vector<Point> > contours;
        Mat binary_not = binary.clone();
        // 取反, 白黑 -> 黑白
        bitwise_not(binary_not, binary_not);
        // 轮廓查找
        findContours(binary_not, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
        int minh = binary.rows / 4;
        int matArea = mat.rows * mat.cols;
        for (int i = 0; i < contours.size(); i++) {
            Rect roi = boundingRect(contours[i]);
            int area = roi.width * roi.height;
            // 面积太小的过滤
            if (area < matArea / 200) {
                drawContours(binary, contours, i, Scalar(255), -1);
                continue;
            }
            // 高度太低的过滤
            if (roi.height <= minh) {
                drawContours(binary, contours, i, Scalar(255), -1);
            }
        }
    

    有了上面这个 Mat 之后,我们首先对其进行字符分割,然后进行特征提取匹配就好了,但是值得提醒的是,我们需要对粘连字符进行分割,尽管我的这张卡没有粘连字符的情况。

        // 字符的分割
        binary.copyTo(binary_not);
        bitwise_not(binary_not, binary_not);
        findContours(binary_not, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
        Rect rects[contours.size()];
        // 创建新的 mat 用来画数字
        Mat contours_mat(binary_not.size(), CV_8UC1, Scalar(255));
        // 记录数字最小宽度(粘连字符)
        int minw = mat.cols;
        for (int i = 0; i < contours.size(); i++) {
            rects[i] = boundingRect(contours[i]);
            drawContours(contours_mat, contours, i, Scalar(0), 1);
            minw = min(minw, rects[i].width);
        }
        imwrite("/storage/emulated/0/ocr/card_number_binary.jpg", contours_mat);
        // 防止循序错乱,进行重新排序
        for (int i = 0; i < contours.size(); ++i) {
            for (int j = 0; j < contours.size() - i - 1; ++j) {
                if (rects[j].x > rects[j + 1].x) {
                    swap(rects[j], rects[j + 1]);
                }
            }
        }
        numbers.clear();
        // 最小宽度的两倍
        minw = minw * 2;
        for (int i = 0; i < contours.size(); ++i) {
            if (minw < rects[i].width) {
                // 大于最小宽度的两倍,判断是粘连字符,对粘连字符进行分割
                Mat number(binary, rects[i]);
                int cols_pos = get_split_cols_pos(number);
                Rect rect_left = Rect(0, 0, cols_pos - 1, rects[i].height);
                numbers.push_back(Mat(contours_mat, rect_left));
                Rect rect_right = Rect(cols_pos + 1, 0, rects[i].width, rects[i].height);
                numbers.push_back(Mat(contours_mat, rect_right));
            } else {
                Mat number(contours_mat, rects[i]);
                numbers.push_back(number);
            }
        }
    
    // 获取粘连位置
    int co1::get_split_cols_pos(const Mat &mat) {
        // 中心位置
        int mx = mat.cols / 2;
        // 高度
        int height = mat.rows;
        // 左右 1/4 去扫描
        int startx = mx - mx/4;
        int endx = mx + mx/4;
        // 粘连位置
        int clos_pos = mx;
        int c = 0;
        // 最小的黑色像素
        int minh = mat.rows;
        for (int col = startx; col <= endx; col++) {
            // 这一列有多少个黑色像素
            int total = 0;
            for (int row = 0; row < height; row++) {
                c = mat.at<Vec3b>(row, col)[0];
                if (c == 0) {
                    total++;
                }
            }
            // 保存当前列
            if (total < minh) {
                minh = total;
                clos_pos = col;
            }
        }
        return clos_pos;
    }
    

    视频地址:https://pan.baidu.com/s/1kC4C80z_DGClm16IRmzBPw
    视频密码:jga0

    相关文章

      网友评论

        本文标题:Android智能识别 - 银行卡数字识别

        本文链接:https://www.haomeiwen.com/subject/oaeaqktx.html