I am a newbie in android, and open CV both. However, I am trying to take an image from the camera, convert it into the desired format, and pass it to the tflite model.
Code for capturing image, and applying image processing to it.
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
float mh = mRGBA.height();
float cw = (float) Resources.getSystem().getDisplayMetrics().widthPixels;
float scale = mh / cw * 0.7f;
mRGBA = inputFrame.rgba();
frame = classifier.processMat(mRGBA);
Mat temp = new Mat();
Mat temp3= new Mat();
if (!isDebug) {
if (counter == CLASSIFY_INTERVAL) {
Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2GRAY);
Core.rotate(frame, frame, Core.ROTATE_90_CLOCKWISE);
Imgproc.GaussianBlur(frame, frame, new Size(5, 5), 0);
Imgproc.adaptiveThreshold(frame, frame, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV , 3, 2);
Bitmap bmsp = null;
runInterpreter();
counter = 0;
} else {
counter++;
}
}
Imgproc.rectangle(mRGBA,
new Point(mRGBA.cols() / 2f - (mRGBA.cols() * scale / 2),
mRGBA.rows() / 2f - (mRGBA.cols() * scale / 2)),
new Point(mRGBA.cols() / 2f + (mRGBA.cols() * scale / 2),
mRGBA.rows() / 2f + (mRGBA.cols() * scale / 2)),
new Scalar(0, 255, 0), 1);
if (isEdge) {
mRGBA = classifier.debugMat(mRGBA);
}
System.gc();
return mRGBA;
}
My output looks like this image, but I want the hand to be filled with white color before passing it to model. Can somebody suggest?


The main issue is that the result of
adaptiveThresholdhas gaps in the external edge, so you can't use it as input tofindContours.I think that using
GaussianBlurmakes things worst, because it blurs the edge between the hand and the background.You may use the following stages:
adaptiveThresholdwith large kernel size (I used size 51).Using a large kernel size, keeps a thick edge line without gaps (except from a small gap at the fingernail).
Find the contour with the maximum area.
There is a problem: the inner part of the hand is not filled due to the weird shape of the contour.
Find the center of the contour, and fill it using
floodFill.Here is a Python code sample:
Results:
thres_gray:resbeforefloodFill:resafterfloodFill:JAVA implementation: