List of usage examples for org.opencv.core Scalar Scalar
public Scalar(double v0, double v1, double v2, double v3)
From source file:org.pidome.client.userdetection.faces.FD_Controller.java
/** * Perform face detection and show a rectangle around the detected face. * * @param frame the current frame// www . j av a 2s. c o m */ private void detectAndDisplay(Mat frame) { // init MatOfRect faces = new MatOfRect(); Mat grayFrame = new Mat(); // convert the frame in gray scale Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY); // equalize the frame histogram to improve the result Imgproc.equalizeHist(grayFrame, grayFrame); // compute minimum face size (20% of the frame height) if (this.absoluteFaceSize == 0) { int height = grayFrame.rows(); if (Math.round(height * 0.2f) > 0) { this.absoluteFaceSize = Math.round(height * 0.2f); } } // detect faces this.faceCascade.detectMultiScale(grayFrame, faces, 1.1, 2, Objdetect.CASCADE_SCALE_IMAGE | Objdetect.CASCADE_DO_ROUGH_SEARCH | Objdetect.CASCADE_FIND_BIGGEST_OBJECT, new Size(this.absoluteFaceSize, this.absoluteFaceSize), new Size()); // each rectangle in faces is a face Rect[] facesArray = faces.toArray(); if (facesArray.length == 1) { Point loc = facesArray[0].tl(); Size size = facesArray[0].size(); faceRect = new Rectangle(); synchronized (faceRect) { faceRect.setRect(loc.x, loc.y, size.width, size.height); } Core.rectangle(frame, loc, facesArray[0].br(), new Scalar(0, 255, 0, 255), 2); } }
From source file:org.pidome.client.video.capture.faces.recognition.FaceDetection.java
/** * Perform face detection.//from w w w.j a v a 2 s . com * When drawRectangle is set to true a green rectangle will be placed on the image around a detected face. * @param frame the current frame * @return The location and size of the detected face. */ public final FaceRect detectFace(Mat frame) { // init MatOfRect faces = new MatOfRect(); Mat grayFrame = new Mat(); // convert the frame in gray scale Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY); // equalize the frame histogram to improve the result Imgproc.equalizeHist(grayFrame, grayFrame); // compute minimum face size (20% of the frame height) if (absoluteFaceSize == 0) { int height = grayFrame.rows(); if (Math.round(height * 0.2f) > 0) { absoluteFaceSize = Math.round(height * 0.2f); } } // detect faces this.faceCascade.detectMultiScale(grayFrame, faces, 1.1, 2, Objdetect.CASCADE_SCALE_IMAGE | Objdetect.CASCADE_DO_ROUGH_SEARCH | Objdetect.CASCADE_FIND_BIGGEST_OBJECT, new Size(absoluteFaceSize, absoluteFaceSize), new Size()); // each rectangle in faces is a face Rect[] facesArray = faces.toArray(); FaceRect faceRect = new FaceRect(); if (facesArray.length == 1) { Point loc = facesArray[0].tl(); Size size = facesArray[0].size(); faceRect.setRect(loc.x + rectThickness, loc.y + rectThickness, size.width - rectThickness, size.height - rectThickness); if (drawRectangle) { Core.rectangle(frame, loc, facesArray[0].br(), new Scalar(0, 255, 0, 255), rectThickness); } } return faceRect; }
From source file:udp.server.ObjectTracker.java
private ArrayList<Float> getTargetError() { ArrayList<Float> angles = null; int x = getX(contours); int y = getY(contours); contours.clear();/*from w ww . j a va2 s. c o m*/ angles = new ArrayList<>(); if (x > 0) { Core.circle(webcam_image, new Point(x, y), 4, new Scalar(50, 49, 0, 255), 4); float centerX = webcam_image.width() / 2; float centerY = webcam_image.height() / 2; // CenterCirle Core.circle(webcam_image, new Point(centerX, centerY), 4, new Scalar(50, 49, 0, 255), 4); // Setup camera angles (from producer) float cameraAngleX = 60.0f; //70.42f; float cameraAngleY = 43.30f; // Calculate difference from x and y to center float pixErrorX = x - centerX; float pixErrorY = -y + centerY; // Calculate angle error in x and y direction angleErrorX = (pixErrorX / centerX) * cameraAngleX; angleErrorY = (pixErrorY / centerY) * cameraAngleY; Core.line(webcam_image, new Point(x, y), new Point(centerX, centerY), new Scalar(150, 150, 100)/*CV_BGR(100,10,10)*/, 3); //angles = new float[]{angleErrorX, angleErrorY}; //angles.add(angleErrorX); //angles.add(angleErrorY); } else { if (angleErrorX > 0) { angleErrorX = 255; angleErrorY = 255; } else { angleErrorX = -255; angleErrorY = -255; } } angles.add(angleErrorX); angles.add(angleErrorY); return angles; }
From source file:uk.ac.horizon.artcodes.detect.ImageBuffers.java
License:Open Source License
public Mat getOverlay(boolean clear) { if (overlayReady) { return overlay; }/*from w w w. j a va2 s. co m*/ if (currentBuffer == yuvBuffer) { this.setImage(this.getImageInGrey()); } rotate(currentBuffer); if (overlay == null) { overlay = new Mat(currentBuffer.rows(), currentBuffer.cols(), CvType.CV_8UC4); } if (clear) { overlay.setTo(new Scalar(0, 0, 0, 0)); } overlayReady = true; return overlay; }
From source file:uom.research.thalassemia.logic.BloodCellsManipulationImpl.java
/** * do Blood Cell Processing./* w w w. ja v a2s . c o m*/ * * @throws Exception Exception */ @Override public void doBloodCellProcessing() throws Exception { if (imageFile.isFile()) { // Load Native Library System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // Load Selected File onto Original Mat original = Imgcodecs.imread(imageFile.getAbsolutePath(), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED); // Create Mats with Original Mat's Size gray = original.clone(); smooth = original.clone(); canny = original.clone(); threshold = original.clone(); red = original.clone(); laplacian = original.clone(); circularsMat = original.clone(); ellipsesMat = original.clone(); // Sets Gray Scale to gray Map Imgproc.cvtColor(original, gray, Imgproc.COLOR_BGR2HSV); grayImage = convertMapToImage(gray); // Sest Red Colour Range Min and Max Scalar minc = new Scalar(95, 150, 75, 0); Scalar maxc = new Scalar(145, 255, 255, 0); // Sets Red Colour Mat for Given Range Core.inRange(original, maxc, maxc, red); redImage = convertMapToImage(red); int type = BufferedImage.TYPE_INT_RGB; //gray = Imgcodecs.imdecode(original, type); image = new BufferedImage(original.cols(), original.rows(), type); //Imgproc.GaussianBlur(mGray, mGray, new Size(15,15),50); //redImage = new BufferedImage(original.width(), original.height(), BufferedImage.TYPE_INT_RGB); //byte[] data2 = ((DataBufferByte) redImage.getRaster().getDataBuffer()).getData(); Imgproc.threshold(gray, threshold, 155, 255, Imgproc.THRESH_BINARY); //Imgproc.Laplacian(gray, laplacian, CvType.CV_8UC3);//CV_8UC1); //laplacianImage = convertMapToImage(laplacian); thresholdImage = convertMapToImage(threshold); Imgproc.Canny(threshold, canny, 50, 100, 3, false); cannyImage = convertMapToImage(canny); //Imgproc.cvtColor(canny, canny, Imgproc.COLOR_BGR2GRAY); circles = new Mat(); int imageArea = original.width() * original.height(); if (imageArea > 5100000) { Imgproc.HoughCircles(canny, circles, Imgproc.CV_HOUGH_GRADIENT, 8, //Inverse ratio 220, //Minimum distance between the centers of the detected circles. default 100 100, //Higher threshold for canny edge detector. default100 200, //Threshold at the center detection stage. default 200 90, //min radius. default 50 130 //max radius. default 90 ); } else if (imageArea > 1309500) { Imgproc.HoughCircles(canny, circles, Imgproc.CV_HOUGH_GRADIENT, 8, //Inverse ratio 110, //Minimum distance between the centers of the detected circles. default 100 100, //Higher threshold for canny edge detector. default100 150, //Threshold at the center detection stage. default 200 50, //min radius. default 50 67 //max radius. default 90 ); } else if (imageArea > 540000) { Imgproc.HoughCircles(canny, circles, Imgproc.CV_HOUGH_GRADIENT, 8, //Inverse ratio 110, //Minimum distance between the centers of the detected circles. default 100 100, //Higher threshold for canny edge detector. default100 200, //Threshold at the center detection stage. default 200 30, //min radius. default 50 65 //max radius. default 90 ); } else if (imageArea > 400000) { Imgproc.HoughCircles(canny, circles, Imgproc.CV_HOUGH_GRADIENT, 8, //Inverse ratio 50, //Minimum distance between the centers of the detected circles. default 100 60, //Higher threshold for canny edge detector. default100 100, //Threshold at the center detection stage. default 200 25, //min radius. default 50 40 //max radius. default 90 ); } if (circles.cols() > 0) { for (int x = 0; x < circles.cols(); x++) { double vCircle[] = circles.get(0, x); if (vCircle == null) { break; } Point pt = new Point(Math.round(vCircle[0]), Math.round(vCircle[1])); int radius = (int) Math.round(vCircle[2]); radiusList.add(radius); // draw the found circle Imgproc.circle(circularsMat, pt, radius, new Scalar(0, 255, 0), 3); } } // set Pallor circles circlesPallor = new Mat(); if (imageArea > 5100000) { Imgproc.HoughCircles(canny, circlesPallor, Imgproc.CV_HOUGH_GRADIENT, 8, //Inverse ratio 220, //Minimum distance between the centers of the detected circles 100, //Higher threshold for canny edge detector 150, //Threshold at the center detection stage 0, //min radius 70 //max radius ); } else if (imageArea > 1309500) { Imgproc.HoughCircles(canny, circlesPallor, Imgproc.CV_HOUGH_GRADIENT, 8, //Inverse ratio 110, //Minimum distance between the centers of the detected circles 50, //Higher threshold for canny edge detector 100, //Threshold at the center detection stage 0, //min radius 45 //max radius ); } else if (imageArea > 540000) { Imgproc.HoughCircles(canny, circlesPallor, Imgproc.CV_HOUGH_GRADIENT, 8, //Inverse ratio 100, //Minimum distance between the centers of the detected circles 50, //Higher threshold for canny edge detector 100, //Threshold at the center detection stage 0, //min radius 35 //max radius ); } else if (imageArea > 400000) { Imgproc.HoughCircles(canny, circlesPallor, Imgproc.CV_HOUGH_GRADIENT, 8, //Inverse ratio 50, //Minimum distance between the centers of the detected circles 10, //Higher threshold for canny edge detector 30, //Threshold at the center detection stage 0, //min radius 25 //max radius ); } if (circlesPallor.cols() > 0) { for (int x = 0; x < circlesPallor.cols(); x++) { double vCircle[] = circlesPallor.get(0, x); if (vCircle == null) { break; } Point pt = new Point(Math.round(vCircle[0]), Math.round(vCircle[1])); int radius = (int) Math.round(vCircle[2]); //radiusList.add(radius); // draw the found circle Imgproc.circle(circularsMat, pt, radius, new Scalar(0, 0, 255), 0); } } // set Ellipses contours = new ArrayList<>(); Imgproc.findContours(canny, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); MatOfPoint allcontours = new MatOfPoint(); for (MatOfPoint mat : contours) { mat.copyTo(allcontours); RotatedRect boundingEllipse = null; if (allcontours.toArray().length > 4) { MatOfPoint newMat1 = new MatOfPoint(allcontours.toArray()); MatOfPoint2f newMat2 = new MatOfPoint2f(allcontours.toArray()); Rect boundingRect = Imgproc.boundingRect(newMat1); boundingEllipse = Imgproc.fitEllipse(newMat2); } if (boundingEllipse != null) { Imgproc.ellipse(ellipsesMat, boundingEllipse, new Scalar(255, 0, 0), 2); } } circleCount = circles.cols(); circularsImage = convertMapToImage(circularsMat); ellipsesImage = convertMapToImage(ellipsesMat); } }
From source file:video.PictureView.java
public static BufferedImage setCross(BufferedImage img) { Mat imgMat;/*from w ww. jav a2 s.c om*/ imgMat = bufferedImageToMat(img); Imgproc.circle(imgMat, new Point(imgMat.width() / 2, imgMat.height() / 2), 4, new Scalar(255, 49, 255, 255)); img = mat2Img(imgMat); return img; }