我能够使用C++检测图像中的轮廓。然而,我想用java语言在安卓系统中转换它。所以,我用安卓工作室制作了它。(部分原因是我是JAVA新手)
然后我发现了一些问题,当我运行它时,我发现这个代码有一个错误问题:
如果(contours[coutroIdx].size()>100)
在这个主程序中。
@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;
}
}
希望有人在这方面有一些经验。谢谢
使用区域概念
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());
}