List of usage examples for org.opencv.core Mat copyTo
public void copyTo(Mat m)
From source file:tk.year.opencv.demo.filters.Erode.java
License:Open Source License
@Override public Mat filter(final Mat src) { final Mat dst = new Mat(src.cols(), src.rows(), CvType.CV_8UC3); src.copyTo(dst); Imgproc.erode(dst, dst, structuringElement); return dst;//from ww w.j av a2 s. c o m }
From source file:tk.year.opencv.demo.filters.FindContours.java
License:Open Source License
@Override public Mat filter(final Mat src) { final Mat dst = new Mat(src.rows(), src.cols(), src.type()); src.copyTo(dst); Imgproc.cvtColor(dst, dst, Imgproc.COLOR_BGR2GRAY); final List<MatOfPoint> points = new ArrayList<>(); final Mat hierarchy = new Mat(); Imgproc.findContours(dst, points, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); Imgproc.cvtColor(dst, dst, Imgproc.COLOR_GRAY2BGR); return dst;//from w w w . ja va 2s. c o m }
From source file:uk.ac.horizon.aestheticodes.controllers.MatTranform.java
License:Open Source License
static void rotate(Mat src, Mat dst, int angle, boolean flip) { if (src != dst) { src.copyTo(dst); }//w w w . j av a2s .c om angle = ((angle / 90) % 4) * 90; //0 : flip vertical; 1 flip horizontal int flip_horizontal_or_vertical = angle > 0 ? 1 : 0; if (flip) { flip_horizontal_or_vertical = -1; } int number = Math.abs(angle / 90); for (int i = 0; i != number; ++i) { Core.transpose(dst, dst); Core.flip(dst, dst, flip_horizontal_or_vertical); } }
From source file:view.TelaComCaptura.java
public void mostraVideo(JPanel containerVideo, Thread t) { Utils ut = new Utils(); Graphics g = containerVideo.getGraphics(); //Matriz que contem os dados da imagem Mat frame = new Mat(); Mat frameSemRetangulo = new Mat(); if (captura != null) { if (captura.isOpened()) { captura.release();// www . j ava2 s .com } } try { captura = new VideoCapture(0); } catch (Exception e) { Utils.msgErro("Erro ao iniciar webcam", "Erro"); } ClassificadorFacial.classificador = new CascadeClassifier( "resources\\cascades\\haarcascade_frontalface_alt.xml"); // String cascadeFile = localPath + "\\resources\\cascades\\haarcascade_frontalface_alt.xml"; //String cascadePath = cascadeFile; //CascadeClassifier classificador = new CascadeClassifier(); //classificador.load(cascadeFile); // if (!classificador.load(cascadeFile)) { // System.out.println("Erro ao carregar cascade file"); // // if (!new File(cascadeFile).canRead()) { // new File(cascadeFile).setReadable(true); // System.out.println("Arquivo travado"); // } // // return; // } else { // if (new File(cascadeFile).canRead()) { // System.out.println("Arquivo liberado"); // } // } MatOfRect facesDetectadas = new MatOfRect(); double scaleFactor = 1.1; int minNeighbors = 2; int flags = 0; Size minSize = new Size(30, 30); Size maxSize = new Size(500, 500); Scalar cor = new Scalar(0, 255, 0); int larguraPanel = containerVideo.getWidth(); int alturaPanel = containerVideo.getHeight(); int thickness = 3; while (!t.isInterrupted()) { // try { // Thread.sleep(100); // } catch (InterruptedException ex) { // Logger.getLogger(TelaComCaptura.class.getName()).log(Level.SEVERE, null, ex); // } if (captura.isOpened()) { //Captura um frame captura.read(frame); if (!frame.empty()) { // setSize(frame.width(), frame.height()); Mat imagemColorida = frame; Mat imagemCinza = new Mat(); Imgproc.cvtColor(imagemColorida, imagemCinza, Imgproc.COLOR_BGR2GRAY); //Detecta faces ClassificadorFacial.classificador.detectMultiScale(imagemCinza, facesDetectadas, scaleFactor, minNeighbors, flags, minSize, maxSize); Rect[] faces = facesDetectadas.toArray(); frame.copyTo(frameSemRetangulo); Rect faceRecortada = null; if (lstFacesRecortadas.size() > faces.length) { lstFacesRecortadas.clear(); } for (int i = 0; i < faces.length; i++) { Imgproc.rectangle(frame, faces[i].tl(), faces[i].br(), cor, thickness); int larguraFace = (int) (faces[i].width * 0.8f); int alturaFace = (int) (faces[i].height * 0.8f); faceRecortada = new Rect(faces[i].x + 20, faces[i].y + 20, larguraFace, alturaFace); // faceRecortada = new Rect(faces[i].x, faces[i].y, 150, 160); if (faceRecortada != null) { // if (lstFacesRecortadas.size() < faces.length) { //System.out.println("Pessoa entrou da captura"); try { //lstFacesRecortadas.add(ut.matToBufferedImage(new Mat(frameSemRetangulo, faceRecortada))); lstFacesRecortadas.add(new Mat(frameSemRetangulo, faceRecortada)); } catch (Exception e) { } // } else if (lstFacesRecortadas.size() > faces.length) { // //System.out.println("Pessoa saiu da captura"); // lstFacesRecortadas.clear(); // lstFacesRecortadas.add(ut.matToBufferedImage(new Mat(frameSemRetangulo, faceRecortada))); // } } } BufferedImage frameLimpo = ut.matToBufferedImage(frameSemRetangulo); BufferedImage frameComRetangulos = ut.matToBufferedImage(frame); this.setFrameAtual(frameLimpo); g.drawImage(frameComRetangulos, 0, 0, larguraPanel, alturaPanel, null); //Limpa variveis imagemCinza = null; faceRecortada = null; frameLimpo = null; if (!isSalvandoPGM()) { //lstFacesRecortadas.clear(); } frameComRetangulos = null; } } } captura.release(); //new File(cascadeFile). System.out.println("Captura finalizada"); }
From source file:webcamfacedetect.Processor.java
public Mat detect(Mat inputframe) { Mat mRgba = new Mat(); Mat mGrey = new Mat(); MatOfRect faces = new MatOfRect(); inputframe.copyTo(mRgba); inputframe.copyTo(mGrey);/*from ww w.j av a 2 s . c o m*/ Imgproc.cvtColor(mRgba, mGrey, Imgproc.COLOR_BGR2GRAY); Imgproc.equalizeHist(mGrey, mGrey); face_cascade.detectMultiScale(mGrey, faces); System.out.println(String.format("Detected %s faces.", faces.toArray().length)); for (Rect rect : faces.toArray()) { Point center = new Point(rect.x + rect.width * 0.5, rect.y + rect.height * 0.5); Imgproc.ellipse(mRgba, center, new Size(rect.width * 0.5, rect.height * 0.5), 0, 0, 360, new Scalar(255, 0, 255), 4, 8, 0); } return mRgba; }