卡号区域发现与截取
- RGB与HSV色彩空间:色彩空间转换,通过 cvtColor() 实现。
- inRange过滤
- 比例与定位计算
程序代码
- 完善算法部分 (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;
public class TextImageProcessor {
private String TAG = "OCR-Algorithm";
public Mat findCardNumBlock(Mat card) {
Mat hsv = new Mat();
Mat binary = new Mat();
Imgproc.cvtColor(card, hsv, Imgproc.COLOR_BGR2HSV);
Core.inRange(hsv, new Scalar(30, 40, 45), new Scalar(180, 255, 255), binary);
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5, 5));
Imgproc.morphologyEx(binary, binary, Imgproc.MORPH_OPEN, kernel);
List<MatOfPoint> contours = new ArrayList<>();
Mat hireachy = new Mat();
Imgproc.findContours(binary, contours, hireachy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));
int offsetx = binary.cols()/3