java - 想先找到焦距,然后使用 opencv android 实时检测到人脸的距离

标签 java android opencv camera

焦距公式如下:

F = (P x D)/W

但是我无法实时找到检测到的面部上出现的矩形的像素值(P):

想求出图中手机周围画出的矩形的宽度:

enter image description here

它是使用 Python 和 OpenCV 完成的,但我对如何在 Java OpenCV 中实现它感到困惑。

http://www.pyimagesearch.com/2015/01/19/find-distance-camera-objectmarker-using-python-opencv/

最佳答案

在您添加的图像中,您在手机周围绘制了一个正方形,因此您已经有了正方形的宽度。我从你的问题中了解到,你想要在手机周围获得真正的矩形。

要实现这一点,可以有多种解决方案,但通过使用轮廓来完成的一种解决方案类似于以下代码:

// localImage would be the cropped image of the square you have drawn,
// the global image is the original image and phoneSquare is the Rect you 
// have drawn
localImage = new Mat(globalImage, phoneSqure).clone();

// make the phone black and surroundings white
Imgproc.threshold(localImage, localImage, 127, 255, Imgproc.THRESH_OTSU + Imgproc.THRESH_BINARY_INV);

// get contours
ArrayList<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.findContours(canny, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_NONE);

// sort contours by size and get the biggest which is assumed to be the outer contour of the phone
contours.sort(new Comparator<MatOfPoint>() {
                                 @Override
                                 public int compare(MatOfPoint o1, MatOfPoint o2) {
                                     return (int) Math.signum(o2.size().area() - o1.size().area());
                                 }
                             });
MatOfPoints biggestContour = contours.get(contours.size() - 1);
// get the bounding rectangle of the phone, the you can get the width
Rect whatYouWant = Imgproc.boundingRect(biggestContour);

关于java - 想先找到焦距,然后使用 opencv android 实时检测到人脸的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36954945/

相关文章:

java - 如何解决 ,"AWT-EventQueue-0"java.lang.IndexOutOfBoundsException : Index: 0, 大小:0

java - 带有 Jetty 的 AbstractAnnotationConfigDispatcherServletInitializer

java - 如何更改记录的时间戳?

java - Android DrawText 不适用于 SurfaceView

android - MediaPlayer 在 Activity 之间开始-停止-暂停

opencv - 使用 OpenCV 进行基于特征检测的定位

java - 如何阻止 Maven 弄乱我的目录布局及其属性?

android - “tel” 、 “sms” 和 “mailto” 在升级到 cordova 3.6.3 后不再在 Android 中工作

opencv - 立体相机校准错误

opencv - 为 python3 安装 OpenCV