Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to find the Focal length first then distance of face detected in real time using opencv android

Formula for focal length is given below:

F = (P x D) / W

But I am unable to find pixel value (P) of the rectangle that appears on the detected face in real time:

Want to find the width of rectangle drawn around the mobile phone in the image:

enter image description here

It was done using Python and OpenCV but I am confused as to how to implement it in Java OpenCV.

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

like image 878
Kamran Alam Avatar asked Apr 30 '16 12:04

Kamran Alam


People also ask

What algorithm is used to detect face in OpenCV?

The haar like cascade algorithm is used for face detection. There are various algorithms for face recognition, but LBPH is easy and popular algorithm among them. It generally focuses on the local features in the image.

How does OpenCV calculate distance?

Steps for Distance Estimation: Capture Reference Image: Measure the distance from the object(face) to the camera, capture a Reference image and note down the measured distance. Measure the object (face) width, make sure that measurement units are kept for reference image and object(face) width. Mine Reference Image.

How does OpenCV face recognition work?

Various face detection algorithms are there but the Viola-Jones Algorithm is the oldest method that is also used today. Face detection is generally the first step towards many face-related applications like face recognition or face verification. But, face detection has very useful applications.


1 Answers

In the image you added you have drawn a square around the phone so you already have the width of the square. What I understand from your question is that you want to get the real rectangle around the phone.

For this to be achieved there can be several solutions but one done through working with contours is like the following code:

// 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);
like image 143
Ali Asgari Avatar answered Oct 23 '22 02:10

Ali Asgari