Looking for opencv Answers? Try Ask4KnowledgeBase
Looking for opencv Keywords? Try Ask4Keywords

opencvUsing Cascade Classifiers In Java


Syntax

  • CascadeClassifier cascade = new CascadeClassifier("cascade.xml"); // Creates a cascade classifier from cascade.xml
  • Mat image = Imgcodecs.imread("image.png"); // Converts image.png into a Mat (Matrix) object
  • MatOfRect detections = new MatOfRect(); // Creates an empty MatOfRect (Matrix of Rectangles) file, used as output for our detection classes
  • detections.toArray(); // Returns an array of Rect objects that can be iterated over
  • Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0)); // Draws an green outlined rectangle from the first Point's x and y locations to the second Point's x and y location onto the Mat object "image". "rect" is a Rect object, usually provided by detections.toArray(). Uses OpenCV's Point class.
  • Imgcodecs.imwrite("output.png", image); // Writes the modified Mat object "image" to the "output.png"
  • CascadeClassifier.detectMultiScale(image, detections); // Detects any object in the Mat object "image" and outputs the detections in the MatOfRect object "detections"
  • CascadeClassifier.detectMultiScale(image, detections, scaleFactor, minNeighbors, flags, minSize, maxSize); // Performs a detection with additional parameters. See details below.
  • Imgproc.ellipse(image, center, axes, 0, 0, 360, new Scalar(255, 0, 255), thickness, lineType, 0); // Draws an ellipse onto the image at the point center. Uses OpenCV's Point class.

Parameters

ParameterDetails
scaleFactorHow much the image size is reduced at each image scale. Default = 1.1
minNeighborsHow many neighbors a candidate rectangle should have before selecting it as an detected object. Default = 4
flagsLegacy flags. In most cases, this should be set to 0. Default = 0
minSizeMinimum size a candidate rectangle can be. This uses OpenCV's Size class. Can be used to decrease detection time and CPU usage, as well as to reduce false positives.
maxSizeMaximum size a candidate rectangle can be. This uses OpenCV's Size class. Can be used to decrease detection time and CPU usage, as well as to reduce false positives.
axesUses OpenCV's Size class. Defines the width and height of the ellipse.
thicknessThickness of the line, in pixels.
lineTypeHas various parameters. 0 is the solid line, 8 is for a 8-connected line, 4 is for a 4-connected line, and CV_AA is for an antialiased line. Default = 8