/**
   * @param inputImg
   * @return Mat
   */
  public static Mat watershed(Mat inputImg) {

    Mat target = new Mat(inputImg.rows(), inputImg.cols(), CvType.CV_8UC3);
    Imgproc.cvtColor(inputImg, target, Imgproc.COLOR_BGR2RGB);

    // Conversion to 8UC1 grayscale image
    Mat grayScale = new Mat(inputImg.rows(), inputImg.cols(), CvType.CV_32SC1);
    Imgproc.cvtColor(inputImg, grayScale, Imgproc.COLOR_BGR2GRAY);

    // constructing a 3x3 kernel for morphological opening
    Mat openingKernel = Mat.ones(9, 9, CvType.CV_8U);

    // яскравість
    // target.convertTo(target, -1, 10d * 12 / 100, 0);
    // Imgproc.dilate(target, target, new Mat(), new Point(-1, -1), 1);

    Size s = new Size(27, 27);
    Imgproc.GaussianBlur(target, target, s, 1.7);

    Imgproc.morphologyEx(target, target, Imgproc.MORPH_OPEN, openingKernel);

    // dilation operation for extracting the background
    // Imgproc.dilate(target, target, openingKernel);
    // Imgproc.erode(target, target, new Mat(), new Point(-1, -1), 1);

    Mat seeds = new Mat(target.rows(), target.cols(), CvType.CV_32SC1);

    for (int i = 0; i < 10; i++) {
      seeds.put(((int) Math.random()) % target.rows(), ((int) Math.random()) % target.cols(), i);
    }

    Imgproc.watershed(target, seeds);
    // Imgproc.threshold(target,target, 50, 155, Imgproc.THRESH_BINARY );
    return target;
  }
Example #2
0
  // Generates random email string for MongoDB to store fake accounts for testing
  public String randomEmail() {

    final String LOTTO_POOL = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    StringBuilder bob = new StringBuilder();

    int length = 6;
    while (length-- != 0) {

      int character = (int) (Math.random() * LOTTO_POOL.length());
      bob.append(LOTTO_POOL.charAt(character));
    }

    return bob.toString() + "@email.com";
  }
Example #3
0
  public void run() {
    System.out.println("\nRunning DetectFaceDemo");

    // Create a face detector from the cascade file in the resources
    // directory.
    // String facefilterpath =
    // getClass().getResource("../resources/haarcascade_mcs_eyepair_big.xml").getPath();
    String facefilterpath = getClass().getResource("../resources/haarcascade_eye.xml").getPath();
    facefilterpath = facefilterpath.substring(1, facefilterpath.length());
    CascadeClassifier faceDetector = new CascadeClassifier(facefilterpath);
    String pngpath = getClass().getResource("../resources/brown_eyes.jpg").getPath();
    pngpath = pngpath.substring(1, pngpath.length());
    Mat image = Highgui.imread(pngpath);

    // Detect faces in the ismage.
    // MatOfRect is a special container class for Rect.
    MatOfRect faceDetections = new MatOfRect();
    faceDetector.detectMultiScale(image, faceDetections);

    Mat image2 = image;

    Imgproc.cvtColor(image2, image, 6); // 6 = CV_BGR2GRAY not working
    Imgproc.GaussianBlur(image, image, new Size(7, 7), 4, 4);
    // Imgproc.medianBlur(image,image, 2);
    MatOfPoint3f circles = new MatOfPoint3f();
    MatOfPoint3f circles2 = new MatOfPoint3f();

    Imgproc.HoughCircles(
        image, circles, Imgproc.CV_HOUGH_GRADIENT, 5, image.rows() / 5, 100, 100, 10, 50);

    Imgproc.HoughCircles(
        image, circles2, Imgproc.CV_HOUGH_GRADIENT, 5, image.rows() / 5, 100, 100, 50, 400);

    Imgproc.cvtColor(image, image, 8); // 6 = CV_BGR2GRAY not working

    System.out.println(String.format("Detected %s faces", faceDetections));
    // Draw a bounding box around each face.
    for (Rect rect : faceDetections.toArray()) {
      // Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y +
      // rect.height), new Scalar(0, 255, 0),100);
    }

    System.out.println(String.format("Detected %s circles", circles.total()));

    for (Point3 circle : circles.toArray()) {
      Point center = new Point(circle.x, circle.y);
      int radius = (int) Math.round(circle.z);
      Core.circle(image, center, 3, new Scalar(0, 255, 0), -1, 8, 0);
      Core.circle(image, center, radius, new Scalar(0, 0, 255), 3, 8, 0);
      // Core.circle(image, center, radius, new Scalar(0,255,0), 10,8, 0);
    }
    for (Point3 circle : circles2.toArray()) {
      Point center = new Point(circle.x, circle.y);
      int radius = (int) Math.round(circle.z);
      Core.circle(image, center, 3, new Scalar(0, 255, 0), -1, 8, 0);
      Core.circle(image, center, radius, new Scalar(0, 0, 255), 3, 8, 0);
      // Core.circle(image, center, radius, new Scalar(0,255,0), 10,8, 0);
    }

    // Core.circle(image, new Point(100,100), 10, new Scalar(0,255,0), 10, 8, 0);
    // Save the visualized detection.

    String filename = "faceDetection.png";
    System.out.println(String.format("Writing %s", filename));
    Highgui.imwrite(filename, image);
  }