List of usage examples for org.opencv.core Rect br
public Point br()
From source file:OctoEye.java
License:Open Source License
private void detectPupil() { // min and max pupil radius int r_min = 2; int r_max = 45; // min and max pupil diameter int d_min = 2 * r_min; int d_max = 2 * r_max; // min and max pupil area double area;/*from ww w .jav a2 s .co m*/ double a_min = Math.PI * r_min * r_min; double a_max = Math.PI * r_max * r_max; // histogram stuff List<Mat> images; MatOfInt channels; Mat mask; Mat hist; MatOfInt mHistSize; MatOfFloat mRanges; // contour and circle stuff Rect rect = null; Rect rectMin; Rect rectMax; List<MatOfPoint> contours; MatOfPoint3 circles; // pupil center Point p; // ellipse test points Point v; Point r; Point s; // rect points Point tl; Point br; // pupil edge detection Vector<Point> pointsTest; Vector<Point> pointsEllipse; Vector<Point> pointsRemoved; // temporary variables double distance; double rad; double length; int x; int y; int tmp; byte buff[]; // ------------------------------------------------------------------------------------------------------------- // step 1 // blur the image to reduce noise Imgproc.medianBlur(src, tmp1, 25); // ------------------------------------------------------------------------------------------------------------- // step 2 // locate the pupil with feature detection and compute a histogram for each, // the best feature will be used as rough pupil location (rectMin) int score = 0; int winner = 0; // feature detection MatOfKeyPoint matOfKeyPoints = new MatOfKeyPoint(); FeatureDetector blobDetector = FeatureDetector.create(FeatureDetector.MSER); // Maximal Stable Extremal Regions blobDetector.detect(tmp1, matOfKeyPoints); List<KeyPoint> keyPoints = matOfKeyPoints.toList(); // histogram calculation for (int i = 0; i < keyPoints.size(); i++) { x = (int) keyPoints.get(i).pt.x; y = (int) keyPoints.get(i).pt.y; tl = new Point(x - 5 >= 0 ? x - 5 : 0, y - 5 >= 0 ? y - 5 : 0); br = new Point(x + 5 < WIDTH ? x + 5 : WIDTH - 1, y + 5 < HEIGHT ? y + 5 : HEIGHT - 1); images = new ArrayList<Mat>(); images.add(tmp1.submat(new Rect(tl, br))); channels = new MatOfInt(0); mask = new Mat(); hist = new Mat(); mHistSize = new MatOfInt(256); mRanges = new MatOfFloat(0f, 256f); Imgproc.calcHist(images, channels, mask, hist, mHistSize, mRanges); tmp = 0; for (int j = 0; j < 256 / 3; j++) { tmp += (256 / 3 - j) * (int) hist.get(j, 0)[0]; } if (tmp >= score) { score = tmp; winner = i; rect = new Rect(tl, br); } if (debug) { // show features (orange) Core.circle(dbg, new Point(x, y), 3, ORANGE); } } if (rect == null) { return; } rectMin = rect.clone(); if (debug) { // show rectMin (red) Core.rectangle(dbg, rectMin.tl(), rect.br(), RED, 1); } // ------------------------------------------------------------------------------------------------------------- // step 3 // compute a rectMax (blue) which is larger than the pupil int margin = 32; rect.x = rect.x - margin; rect.y = rect.y - margin; rect.width = rect.width + 2 * margin; rect.height = rect.height + 2 * margin; rectMax = rect.clone(); if (debug) { // show features (orange) Core.rectangle(dbg, rectMax.tl(), rectMax.br(), BLUE); } // ------------------------------------------------------------------------------------------------------------- // step 4 // blur the image again Imgproc.medianBlur(src, tmp1, 7); Imgproc.medianBlur(tmp1, tmp1, 3); Imgproc.medianBlur(tmp1, tmp1, 3); Imgproc.medianBlur(tmp1, tmp1, 3); // ------------------------------------------------------------------------------------------------------------- // step 5 // detect edges Imgproc.Canny(tmp1, tmp2, 40, 50); // ------------------------------------------------------------------------------------------------------------- // step 6 // from pupil center to maxRect borders, find all edge points, compute a first ellipse p = new Point(rectMin.x + rectMin.width / 2, rectMin.y + rectMin.height / 2); pointsTest = new Vector<Point>(); pointsEllipse = new Vector<Point>(); pointsRemoved = new Vector<Point>(); buff = new byte[tmp2.rows() * tmp2.cols()]; tmp2.get(0, 0, buff); length = Math.min(p.x - rectMax.x - 3, p.y - rectMax.y - 3); length = Math.sqrt(2 * Math.pow(length, 2)); Point z = new Point(p.x, p.y - length); for (int i = 0; i < 360; i += 15) { rad = Math.toRadians(i); x = (int) (p.x + Math.cos(rad) * (z.x - p.x) - Math.sin(rad) * (z.y - p.y)); y = (int) (p.y + Math.sin(rad) * (z.x - p.x) - Math.cos(rad) * (z.y - p.y)); pointsTest.add(new Point(x, y)); } if (debug) { for (int i = 0; i < pointsTest.size(); i++) { Core.line(dbg, p, pointsTest.get(i), GRAY, 1); Core.rectangle(dbg, rectMin.tl(), rectMin.br(), GREEN, 1); Core.rectangle(dbg, rectMax.tl(), rectMax.br(), BLUE, 1); } Core.rectangle(dbg, rectMin.tl(), rectMin.br(), BLACK, -1); Core.rectangle(dbg, rectMin.tl(), rectMin.br(), RED, 1); Core.rectangle(dbg, rectMax.tl(), rectMax.br(), BLUE); } // p: Ursprung ("Mittelpunkt" der Ellipse) // v: Zielpunkt (Testpunkt) // r: Richtungsvektor PV for (int i = 0; i < pointsTest.size(); i++) { v = new Point(pointsTest.get(i).x, pointsTest.get(i).y); r = new Point(v.x - p.x, v.y - p.y); length = Math.sqrt(Math.pow(p.x - v.x, 2) + Math.pow(p.y - v.y, 2)); boolean found = false; for (int j = 0; j < Math.round(length); j++) { s = new Point(Math.rint(p.x + (double) j / length * r.x), Math.rint(p.y + (double) j / length * r.y)); s.x = Math.max(1, Math.min(s.x, WIDTH - 2)); s.y = Math.max(1, Math.min(s.y, HEIGHT - 2)); tl = new Point(s.x - 1, s.y - 1); br = new Point(s.x + 1, s.y + 1); buff = new byte[3 * 3]; rect = new Rect(tl, br); try { (tmp2.submat(rect)).get(0, 0, buff); for (int k = 0; k < 3 * 3; k++) { if (Math.abs(buff[k]) == 1) { pointsEllipse.add(s); found = true; break; } } } catch (Exception e) { break; } if (found) { break; } } } double e_min = Double.POSITIVE_INFINITY; double e_max = 0; double e_med = 0; for (int i = 0; i < pointsEllipse.size(); i++) { v = pointsEllipse.get(i); length = Math.sqrt(Math.pow(p.x - v.x, 2) + Math.pow(p.y - v.y, 2)); e_min = (length < e_min) ? length : e_min; e_max = (length > e_max) ? length : e_max; e_med = e_med + length; } e_med = e_med / pointsEllipse.size(); if (pointsEllipse.size() >= 5) { Point[] points1 = new Point[pointsEllipse.size()]; for (int i = 0; i < pointsEllipse.size(); i++) { points1[i] = pointsEllipse.get(i); } MatOfPoint2f points2 = new MatOfPoint2f(); points2.fromArray(points1); pupil = Imgproc.fitEllipse(points2); } if (pupil.center.x == 0 && pupil.center.y == 0) { // something went wrong, return null reset(); return; } if (debug) { Core.ellipse(dbg, pupil, PURPLE, 2); } // ------------------------------------------------------------------------------------------------------------- // step 7 // remove some outlier points and compute the ellipse again try { for (int i = 1; i <= 4; i++) { distance = 0; int remove = 0; for (int j = pointsEllipse.size() - 1; j >= 0; j--) { v = pointsEllipse.get(j); length = Math.sqrt(Math.pow(v.x - pupil.center.x, 2) + Math.pow(v.y - pupil.center.y, 2)); if (length > distance) { distance = length; remove = j; } } v = pointsEllipse.get(remove); pointsEllipse.removeElementAt(remove); pointsRemoved.add(v); } } catch (Exception e) { // something went wrong, return null reset(); return; } if (pointsEllipse.size() >= 5) { Point[] points1 = new Point[pointsEllipse.size()]; for (int i = 0; i < pointsEllipse.size(); i++) { points1[i] = pointsEllipse.get(i); } MatOfPoint2f points2 = new MatOfPoint2f(); points2.fromArray(points1); pupil = Imgproc.fitEllipse(points2); Point[] vertices = new Point[4]; pupil.points(vertices); double d1 = Math .sqrt(Math.pow(vertices[1].x - vertices[0].x, 2) + Math.pow(vertices[1].y - vertices[0].y, 2)); double d2 = Math .sqrt(Math.pow(vertices[2].x - vertices[1].x, 2) + Math.pow(vertices[2].y - vertices[1].y, 2)); if (d1 >= d2) { pupilMajorAxis = (int) (d1 / 2); pupilMinorAxis = (int) (d2 / 2); axisA = new Point(vertices[1].x + (vertices[2].x - vertices[1].x) / 2, vertices[1].y + (vertices[2].y - vertices[1].y) / 2); axisB = new Point(vertices[0].x + (vertices[1].x - vertices[0].x) / 2, vertices[0].y + (vertices[1].y - vertices[0].y) / 2); } else { pupilMajorAxis = (int) (d2 / 2); pupilMinorAxis = (int) (d1 / 2); axisB = new Point(vertices[1].x + (vertices[2].x - vertices[1].x) / 2, vertices[1].y + (vertices[2].y - vertices[1].y) / 2); axisA = new Point(vertices[0].x + (vertices[1].x - vertices[0].x) / 2, vertices[0].y + (vertices[1].y - vertices[0].y) / 2); } } double ratio = (double) pupilMinorAxis / (double) pupilMajorAxis; if (ratio < 0.75 || 2 * pupilMinorAxis <= d_min || 2 * pupilMajorAxis >= d_max) { // something went wrong, return null reset(); return; } // pupil found if (debug) { Core.ellipse(dbg, pupil, GREEN, 2); Core.line(dbg, pupil.center, axisA, RED, 2); Core.line(dbg, pupil.center, axisB, BLUE, 2); Core.circle(dbg, pupil.center, 1, GREEN, 0); x = 5; y = 5; Core.rectangle(dbg, new Point(x, y), new Point(x + 80 + 4, y + 10), BLACK, -1); Core.rectangle(dbg, new Point(x + 2, y + 2), new Point(x + 2 + pupilMajorAxis, y + 4), RED, -1); Core.rectangle(dbg, new Point(x + 2, y + 6), new Point(x + 2 + pupilMinorAxis, y + 8), BLUE, -1); for (int i = pointsEllipse.size() - 1; i >= 0; i--) { Core.circle(dbg, pointsEllipse.get(i), 2, ORANGE, -1); } for (int i = pointsRemoved.size() - 1; i >= 0; i--) { Core.circle(dbg, pointsRemoved.get(i), 2, PURPLE, -1); } } Core.ellipse(dst, pupil, GREEN, 2); Core.circle(dst, pupil.center, 1, GREEN, 0); }
From source file:by.zuyeu.deyestracker.core.detection.DemoPanel.java
private static void addRectangleToImage(Rect face, Mat webcamImage, final Scalar color) { Core.rectangle(webcamImage, face.tl(), face.br(), color); }
From source file:ch.zhaw.facerecognitionlibrary.Helpers.MatOperation.java
License:Open Source License
public static Point drawRectangleOnPreview(Mat img, Rect face, boolean front_camera) { if (front_camera) { int topLeftX = (int) (img.cols() - (face.tl().x + face.width)); int bottomRightX = (int) (img.cols() - (face.br().x) + face.width); Point tl = new Point(topLeftX, face.tl().y); Point br = new Point(bottomRightX, face.br().y); Imgproc.rectangle(img, tl, br, FACE_RECT_COLOR, THICKNESS); return tl; } else {//from w w w . ja va 2s.c o m Imgproc.rectangle(img, face.tl(), face.br(), FACE_RECT_COLOR, THICKNESS); return face.tl(); } }
From source file:com.astrocytes.core.operationsengine.CoreOperations.java
License:Open Source License
/** * Calculates an average intensity of pixels on image within specified contour. * * @param src - source image used for calculating an average intensity. * @param contour - a contour which is presented as some region of interest for operation. * @return a level of average intensity. */// w w w. j av a2 s.co m public static int averageIntensity(Mat src, MatOfPoint contour) { int averageIntensityWithinContour = 0; int quantityOfPixelsWithinContour = 0; Rect boundingRectangle = boundingRect(contour); for (int xCoord = (int) boundingRectangle.tl().x; xCoord <= (int) boundingRectangle.br().x; xCoord++) { for (int yCoord = (int) boundingRectangle.tl().y; yCoord <= (int) boundingRectangle.br().y; yCoord++) { if (pointPolygonTest(new MatOfPoint2f(contour.toArray()), new Point(xCoord, yCoord), false) > 0) { averageIntensityWithinContour += intensity(src, xCoord, yCoord); quantityOfPixelsWithinContour++; } } } if (quantityOfPixelsWithinContour == 0) { quantityOfPixelsWithinContour = 1; averageIntensityWithinContour = intensity(src, boundingRectangle.x, boundingRectangle.y); if (src.channels() == 1) { averageIntensityWithinContour = averageIntensityWithinContour > 127 ? 0 : 255; } } return averageIntensityWithinContour / quantityOfPixelsWithinContour; }
From source file:com.example.afs.makingmusic.process.ImageGenerator.java
License:Open Source License
@Override public void process(Frame frame) { Mat image = frame.getImageMatrix();/*from w ww . j a v a 2 s. c o m*/ for (MusicAnnotation musicAnnotation : frame.getMusicAnnotations()) { Scalar color; Type type = musicAnnotation.getType(); switch (type) { case NEW: color = GREEN; break; case ACTIVE: color = BLUE; break; case DUPLICATE: color = YELLOW; break; case OVERFLOW: color = RED; break; default: throw new UnsupportedOperationException(); } Rect item = musicAnnotation.getItem(); Imgproc.rectangle(image, item.br(), item.tl(), color, 1); //Imgproc.putText(image, musicAnnotation.getInstrument().getName(), item.tl(), Core.FONT_HERSHEY_PLAIN, 1d, color); } BufferedImage bufferedImage = toBufferedImage(image); frame.setBufferedImage(bufferedImage); }
From source file:com.mycompany.objectdetection.ObjectDetector.java
public void findObjects() { // Imgproc.cvtColor(img, imgGrayscale, Imgproc.COLOR_RGBA2GRAY, 1); // Core.convertScaleAbs(img, imgGrayscale); // Core.normalize(imgGrayscale, imgMeanShifted, 0.0, 1.0, NORM_MINMAX); preProcessImg();//from w ww.j a v a 2 s .c om toGrayScale(imgMeanShifted); detectEdges(imgGrayscale); Imgproc.findContours(imgCanny, contours, imgCanny, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); objList = new ArrayList(); for (MatOfPoint mop : contours) { MatOfPoint2f m2p; m2p = new MatOfPoint2f(mop.toArray()); Double peri = Imgproc.arcLength(m2p, true); Imgproc.approxPolyDP(m2p, m2p, 0.02 * peri, true); Imgproc.drawContours(imgOut, contours, -1, new Scalar(0, 0, 255), 2); float area = img.width() * img.height(); Rect rect = Imgproc.boundingRect(mop); objList.add(rect); Imgproc.rectangle(imgOut, rect.tl(), rect.br(), new Scalar(255, 0, 0)); } Collections.sort(objList, new Comparator<Rect>() { @Override public int compare(Rect r1, Rect r2) { return (int) (r2.area() - r1.area()); } }); List<Rect> arr = objList; while (arr.size() > 0) { //System.out.println("---->" + arr); Rect bigRect = arr.get(0); arr.remove(0); Rect bigRect2 = new Rect(); while (!equals(bigRect, bigRect2)) { bigRect2 = bigRect; for (int i = 0; i < arr.size(); ++i) { // System.out.println("elotte"+arr.get(i)); if (doOverlap(bigRect, arr.get(i))) { //System.out.println("utana"+arr.get(i)); bigRect = union(bigRect, arr.get(i)); arr.remove(i); break; } } } mainRect = bigRect; if (objList.size() > 5 && mainRect.area() >= img.width() * img.height() * 3 / 100) { Imgproc.rectangle(imgOut, bigRect.tl(), bigRect.br(), new Scalar(255, 255, 0)); mainObjects.add(mainRect); } else if (objList.size() <= 5) { mainObjects.add(mainRect); } } }
From source file:com.mycompany.objectdetection.ObjectDetector.java
public boolean doOverlap(Rect r1, Rect r2) { if (r1.tl().x > r2.br().x || r1.br().x < r2.tl().x || r1.tl().y > r2.br().y || r1.br().y < r2.tl().y) { return false; } else {/* w w w . j a v a 2 s . c o m*/ return true; } }
From source file:com.mycompany.objectdetection.ObjectDetector.java
public boolean equals(Rect r1, Rect r2) { if (r1.tl().x == r2.tl().x && r1.br().x == r2.br().x && r1.tl().y == r2.tl().y && r1.br().y == r2.br().y) { return true; }// ww w . jav a2 s . c o m return false; }
From source file:com.mycompany.objectdetection.ObjectDetector.java
public Rect union(Rect r1, Rect r2) { Point[] rects_pts = new Point[4]; rects_pts[0] = r1.tl();// ww w. ja v a 2 s . c o m rects_pts[1] = r1.br(); rects_pts[2] = r2.tl(); rects_pts[3] = r2.br(); MatOfPoint mof = new MatOfPoint(); mof.fromArray(rects_pts); Rect union = Imgproc.boundingRect(mof); return union; }
From source file:com.raulh82vlc.face_detection_sample.opencv.render.FaceDrawerOpenCV.java
License:Apache License
public static void drawFaceShapes(Rect face, Mat matrixRGBA) { Point start = face.tl();/*from w w w. jav a2s . co m*/ int h = (int) start.y + (face.height / 2); int w = (int) start.x + (face.width / 2); Imgproc.rectangle(matrixRGBA, start, face.br(), FACE_RECT_COLOR, 3); Point center = new Point(w, h); Imgproc.circle(matrixRGBA, center, 10, new Scalar(255, 0, 0, 255), 3); }