OpenCV-Android平台应用实战 - 银行卡卡号识别(07、二值化与字符分割)

思路二值化方法选择指定阈值自动阈值( OTSU 和 Triangle )轮廓发现轮廓提取干扰区域过滤粘连字符分割解决字符粘连问题—粘连字符分割程序代码运行截图...
摘要由CSDN通过智能技术生成

思路

  • 二值化方法选择
    指定阈值
    自动阈值( OTSU 和 Triangle )
  • 轮廓发现
    轮廓提取
    干扰区域过滤
  • 粘连字符分割
    解决字符粘连问题—粘连字符分割

程序代码

  • 完善算法类: TextImageProcessor.java
package com.example.bankcardrec.ocr.algo;

import android.net.Uri;
import android.os.Environment;
import android.util.Log;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * Created by Administrator on 2019/4/2/002.
 */

public class TextImageProcessor {
   
    private String TAG = "OCR-Algorithm";

    //获取分割字符 Mat 对象
    public List<Mat> splitNumberBlock(Mat textImage) {
   
//    public Mat splitNumberBlock(Mat textImage) {//暂时性查看中间结果
        List<Mat> numberImgs = new ArrayList<>();//分割到的 Mat 对象
        Mat gray = new Mat();
        Mat binary = new Mat();

        // gray
        Imgproc.cvtColor(textImage, gray, Imgproc.COLOR_BGR2GRAY);

        // binary and noise remove
        Imgproc.threshold(gray, binary, 0, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);
        //Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
        //Imgproc.morphologyEx(binary, binary, Imgproc.MORPH_CLOSE, kernel);//形态学操作 闭操作 去除噪点

        // noise block fill
        List<MatOfPoint> contours = new ArrayList<>();
        Mat hireachy = new Mat();
        Mat contourBin = binary.clone();//不要改动原来的二值图像,重新复制一份新的Mat对象
        Core.bitwise_not(contourBin, contourBin);//取反操作
        Imgproc.findContours(contourBin, contours, hireachy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));
        int minh = binary.rows() / 3;
        for(int i=0; i<contours.size(); i++) {
   
            Rect roi = Imgproc.boundingRect(contours.get(i));
            double area = Imgproc.contourArea(contours.get(i)); //获取轮廓的区域面积
            if(area < 100) {
   //如果面积小于100就将它填充起来(用背景填充),以去除噪点
                Imgproc.drawContours(binary, contours, i, new Scalar(255), -1);
                continue;
            }
            if(roi.height <= minh) {
   //轮廓小于图片高度的 1/3 也填充起来
                Imgproc.drawContours(binary, contours, i, new Scalar(255), -1);
                continue;
            }
        }

//        //查看中间结果
//        Mat result = new Mat();
//        Imgproc.cvtColor(binary,result,Imgproc.COLOR_GRAY2BGR);
//        return result;

        // split digit text  分割字符
        contours.clear();
        binary.copyTo(contourBin);
        Core.bitwise_not(contourBin, contourBin);
        Imgproc.findContours(contourBin, contours, hireachy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));
        Rect[] textBoxes = new Rect[contours.size()];
        int index = 0;
        int minWidth = 1000000;//假设一个最小宽度(后面会进行更正)
        Mat contoursImage = new Mat(contourBin.size(), CvType.CV_8UC1);//画出结果,方便查看
        contoursImage
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值