Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to remove background of a selected contour

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    Mat srcMat = inputFrame.rgba();
    Mat processedMat = new Mat();
    Imgproc.cvtColor(srcMat, processedMat, Imgproc.COLOR_BGR2GRAY);
    Imgproc.GaussianBlur(processedMat, processedMat, new Size(3, 3), 0);
    Imgproc.threshold(processedMat, processedMat, 127, 255, Imgproc.THRESH_BINARY);
    List<MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(processedMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
    Log.e(TAG, "ContourSize: " + contours.size());
    double maxVal = 0;
    int maxValIdx = 0;
    for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) {
        double contourArea = Imgproc.contourArea(contours.get(contourIdx));
        if (maxVal < contourArea) {
            maxVal = contourArea;
            maxValIdx = contourIdx;
        }
    }
    Imgproc.drawContours(srcMat, contours, maxValIdx, new Scalar(0,255,0),  -2);
    if (!contours.isEmpty()) {
        Log.e("largestContour", "" + contours.get(maxValIdx));
        Rect rect = Imgproc.boundingRect(contours.get(maxValIdx));
        srcMat.submat(rect);
    }
        return srcMat; }

Input Image: https://ibin.co/3irmfXKjYocz.jpg

Output:

image description

As you can see I have applied .submat but I'm not sure why is it masking the inners of the contour instead of the outer surface. Moreover, filtering the largest contour is also not consistent. Any ideas on how can I make it more robust based on the dimensions of the ticket as my aim is to scan tickets with very same dimensions.

As a newbie OpenCV enthusiast, I want someone to guide me with my final mission here, which is to get the year, month, date, and time from the calender in the ticket and show it in text form. The ticket will have punched holes as markings. So, what I have in mind is to track each (hole) blob and extract it along with it's surrounding digits to know which number it was based on its surrounding, and do this to all of the blobs (holes) with some hierarchy in place to know if the extraction was from the 'year' field or month or day field respectively. Or is there any other better approach for this?

How to remove background of a selected contour

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    Mat srcMat = inputFrame.rgba();
    Mat processedMat = new Mat();
    Imgproc.cvtColor(srcMat, processedMat, Imgproc.COLOR_BGR2GRAY);
    Imgproc.GaussianBlur(processedMat, processedMat, new Size(3, 3), 0);
    Imgproc.threshold(processedMat, processedMat, 127, 255, Imgproc.THRESH_BINARY);
    List<MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(processedMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
    Log.e(TAG, "ContourSize: " + contours.size());
    double maxVal = 0;
    int maxValIdx = 0;
    for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) {
        double contourArea = Imgproc.contourArea(contours.get(contourIdx));
        if (maxVal < contourArea) {
            maxVal = contourArea;
            maxValIdx = contourIdx;
        }
    }
    Imgproc.drawContours(srcMat, contours, maxValIdx, new Scalar(0,255,0),  -2);
    if (!contours.isEmpty()) {
        Log.e("largestContour", "" + contours.get(maxValIdx));
        Rect rect = Imgproc.boundingRect(contours.get(maxValIdx));
        srcMat.submat(rect);
    }
        return srcMat; }

Input Image: https://ibin.co/3irmfXKjYocz.jpg

Output:

image description

As you can see I have applied .submat but I'm not sure why is it masking the inners of the contour instead of the outer surface. Moreover, filtering the largest contour is also not consistent. Any ideas on how can I make it more robust based on the dimensions of the ticket as my aim is to scan tickets with very same dimensions.

As a newbie OpenCV enthusiast, I want someone to guide me with my final mission here, which is to get the year, month, date, and time from the calender in the ticket and show it in text form. The ticket will have punched holes as markings. So, what I have in mind is to track each (hole) blob and extract it along with it's surrounding digits to know which number it was based on its surrounding, and do this to all of the blobs (holes) with some hierarchy in place to know if the extraction was from the 'year' field or month or day field respectively. Or is there any other better approach for this?