user3733642
user3733642

Reputation: 11

Android [opencv] crop plate

I need help with android opencv 3.0 I want to send to my OCR (tessract) image but now I'm sending this kind of: enter image description here

and tessract recognise this but efficiency is too bad ... I want to crop this plate using opencv and get sth like this: enter image description here

Could you help me with this issue? I used GaussianBlur and threshold to get this is image which I have right now. This image with black background is in new Mat.

Upvotes: 1

Views: 525

Answers (1)

Daniel Kuta
Daniel Kuta

Reputation: 1634

I think, you should use findContours function. This code below find biggest contour in source Mat, in your case this can be plate. In r variable you have rectangle with your ROI. Then you can use Imgproc.findContours to each char, because Imgproc.findContours return Rect.

List<MatOfPoint> contours = new ArrayList<>();

private void findBiggestContour(Mat src) {
    contours.clear();

    Imgproc.findContours(src, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);

    double maxArea = 0;
    int idxMax = 0;
    for (int i = 0; i < contours.size(); i++) {
        double rozmiar = Math.abs(Imgproc.contourArea(contours.get(i)));
        if (rozmiar > maxArea) {
            maxArea = rozmiar;
            idxMax = i;
        }
    }

    Imgproc.drawContours(mRgba, contours, idxMax, new Scalar(100, 255, 99, 255), Core.FILLED);

    if (contours.size() >= 1) {
        Rect r = Imgproc.boundingRect(contours.get(idxMax));
        Imgproc.rectangle(mRgba, r.tl(), r.br(), new Scalar(255, 0, 0, 255), 3, 8, 0); //draw rectangle
    }
}

Upvotes: 1

Related Questions