List of usage examples for org.opencv.core Scalar Scalar
public Scalar(double v0, double v1, double v2)
From source file:depthDataFromStereoCamsOpenCV.ProcessImages.java
/** * /*w w w . j a va 2 s. c o m*/ * @param image * @return */ public static Mat bringImageToStdSize(Mat image) { //create the square container int dstWidth = 300; int dstHeight = 500; Mat dst = new Mat(dstHeight, dstWidth, CvType.CV_8UC3, new Scalar(0, 0, 0)); //ProcessImages.displayImage(ProcessImages.Mat2BufferedImage(dst),"background"); //Put the image into the container, roi is the new position Rect roi = new Rect((dstWidth - image.cols()) / 2, (dstHeight - image.rows()) / 2, image.cols(), image.rows()); Mat targetROI = new Mat(dst, roi); image.copyTo(targetROI); // ProcessImages.displayImage(ProcessImages.Mat2BufferedImage(dst),"Standardized"); return dst; }
From source file:detectiontest.ImageDisplayer.java
public static Scalar colorToScalar(Color c) { return new Scalar(c.getBlue(), c.getGreen(), c.getRed()); }
From source file:dfmDrone.examples.fitEllipseExample.java
private static Mat findAndDrawEllipse(Mat sourceImg) { Mat grayScaleImg = new Mat(); Mat hsvImg = new Mat(); Imgproc.cvtColor(sourceImg, hsvImg, Imgproc.COLOR_BGR2HSV); Mat lower_hue_range = new Mat(); Mat upper_hue_range = new Mat(); Core.inRange(hsvImg, new Scalar(0, 100, 45), new Scalar(15, 255, 255), lower_hue_range); Core.inRange(hsvImg, new Scalar(160, 100, 45), new Scalar(180, 255, 255), upper_hue_range); Mat red_hue_image = new Mat(); Core.addWeighted(lower_hue_range, 1.0, upper_hue_range, 1.0, 0, red_hue_image); Mat dilateElement = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(24, 24)); Mat erodeElement = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10, 10)); Imgproc.blur(red_hue_image, red_hue_image, new Size(11, 11)); // init//from w w w .j a v a 2s . c om List<MatOfPoint> contours = new ArrayList<>(); Mat hierarchy = new Mat(); // find contours Imgproc.findContours(red_hue_image, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE); System.out.println("After findcontours"); // if any contour exist... if (hierarchy.size().height > 0 && hierarchy.size().width > 0) { // for each contour, display it in blue for (int idx = 0; idx >= 0; idx = (int) hierarchy.get(0, idx)[0]) { System.out.println(idx); // Imgproc.drawContours(frame, contours, idx, new Scalar(250, 0, 0), 3); } } MatOfPoint2f approxCurve = new MatOfPoint2f(); //For each contour found MatOfPoint2f contour2f = null; RotatedRect rotatedrect = null; for (MatOfPoint contour : contours) { //Convert contours(i) from MatOfPoint to MatOfPoint2f if (contour2f == null) contour2f = new MatOfPoint2f(contour.toArray()); if (contour.size().area() > contour2f.size().area()) { contour2f = new MatOfPoint2f(contour.toArray()); } } try { Imgproc.fitEllipse(contour2f); rotatedrect = Imgproc.fitEllipse(contour2f); double approxDistance = Imgproc.arcLength(contour2f, true) * 0.02; Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true); //Convert back to MatOfPoint MatOfPoint points = new MatOfPoint(approxCurve.toArray()); // Get bounding rect of contour Rect rect = Imgproc.boundingRect(points); // draw enclosing rectangle (all same color, but you could use variable i to make them unique) Imgproc.rectangle(sourceImg, rect.tl(), rect.br(), new Scalar(255, 0, 0), 1, 8, 0); Imgproc.ellipse(sourceImg, rotatedrect, new Scalar(255, 192, 203), 4, 8); } catch (CvException e) { e.printStackTrace(); System.out.println("Ingen ellipse fundet"); } return sourceImg; }
From source file:digimesh.xbee.gui.SensorMap.java
private void drawSensor(SmartSensor sensor) { Point sensorLocation = new Point(sensor.getPositionXY().positionX - 50, sensor.getPositionXY().positionY + 30); int measNr = sensor.getMeasurmentToDraw(); String id = sensor.getId();//from w w w .jav a 2 s.c o m String sTextName = "Sensor ID : " + id.substring(id.length() - 4, id.length()); int fontFace = Core.FONT_HERSHEY_PLAIN; double fontScale = 0.7; Imgproc.putText(map, sTextName, sensorLocation, fontFace, fontScale, Scalar.all(255)); Scalar circleColor = new Scalar(255, 255, 255); if (sensor.hasMeasurements) { double value = sensor.m_measurments.get(measNr).value; String measName = sensor.m_measurments.get(measNr).name; String unit = sensor.m_measurments.get(measNr).unit; String sTextMeas = measName + " = " + value + "[" + unit + "]"; Imgproc.putText(map, sTextMeas, new Point(sensor.getPositionXY().positionX - 50, (sensor.getPositionXY().positionY + 50)), fontFace, fontScale, Scalar.all(255)); double upperLimit = sensor.m_measurments.get(measNr).upperLimit; double lowerLimit = sensor.m_measurments.get(measNr).lowerLimit; //check value if (value > upperLimit) { circleColor = new Scalar(0, 0, 255); } else if (value < lowerLimit) { circleColor = new Scalar(255, 0, 0); } else { circleColor = new Scalar(0, 255, 0); } } else { Imgproc.putText(map, HUB_LABEL, new Point(sensor.getPositionXY().positionX - 50, (sensor.getPositionXY().positionY + 12)), fontFace, fontScale, Scalar.all(255)); } Imgproc.circle(map, sensorLocation, 10, circleColor, 2, fontFace, 0); }
From source file:digitalassistant.Panel.java
public void face_detect(Mat image) { //System.out.println("\nRunning DetectFaceDemo"); // Create a face detector from the cascade file in the resources // directory. String test1 = getClass().getResource("lbpcascade_frontalface.xml").getPath(); test1 = test1.replace("/C:", "C:"); CascadeClassifier faceDetector = new CascadeClassifier(test1); /* String test=getClass().getResource("lena.png").getPath(); test = test.replace("/C:", "C:");//from ww w.jav a 2 s .c o m System.out.println(test); Mat image = Highgui.imread(test);*/ // Detect faces in the image. // MatOfRect is a special container class for Rect. MatOfRect faceDetections = new MatOfRect(); faceDetector.detectMultiScale(image, faceDetections); // System.out.println(String.format("Detected %s faces", faceDetections.toArray().length)); // 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)); } // Save the visualized detection. // String filename = "faceDetection.png"; // System.out.println(String.format("Writing %s", filename)); // Highgui.imwrite(filename, image); }
From source file:Domain.ImgProcess.java
public Mat DetectarFace(Canvas c, Mat image) throws IOException { System.out.println("Rodando DetectFace"); CascadeClassifier faceDetector = new CascadeClassifier( "C:\\Users\\Gertrude\\Documents\\NetBeansProjects\\OWL_Sight\\src\\Resources\\lbpcascade_frontalface.xml"); // seleciona o classificador para identificao da face MatOfRect faceDetections = new MatOfRect(); //matriz de rectangulos representando os rosto encontrados faceDetector.detectMultiScale(image, faceDetections);//detecta multiplas faces na imagem fornecida System.out.printf("Detected %s faces", faceDetections.toArray().length); Mat corte = null;//from w ww .j a va 2s .co m 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, 0, 0));// desenha os rectangulos // Highgui.imwrite("C:/TCC-FacialRecognize/src/resources/TESTECORTE.jpg", utl.corte(image, rect)); corte = new Mat(image, rect); } Size sz = new Size(120, 120); MatOfByte bytemat = new MatOfByte(); //transformando a matriz image em uma matriz de bytes para carga no canvas do form principal Highgui.imencode(".jpg", corte, bytemat); byte[] bytes = bytemat.toArray(); InputStream in = new ByteArrayInputStream(bytes); BufferedImage render = ImageIO.read(in); Graphics g = c.getGraphics(); g.drawImage(render, 1, 1, c.getHeight(), c.getWidth(), c); return corte; }
From source file:dr.Interface.java
private void detect_BtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_detect_BtnMouseClicked if (file != null) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); CascadeClassifier faceDetector = new CascadeClassifier(HAAR_FILE_PATH); CascadeClassifier eyeDetector = new CascadeClassifier("src//dr//haarcascade_eye.xml"); Mat image = Highgui.imread(file.getAbsolutePath()); MatOfRect faceDetections = new MatOfRect(); MatOfRect eyeDetections = new MatOfRect(); faceDetector.detectMultiScale(image, faceDetections, scale, minN, 0, min, max); 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)); }// w w w . ja v a 2 s . c o m if (eye_detection.isSelected()) { eyeDetector.detectMultiScale(image, eyeDetections, e_scale, e_minN, 0, e_min, e_max); for (Rect rect : eyeDetections.toArray()) { Core.circle(image, new Point(rect.x + rect.width / 2, rect.y + rect.width / 2), rect.width / 2, new Scalar(255, 0, 255)); Core.circle(image, new Point(rect.x + rect.width / 2, rect.y + rect.width / 2), rect.width / 2 + 1, new Scalar(255, 0, 255)); Core.circle(image, new Point(rect.x + rect.width / 2, rect.y + rect.width / 2), rect.width / 2 + 1, new Scalar(255, 0, 255)); } } String filename = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf("\\")) + "\\out.png"; Highgui.imwrite(filename, image); BufferedImage i; try { if ((i = ImageIO.read(new File(filename))) != null) { current_image = i; loadImage_Lbl.setIcon(new ImageIcon(resize_image(i))); } else { JOptionPane.showMessageDialog(this, "Nu s-a detectat nimic!", "Info", JOptionPane.INFORMATION_MESSAGE); } } catch (IOException ex) { Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:drawing.application.FaceDetection.java
License:Open Source License
@Override public void run() { CascadeClassifier faceDetector = new CascadeClassifier("lbpcascade_frontalface.xml"); Mat image = Imgcodecs.imread(filePath); MatOfRect faceDetections = new MatOfRect(); faceDetector.detectMultiScale(image, faceDetections); for (Rect rect : faceDetections.toArray()) { Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0)); }/*from w w w.ja v a 2 s . co m*/ System.out.println(String.format("Writing %s", filePath)); Imgcodecs.imwrite(filePath, image); int numFaces = faceDetections.toArray().length; JOptionPane.showMessageDialog(null, "Detected " + faceDetections.toArray().length + (numFaces == 1 ? " face" : " faces")); }
From source file:edu.fiu.cate.breader.BaseSegmentation.java
/** * Finds the bounding box for the book on the stand using * the high resolution image.//ww w . j av a 2 s. c o m * @param src- High Resolution image of the book * @return Rectangle delineating the book */ public Rect highRes(Mat src) { Mat dst = src.clone(); Imgproc.blur(src, dst, new Size(100.0, 100.0), new Point(-1, -1), Core.BORDER_REPLICATE); Imgproc.threshold(dst, dst, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU); Imgproc.Canny(dst, dst, 50, 200, 3, false); List<MatOfPoint> contours = new LinkedList<>(); Mat hierarchy = new Mat(); Imgproc.findContours(dst, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0)); Mat color = new Mat(); Imgproc.cvtColor(src, color, Imgproc.COLOR_GRAY2BGR); for (int k = 0; k < contours.size(); k++) { byte[] vals = ITools.getHeatMapColor((float) k / (float) contours.size()); Imgproc.drawContours(color, contours, k, new Scalar(vals[0], vals[1], vals[2]), 8); } new IViewer("HighRes Contours ", BReaderTools.bufferedImageFromMat(color)); Point center = new Point(src.cols() / 2, src.rows() / 2); //Check hierarchy tree int[] res = polySearch(center, hierarchy, contours, 0); while (res[0] != 1 && res[2] != -1) { res = polySearch(center, hierarchy, contours, res[2]); if (res[0] == 1) break; } MatOfInt tHull = new MatOfInt(); int index = 0; if (res[1] != -1) { index = res[1]; } Imgproc.convexHull(contours.get(index), tHull); //get bounding box MatOfPoint cont = contours.get(index); Point[] points = new Point[tHull.rows()]; for (int i = 0; i < tHull.rows(); i++) { int pIndex = (int) tHull.get(i, 0)[0]; points[i] = new Point(cont.get(pIndex, 0)); } Rect out = Imgproc.boundingRect(new MatOfPoint(points)); return out; }
From source file:edu.fiu.cate.breader.BaseSegmentation.java
/** * Finds the bounding box for the book on the stand using * the depth average image.//from w w w .j av a 2 s .c o m * @param src- The Depth average image * @return Rectangle delineating the book */ public Rect lowResDist(Mat src) { Mat dst = src.clone(); Imgproc.blur(src, dst, new Size(5, 5), new Point(-1, -1), Core.BORDER_REPLICATE); // Imgproc.threshold(dst, dst, 0,255,Imgproc.THRESH_BINARY_INV+Imgproc.THRESH_OTSU); Imgproc.Canny(dst, dst, 50, 200, 3, false); // Canny(src, dst, 20, 60, 3); List<MatOfPoint> contours = new LinkedList<>(); Mat hierarchy = new Mat(); /// Find contours Imgproc.findContours(dst, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0)); Mat color = new Mat(); Imgproc.cvtColor(src, color, Imgproc.COLOR_GRAY2BGR); for (int k = 0; k < contours.size(); k++) { byte[] vals = ITools.getHeatMapColor((float) k / (float) contours.size()); Imgproc.drawContours(color, contours, k, new Scalar(vals[0], vals[1], vals[2]), 1); } new IViewer("LowRes Contours ", BReaderTools.bufferedImageFromMat(color)); for (int k = 0; k < contours.size(); k++) { MatOfPoint2f tMat = new MatOfPoint2f(); Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(k).toArray()), tMat, 5, true); contours.set(k, new MatOfPoint(tMat.toArray())); } List<Point> points = new LinkedList<Point>(); for (int i = 0; i < contours.size(); i++) { points.addAll(contours.get(i).toList()); } MatOfInt tHull = new MatOfInt(); Imgproc.convexHull(new MatOfPoint(points.toArray(new Point[points.size()])), tHull); //get bounding box Point[] tHullPoints = new Point[tHull.rows()]; for (int i = 0; i < tHull.rows(); i++) { int pIndex = (int) tHull.get(i, 0)[0]; tHullPoints[i] = points.get(pIndex); } Rect out = Imgproc.boundingRect(new MatOfPoint(tHullPoints)); return out; }