Issue
I am able to detect the Contour in an image using C++. However, I want to convert it in Android using java lang. So, i make it using android studio. (partly because I am a JAVA newbie)
Then i found some problems, When i run it, i had a error problems with this code :
if(contours[contourIdx].size()>100)
in this main program.
@Override
public void onCameraViewStarted(int width, int height) {
mRgba = new Mat(height, width, CvType.CV_8UC4);
mHsv = new Mat(height,width,CvType.CV_8UC3);
hierarchy = new Mat();
mHsvMask = new Mat();
mDilated = new Mat(height, width, CvType.CV_8UC4);
}
@Override
public void onCameraViewStopped() {
mRgba.release();
mHsv.release();
mHsvMask.release();
mDilated.release();
hierarchy.release();
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mRgba =inputFrame.rgba();
mHsv = new Mat();
Imgproc.cvtColor(mRgba, mHsv, Imgproc.COLOR_RGB2HSV, 3);
hierarchy.release();
Scalar lowerThreshold = new Scalar ( 120, 100, 100 );
Scalar upperThreshold = new Scalar ( 179, 255, 255 );
Core.inRange ( mHsv, lowerThreshold , upperThreshold, mHsvMask );
Imgproc.dilate ( mHsvMask, mDilated, new Mat() );
Imgproc.findContours(mDilated, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
for ( int contourIdx=0; contourIdx < contours.size(); contourIdx++ )
{
if(contours[contourIdx].size()>100) // Minimum size allowed for consideration
{
Imgproc.drawContours ( mRgba, contours, contourIdx, new Scalar(0,255,0) , -5);
}
}
return mRgba;
}
}
Hopefully, someone has some experience in this. Thanks..
Solution
Use the area concept
if(Imgproc.contourArea(contours.get(contourIdx))>100) {
Imgproc.drawContours(mRgba, contours, contourIdx, new Scalar(Math.random() * 255, Math.random() * 255, Math.random() * 255), 1, 8, hierarchy, 0, new Point());
}
Answered By - user5766922
Answer Checked By - Marie Seifert (JavaFixing Admin)