# Revision history [back]

### 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:

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?

 2 retagged sturkmen 6772 ●3 ●48 ●79 https://github.com/stu...

### 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:

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?