我正在尝试开发一种可检测某些物体(例如人,车辆和树木)的应用程序。首先,我尝试移植OpenCV pedestrian sample,但是帧速率很低,但误报很多。
由于我刚开始使用OpenCV并且对C++也不太了解,所以我可能做出了错误的解释和昂贵的计算。
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
Mat frame = inputFrame.rgba();
float minScale = 0.4f, maxScale = 5; /* minimum and maximum scale to detect */
int totalScales = 55; /* preferred number of scales between min and max */
int threshold = -1; /* detections with score less then threshold will be ignored */
HOGDescriptor hog = new HOGDescriptor();
hog.setSVMDetector(HOGDescriptor.getDefaultPeopleDetector());
MatOfRect foundLocations = new MatOfRect();
MatOfDouble foundWeights = new MatOfDouble();
Mat tempMat = new Mat(frame.rows(), frame.cols(), CvType.CV_8UC3);
Imgproc.cvtColor(frame, tempMat, Imgproc.COLOR_RGBA2RGB);
hog.detectMultiScale(tempMat, foundLocations, foundWeights, 1.5, new Size(8,8),
new Size(32, 32), 1.05, 2, false);
Vector<Rect> foundLocationsFilteredList = new Vector<Rect>();
filterRects(foundLocations.toList(), foundLocationsFilteredList);
for (Rect foundLocation : foundLocationsFilteredList) {
Core.rectangle(frame, foundLocation.tl(), foundLocation.br(), new Scalar(0, 255, 0), 3);
}
tempMat.release();
return frame;
}
private final void filterRects(List<Rect> candidates, List<Rect> objects) {
for (int i = 0; i < candidates.size(); ++i) {
Rect r = candidates.get(i);
int j;
for (j = 0; j < candidates.size(); ++j) {
if (j != i && r.equals(candidates.get(j)))
break;
}
if (j == candidates.size())
objects.add(r);
}
}
最佳答案
JNI无疑加快了这一进程。在OpenCV4Android bundle 包中有一个JNI示例。我已经在Android上使用OpenCV和JNI编写了一些程序,对我来说效果很好。这是面部检测示例。用您自己的物体探测器更换探测器。
对于各种检测器,在参数中指定检测多个或单个对象。转到文档。大多数情况下,检测器默认会返回多个结果。
编辑
这是使用OpenCV的JNI的示例。最初的跟踪器是C++,我将其改编成Android。欢迎叉。
Link to Project
关于android - 使用OpenCV在Android上进行多对象检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18303553/