/** * Takes the base OpenCV list of contours and changes the output to be easier to * work with. * * @param contours * The input from the base OpenCV contours output */ private void createParticleReports (List<MatOfPoint> contours) { ParticleReport[] reports = new ParticleReport[contours.size()]; for (int i = 0; i < reports.length; i++) { reports[i] = new ParticleReport(); Rect r = Imgproc.boundingRect(contours.get(i)); reports[i].area = r.area(); reports[i].center = new Point(r.x + (r.width / 2), r.y + (r.height / 2)); reports[i].boundingRect = r; } this.particleReports = reports; }
/** * Create a rectangle based on a set of points * * @param points Set of points (at least 4) defining the rectangle */ public Rectangle(Point[] points) { //Find top-left and bottom-right Point min = new Point(Double.MAX_VALUE, Double.MAX_VALUE); Point max = new Point(Double.MIN_VALUE, Double.MIN_VALUE); for (Point p : points) { if (p.x < min.x) { min.x = p.x; } if (p.y < min.y) { min.y = p.y; } if (p.x > max.x) { max.x = p.x; } if (p.y > max.y) { max.y = p.y; } } setRect(new Rect(min, max)); }
/** * This method is called to overlay rectangles on an image to the video output. * * @param image specifies the frame to be rendered to the video output. * @param detectedObjectRects specifies the detected object rectangles. * @param color specifies the color of the rectangle outline. * @param thickness specifies the thickness of the rectangle outline. */ public void drawRectangles(Mat image, Rect[] detectedObjectRects, Scalar color, int thickness) { // // Overlay a rectangle on each detected object. // synchronized (image) { if (detectedObjectRects != null) { for (Rect r: detectedObjectRects) { // // Draw a rectangle around the detected object. // Imgproc.rectangle( image, new Point(r.x, r.y), new Point(r.x + r.width, r.y + r.height), color, thickness); } } videoSource.putFrame(image); } }
public static Point[] findAllColors(ImageWrapper image, int color, int threshold, Rect rect) { Mat bi = new Mat(); Scalar lowerBound = new Scalar(Color.red(color) - threshold, Color.green(color) - threshold, Color.blue(color) - threshold, 255); Scalar upperBound = new Scalar(Color.red(color) + threshold, Color.green(color) + threshold, Color.blue(color) + threshold, 255); if (rect != null) { Core.inRange(new Mat(image.getMat(), rect), lowerBound, upperBound, bi); } else { Core.inRange(image.getMat(), lowerBound, upperBound, bi); } Mat nonZeroPos = new Mat(); Core.findNonZero(bi, nonZeroPos); if (nonZeroPos.rows() == 0 || nonZeroPos.cols() == 0) { return new Point[0]; } Point[] points = new MatOfPoint(nonZeroPos).toArray(); if (rect != null) { for (int i = 0; i < points.length; i++) { points[i].x += rect.x; points[i].y += rect.y; } } return points; }
public static Rect boundingRect(MatOfPoint points) { Mat points_mat = points; Rect retVal = new Rect(boundingRect_0(points_mat.nativeObj)); return retVal; }
public static RotatedRect CamShift(Mat probImage, Rect window, TermCriteria criteria) { double[] window_out = new double[4]; RotatedRect retVal = new RotatedRect(CamShift_0(probImage.nativeObj, window.x, window.y, window.width, window.height, window_out, criteria.type, criteria.maxCount, criteria.epsilon)); if(window!=null){ window.x = (int)window_out[0]; window.y = (int)window_out[1]; window.width = (int)window_out[2]; window.height = (int)window_out[3]; } return retVal; }
public static void Mat_to_vector_Rect(Mat m, List<Rect> rs) { if (rs == null) throw new java.lang.IllegalArgumentException("rs == null"); int count = m.rows(); if (CvType.CV_32SC4 != m.type() || m.cols() != 1) throw new java.lang.IllegalArgumentException( "CvType.CV_32SC4 != m.type() || m.rows()!=1\n" + m); rs.clear(); int[] buff = new int[4 * count]; m.get(0, 0, buff); for (int i = 0; i < count; i++) { rs.add(new Rect(buff[4 * i], buff[4 * i + 1], buff[4 * i + 2], buff[4 * i + 3])); } }
public static void stereoRectify(Mat cameraMatrix1, Mat distCoeffs1, Mat cameraMatrix2, Mat distCoeffs2, Size imageSize, Mat R, Mat T, Mat R1, Mat R2, Mat P1, Mat P2, Mat Q, int flags, double alpha, Size newImageSize, Rect validPixROI1, Rect validPixROI2) { double[] validPixROI1_out = new double[4]; double[] validPixROI2_out = new double[4]; stereoRectify_0(cameraMatrix1.nativeObj, distCoeffs1.nativeObj, cameraMatrix2.nativeObj, distCoeffs2.nativeObj, imageSize.width, imageSize.height, R.nativeObj, T.nativeObj, R1.nativeObj, R2.nativeObj, P1.nativeObj, P2.nativeObj, Q.nativeObj, flags, alpha, newImageSize.width, newImageSize.height, validPixROI1_out, validPixROI2_out); if(validPixROI1!=null){ validPixROI1.x = (int)validPixROI1_out[0]; validPixROI1.y = (int)validPixROI1_out[1]; validPixROI1.width = (int)validPixROI1_out[2]; validPixROI1.height = (int)validPixROI1_out[3]; } if(validPixROI2!=null){ validPixROI2.x = (int)validPixROI2_out[0]; validPixROI2.y = (int)validPixROI2_out[1]; validPixROI2.width = (int)validPixROI2_out[2]; validPixROI2.height = (int)validPixROI2_out[3]; } return; }
public static int meanShift(Mat probImage, Rect window, TermCriteria criteria) { double[] window_out = new double[4]; int retVal = meanShift_0(probImage.nativeObj, window.x, window.y, window.width, window.height, window_out, criteria.type, criteria.maxCount, criteria.epsilon); if(window!=null){ window.x = (int)window_out[0]; window.y = (int)window_out[1]; window.width = (int)window_out[2]; window.height = (int)window_out[3]; } return retVal; }
public static float rectify3Collinear(Mat cameraMatrix1, Mat distCoeffs1, Mat cameraMatrix2, Mat distCoeffs2, Mat cameraMatrix3, Mat distCoeffs3, List<Mat> imgpt1, List<Mat> imgpt3, Size imageSize, Mat R12, Mat T12, Mat R13, Mat T13, Mat R1, Mat R2, Mat R3, Mat P1, Mat P2, Mat P3, Mat Q, double alpha, Size newImgSize, Rect roi1, Rect roi2, int flags) { Mat imgpt1_mat = Converters.vector_Mat_to_Mat(imgpt1); Mat imgpt3_mat = Converters.vector_Mat_to_Mat(imgpt3); double[] roi1_out = new double[4]; double[] roi2_out = new double[4]; float retVal = rectify3Collinear_0(cameraMatrix1.nativeObj, distCoeffs1.nativeObj, cameraMatrix2.nativeObj, distCoeffs2.nativeObj, cameraMatrix3.nativeObj, distCoeffs3.nativeObj, imgpt1_mat.nativeObj, imgpt3_mat.nativeObj, imageSize.width, imageSize.height, R12.nativeObj, T12.nativeObj, R13.nativeObj, T13.nativeObj, R1.nativeObj, R2.nativeObj, R3.nativeObj, P1.nativeObj, P2.nativeObj, P3.nativeObj, Q.nativeObj, alpha, newImgSize.width, newImgSize.height, roi1_out, roi2_out, flags); if(roi1!=null){ roi1.x = (int)roi1_out[0]; roi1.y = (int)roi1_out[1]; roi1.width = (int)roi1_out[2]; roi1.height = (int)roi1_out[3]; } if(roi2!=null){ roi2.x = (int)roi2_out[0]; roi2.y = (int)roi2_out[1]; roi2.width = (int)roi2_out[2]; roi2.height = (int)roi2_out[3]; } return retVal; }
public static Mat getOptimalNewCameraMatrix(Mat cameraMatrix, Mat distCoeffs, Size imageSize, double alpha, Size newImgSize, Rect validPixROI, boolean centerPrincipalPoint) { double[] validPixROI_out = new double[4]; Mat retVal = new Mat(getOptimalNewCameraMatrix_0(cameraMatrix.nativeObj, distCoeffs.nativeObj, imageSize.width, imageSize.height, alpha, newImgSize.width, newImgSize.height, validPixROI_out, centerPrincipalPoint)); if(validPixROI!=null){ validPixROI.x = (int)validPixROI_out[0]; validPixROI.y = (int)validPixROI_out[1]; validPixROI.width = (int)validPixROI_out[2]; validPixROI.height = (int)validPixROI_out[3]; } return retVal; }
@Override public void onFaceDetected(Rect faceOpenCV, Face face) { if (!isStopped && view != null) { view.drawFace(faceOpenCV, matrixRgba); view.startEyesDetection(faceOpenCV); } }
public static Rect horizontalScale(Rect original, double factor) { double width = original.br().x - original.tl().x; double newWidth = width * factor; double newLeft = original.tl().x - ((newWidth - width) / 2); double height = original.br().y - original.tl().y; return new Rect((int) newLeft, (int) original.tl().y, (int) newWidth, (int) height); }
public void startThread() { if (thread == null) { // Create a new thread that will constantly evaluate new camera frames thread = new Thread(() -> { Mat image = new Mat(CAMERA_HEIGHT, CAMERA_WIDTH, 6); while (!Thread.interrupted()) { // Check whether it's time to evaluate a new rame currentFrameTime = sink.grabFrameNoTimeout(image); if (currentFrameTime == 0 || currentFrameTime == prevFrameTime) { continue; } prevFrameTime = currentFrameTime; // Process image using generated pipeline pipeline.process(image); // Get pipeline objects ArrayList<MatOfPoint> mops = pipeline.filterContoursOutput(); // Evaluate objects for (MatOfPoint mop : mops) { Rect bRect = Imgproc.boundingRect(mop); Imgproc.rectangle(image, bRect.tl(), bRect.br(), new Scalar(255, 0, 255)); // TODO: Evaluate objects } // Send the evaluated image to the output stream output.putFrame(image); } }); thread.start(); } }
public static int floodFill(Mat image, Mat mask, Point seedPoint, Scalar newVal, Rect rect, Scalar loDiff, Scalar upDiff, int flags) { double[] rect_out = new double[4]; int retVal = floodFill_0(image.nativeObj, mask.nativeObj, seedPoint.x, seedPoint.y, newVal.val[0], newVal.val[1], newVal.val[2], newVal.val[3], rect_out, loDiff.val[0], loDiff.val[1], loDiff.val[2], loDiff.val[3], upDiff.val[0], upDiff.val[1], upDiff.val[2], upDiff.val[3], flags); if(rect!=null){ rect.x = (int)rect_out[0]; rect.y = (int)rect_out[1]; rect.width = (int)rect_out[2]; rect.height = (int)rect_out[3]; } return retVal; }
public static boolean clipLine(Rect imgRect, Point pt1, Point pt2) { double[] pt1_out = new double[2]; double[] pt2_out = new double[2]; boolean retVal = clipLine_0(imgRect.x, imgRect.y, imgRect.width, imgRect.height, pt1.x, pt1.y, pt1_out, pt2.x, pt2.y, pt2_out); if(pt1!=null){ pt1.x = pt1_out[0]; pt1.y = pt1_out[1]; } if(pt2!=null){ pt2.x = pt2_out[0]; pt2.y = pt2_out[1]; } return retVal; }
public Mat onCameraFrame(CvCameraViewFrame inputFrame) { mRgba = inputFrame.rgba(); mGray = inputFrame.gray(); if (mAbsoluteFaceSize == 0) { int height = mGray.rows(); if (Math.round(height * mRelativeFaceSize) > 0) { mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize); } } MatOfRect faces = new MatOfRect(); if (mJavaDetector != null) mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2, 2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size()); Rect[] facesArray = faces.toArray(); if(isFaceRectangleEnabled){ for (int i = 0; i < facesArray.length; i++) Core.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(), FACE_RECT_COLOR, 3); } if (facesArray.length == 1) { chooseFaceRect = facesArray[0].clone(); isFaceRectChosen = true; } return mRgba; }
private Rect computeBoundingRectangle(ArrayList<MatOfPoint> contours) { if (contours.size() == 2) { return computeBoundingRectangle(contours.get(0), contours.get(1)); } else if (contours.size() > 2) { MatOfPoint[] topTwoContours = findTopTwoContours(contours); if (topTwoContours == null) return null; return computeBoundingRectangle(topTwoContours[0], topTwoContours[1]); } return null; }
@Override public void run() { synchronized (this) { while (runnable) { if (webSource.grab()) { try { webSource.retrieve(fm); Graphics g = jPanel1.getGraphics(); faceDetector.detectMultiScale(fm, faceDetections); for (Rect rect : faceDetections.toArray()) { Imgproc.rectangle(fm, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0)); } Highgui.imencode(".bmp", fm, mem); Image im = ImageIO.read(new ByteArrayInputStream(mem.toArray())); BufferedImage buff = (BufferedImage) im; if (g.drawImage(buff, 0, 0, getWidth(), getHeight(), 0, 0, buff.getWidth(), buff.getHeight(), null)) { if (runnable == false) { System.out.println("Paused ..... "); this.wait(); } } } catch (Exception ex) { System.out.println("Error"); } } } } }
public static Point findColor(ImageWrapper imageWrapper, int color, int threshold, Rect region) { Point[] points = findAllColors(imageWrapper, color, threshold, region); if (points.length == 0) { return null; } return points[0]; }