使用霍夫检测瞳孔的最佳参数? java opencv

时间:2017-04-10 14:57:18

标签: java opencv detection hough-transform iris-recognition

--------------阅读下面的编辑--------------- 我试图在各种图像中检测瞳孔和虹膜的边缘。我正在改变参数等,但我只能设法得到一个正确的虹膜/瞳孔轮廓,或者在背景中得到不必要的轮廓,或者根本没有。是一些特定的参数,我应该尝试并获得正确的轮廓。或者有一种方法可以将图像裁剪到眼睛,因此系统可以专注于那部分吗?

这是我的UPDATED方法:

private void findPupilIris() throws IOException {
    //converts and saves image in grayscale

    Mat newimg = Imgcodecs.imread("/Users/.../pic.jpg");
    Mat des = new Mat(newimg.rows(), newimg.cols(), newimg.type());
    Mat norm = new Mat();

    Imgproc.cvtColor(newimg, des, Imgproc.COLOR_BGR2HSV);
    List<Mat> hsv = new ArrayList<Mat>();
    Core.split(des, hsv);
    Mat v = hsv.get(2); //gets the grey scale version

    Imgcodecs.imwrite("/Users/Lisa-Maria/Documents/CapturedImages/B&Wpic.jpg", v); //only writes mats

    CLAHE clahe = Imgproc.createCLAHE(2.0, new Size(8,8) ); //2.0, new Size(8,8) 
    clahe.apply(v,v);
//    Imgproc.GaussianBlur(v, v, new Size(9,9), 3); //adds left pupil boundary and random circle on 'a'
 //   Imgproc.GaussianBlur(v, v, new Size(9,9), 13); //adds right outer iris boundary and random circle on 'a'
    Imgproc.GaussianBlur(v, v, new Size(9,9), 7);  //adds left outer iris boundary and random circle on left by hair
  //  Imgproc.GaussianBlur(v, v, new Size(7,7), 15);
    Core.addWeighted(v, 1.5, v, -0.5, 0, v);


    Imgcodecs.imwrite("/Users/.../after.jpg", v); //only writes mats
    if (v != null) {
        Mat circles = new Mat();

        Imgproc.HoughCircles( v, circles, Imgproc.CV_HOUGH_GRADIENT, 2, v.rows(), 100, 20, 20, 200 );

        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

        System.out.println("circles.cols() " + circles.cols());
        if(circles.cols() > 0) {
            System.out.println("1");
            for (int x = 0; x < circles.cols(); x++) {
                System.out.println("2");
                double vCircle[] = circles.get(0, x);


                if(vCircle == null) {
                    break;
                }

                Point pt = new Point(Math.round(vCircle[0]), Math.round(vCircle[1]));
                int radius = (int) Math.round(vCircle[2]);

                //draw the found circle




                Imgproc.circle(v, pt, radius, new Scalar(255,0,0),2); //newimg
                //Imgproc.circle(des, pt, radius/3, new Scalar(225,0,0),2); //pupil
                Imgcodecs.imwrite("/Users/.../Houghpic.jpg", v); //newimg

                //draw the mask: white circle on black background
//                  Mat mask = new Mat( new Size( des.cols(), des.rows() ), CvType.CV_8UC1 );
//                  Imgproc.circle(mask, pt, radius, new Scalar(255,0,0),2); 

//                  des.copyTo(des,mask);
//                  Imgcodecs.imwrite("/Users/..../mask.jpg", des); //newimg


                Imgproc.logPolar(des, norm, pt, radius, Imgproc.WARP_FILL_OUTLIERS);
                Imgcodecs.imwrite("/Users/..../Normalised.jpg",norm);
            }
        }
    }
}

结果:hough pic

1 个答案:

答案 0 :(得分:0)

在评论中进行讨论之后,我发布了一些一般性答案,其中包含了我在OP上传的最坏情况图像上获得的一些结果。

注意:我发布的代码是Python,因为它是我写的最快

步骤1.当您要求裁剪图像的方法时,只关注眼睛,您可能需要查看Face Detection。因为,图像基本上只需要找到眼睛,我做了以下几点:

eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
eyes = eye_cascade.detectMultiScale(v) // v is the value channel of the HSV image
// The results "eyes" gives you the dimensions of the rectangle where the eyes are detected as [x, y, w, h]
// Just for drawing 
cv2.rectangle(v, (x1, y1), (x1+w1, y1+h1), (0, 255, 0), 2)
cv2.rectangle(v, (x2, y2), (x2+w2, y2+h2), (0, 255, 0), 2)

The result of Haar Cascade Classifier

现在,一旦有了边界矩形,就可以从图像中裁剪出矩形,如:

crop_eye1 = v[y1:y1+h1, x1:x1+w1]
crop_eye2 = v[y2:y2+h2, x2:x2+w2]

获得矩形后,我建议查看不同的颜色空间,而不是RGB / BGR,特别是HSV / Lab / Luv。

因为数字图像中对象颜色的R,G和B分量都与击中对象的光量相关,因此相互之间,这些组件的图像描述成为对象歧视困难。色调/亮度/色度或色调/亮度/饱和度的描述通常更相关

然后,有一次,你有眼睛,是时候平衡图像的对比度,但是,我建议使用CLAHE并使用clipLimittileGridSize的参数。这是我在Java中实现的代码:

private static Mat clahe(Mat image, int ClipLimit, Size size){
  CLAHE clahe = Imgproc.createCLAHE();
  clahe.setClipLimit(ClipLimit);
  clahe.setTilesGridSize(size);
  Mat dest_image = new Mat();
  clahe.apply(image, dest_image);
  return dest_image;
}     

一旦您满意,您应该锐化图像,以便HoughCircle健壮。你应该看看unsharpMask。以下是我用Java实现的UnsharpMask的Java代码:

private static Mat unsharpMask(Mat input_image, Size size, double sigma){

// Make sure the {input_image} is gray.
  Mat sharpend_image = new Mat(input_image.rows(), input_image.cols(), input_image.type());
  Mat Blurred_image = new Mat(input_image.rows(), input_image.cols(), input_image.type());
  Imgproc.GaussianBlur(input_image, Blurred_image, size, sigma);
  Core.addWeighted(input_image, 2.0D, Blurred_image, -1.0D, 0.0D, sharpened_image);
  return sharpened_image;
}

或者,您可以使用bilateral filter(边缘保留平滑)或通过this读取来定义用于锐化图像的自定义内核。 希望它有所帮助,祝你好运!