Friday, November 15, 2019

How to start Opencv real time image processing conditionally using Android?

I'm developing an Android application using Opencv to perform some heavy image processing including detecting the largest contour, crop the detected contour, apply segmentation logic, and match similarities of each segmented contour with a reference object array.



I'm done with the processing logic in real time with an fps of 3 and the treatment time is 0.4 second as an average which is good in my case.



The problem is that the project will be used in an industry, and I want to start processing the frame only when the product is in the camera visual field.



I've done some sort of motion detection to detect if there is some contour moving and then start the algorithm but the industry machine carpet is also moving so this approach won't work.




Here is the code for motion detection part :



   @Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
long e1 = Core.getTickCount();
contours.clear();
//gray frame because it requires less resource to process
mGray = inputFrame.gray();

//this function converts the gray frame into the correct RGB format for the BackgroundSubtractorMOG apply function

Imgproc.cvtColor(mGray, mRgb, Imgproc.COLOR_GRAY2RGB);

//apply detects objects moving and produces a foreground mask
//the lRate updates dynamically dependent upon seekbar changes
sub.apply(mRgb, mFGMask, lRate);

//erode and dilate are used to remove noise from the foreground mask
Imgproc.erode(mFGMask, mFGMask, new Mat());
Imgproc.dilate(mFGMask, mFGMask, new Mat());


//drawing contours around the objects by first called findContours and then calling drawContours
//RETR_EXTERNAL retrieves only external contours
//CHAIN_APPROX_NONE detects all pixels for each contour
Imgproc.findContours(mFGMask, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);

//draws all the contours in red with thickness of 2
Imgproc.drawContours(mRgb, contours, -1, new Scalar(255, 0, 0), 2);

long e2 = Core.getTickCount();
long e = e2 - e1;

double time = e / Core.getTickFrequency();

Log.d("timeTAG", "" + contours.size());


return mRgb;
}


What do you suggest as a solution for this problem ?

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...