使用OpenFrameworks,OpenCV和Box2D,我能够以良好的帧速率实现它 . 使用Android似乎是一项更复杂的任务(部分原因是因为我是JAVA新手) .
这就是我的开始:
- 使用“OpenCV样本 - 图像处理”并删除除“canny”效果之外的所有内容,这样可以生成漂亮的黑白图像,非常适合查找轮廓 .
public Mat onCameraFrame(CvCameraViewFrame inputFrame)
{
mRgba = inputFrame.rgba();
Imgproc.Canny(mRgbaInnerWindow, mIntermediateMat, 50, 100);
Imgproc.cvtColor(mIntermediateMat, mRgbaInnerWindow, Imgproc.COLOR_GRAY2BGRA, 4);
return mRgba;
}
- 从“OpenCV Sample - color-blob-detection”中我 grab 了在Mat中找到轮廓的逻辑:
// These two lines are actually in the function onCameraViewStarted
mHierarchy = new Mat();
CONTOUR_COLOR = new Scalar(255,0,0,255);
// These lines are in function onCameraFrame
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(mRgbaInnerWindow, contours, mHierarchy, Imgproc.RETR_EXTERNAL,
Imgproc.CHAIN_APPROX_SIMPLE);
Imgproc.drawContours(mIntermediateMat, contours, -1, CONTOUR_COLOR);
所以,我当前的函数看起来像这样,它不起作用:
public Mat onCameraFrame(CvCameraViewFrame inputFrame)
{
mRgba = inputFrame.rgba();
if ((mRgbaInnerWindow == null) || (mGrayInnerWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height))
CreateAuxiliaryMats();
Imgproc.Canny(mRgbaInnerWindow, mIntermediateMat, 50, 100);
//Imgproc.cvtColor(mIntermediateMat, mRgbaInnerWindow, Imgproc.COLOR_GRAY2BGRA, 4);
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(mRgbaInnerWindow, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
//Imgproc.drawContours(mIntermediateMat, contours, -1, CONTOUR_COLOR);
return mRgba;
}
- 现在,这是我没有使用正确的尺寸或将Mat转换为正确的颜色空间的地方 . 这篇文章有一些见解,但我不正确:OpenCV on Android findContours throws Exception
2 回答
嗨,我也是openCV的新手,但是这段代码可能有帮助,
我知道这可能不是实现这一目标的最佳方式,但我们都在这里学习新方法:)
简答:您必须使用
Canny
的结果作为findContours
的输入(第一个参数),即