2016-04-30 9 views
9

式を以下に与えられるのOpenCVを用いてリアルタイムに検出された顔の焦点距離まず距離を見つけたいですリアルタイムに検出された顔の上に表示される矩形の画素値(P)を見つけるために:焦点距離のためにアンドロイド

する画像携帯電話の周りに描かれた矩形の幅を見つけたい:

enter image description here

これはPythonとOpenCVを使って行われましたが、Java OpenCVで実装する方法については混乱しています。画像で

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

+0

あなたはJavaでこれを実装するために使用されているコードからいくつかの行を追加してください:そこにいくつかのソリューションもよいが、輪郭での作業を通じて行う1次のコードのようなものであることができます達成するために、このために

。 – abggcv

答えて

0

あなたはすでに、正方形の幅を持っているので、あなたは携帯電話の周りの正方形を描いた追加。私があなたの質問から理解しているのは、あなたが電話の周りに真の矩形を得たいということです。

// 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); 
関連する問題