List of usage examples for org.opencv.core Mat width
public int width()
From source file:bgslibrary.Utils.java
License:Open Source License
static final public void imshow(Mat image) { try {/* w ww. j a v a 2 s .c om*/ BufferedImage bufImage = toBufferedImage(image); JFrame frame = new JFrame("Image"); frame.getContentPane().setLayout(new FlowLayout()); frame.getContentPane().add(new JLabel(new ImageIcon(bufImage))); frame.pack(); frame.setSize(image.width() + 40, image.height() + 60); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } catch (Exception e) { e.printStackTrace(); } }
From source file:br.cefetmg.lsi.opencv.multipleObjectTracking.processing.MultipleObjectTracking.java
License:Open Source License
private void setFramesSizes(Mat image) { frameCamera.setSize(image.width() + 20, image.height() + 60); if (calibrationMode) { frameThreshold.setSize(image.width() + 20, image.height() + 60); }// w ww . j a va 2 s . c o m }
From source file:by.zuyeu.deyestracker.core.detection.DemoPanel.java
/** * Converts/writes a Mat into a BufferedImage. * * @param matrix Mat of type CV_8UC3 or CV_8UC1 * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY *//* www . java2 s . c om*/ public boolean convertMatToBufferedImage(Mat matBGR) { int width = matBGR.width(), height = matBGR.height(), channels = matBGR.channels(); byte[] sourcePixels = new byte[width * height * channels]; matBGR.get(0, 0, sourcePixels); // create new image and get reference to backing data image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length); return true; }
From source file:by.zuyeu.deyestracker.core.detection.DemoPanel.java
public static void main(String arg[]) throws DEyesTrackerException, InterruptedException, ExecutionException { LOG.info("main - start;"); final String windowName = "Capture - Face detection"; final JFrame frame = new JFrame(windowName); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400);/*from w w w. j a v a2 s. c o m*/ final DemoPanel demoPanel = new DemoPanel(); frame.setContentPane(demoPanel); frame.setVisible(true); //-- 2. Read the video stream final FaceInfoSampler sampler = new FaceInfoSampler(); final IFrameCapture capture = sampler.getCapture(); final Scalar faceRegionColor = new Scalar(0, 255, 0); final Scalar eyesRegionColor = new Scalar(120, 120, 120); final ExecutorService executorService = Executors.newSingleThreadExecutor(); FutureTask<DetectFaceSample> detectFaceTask = TaskUtils.wrapFutureAnd(new DetectTask(sampler), executorService); DetectFaceSample sample = new DetectFaceSample(); while (true) { final Mat webcamImage = capture.getNextFrame(); if (webcamImage != null && !webcamImage.empty()) { frame.setSize(webcamImage.width() + 40, webcamImage.height() + 60); if (detectFaceTask.isDone()) { sample = detectFaceTask.get(); detectFaceTask = TaskUtils.wrapFutureAnd(new DetectTask(sampler), executorService); } if (sample.getFace() != null) { addRectangleToImage(sample.getFace(), webcamImage, faceRegionColor); } if (sample.getLeftEye() != null) { addRectangleToImage(sample.getLeftEye(), webcamImage, eyesRegionColor); } if (sample.getRightEye() != null) { addRectangleToImage(sample.getRightEye(), webcamImage, eyesRegionColor); } if (sample.getLeftPupil() != null) { drawCircle(webcamImage, sample.getLeftPupil()); } if (sample.getRightPupil() != null) { drawCircle(webcamImage, sample.getRightPupil()); } //-- 4. Display the image demoPanel.convertMatToBufferedImage(webcamImage); // We could look at the error... demoPanel.repaint(); } } }
From source file:by.zuyeu.deyestracker.core.util.CVCoreUtils.java
public static Mat selectSubmatByRect(Rect rect, Mat image) { double colScale = 1.0 * image.cols() / image.width(); int colStart = (int) (1.0 * rect.x * colScale); int colEnd = (int) (1.0 * (rect.x + rect.width) * colScale); double rowScale = 1.0 * image.rows() / image.height(); int rowStart = (int) (1.0 * rect.y * rowScale); int rowEnd = (int) (1.0 * (rect.y + rect.height) * rowScale); return image.submat(rowStart, rowEnd, colStart, colEnd); }
From source file:by.zuyeu.deyestracker.core.util.CVCoreUtils.java
public static void insertSubmatByRect(Mat subImage, Rect rect, Mat origImage) { double colScale = 1.0 * origImage.cols() / origImage.width(); int colStart = (int) (1.0 * rect.x * colScale); double rowScale = 1.0 * origImage.rows() / origImage.height(); int rowStart = (int) (1.0 * rect.y * rowScale); for (int x1 = 0, x2 = colStart; x1 < subImage.cols(); x1++, x2++) { for (int y1 = 0, y2 = rowStart; y1 < subImage.rows(); y1++, y2++) { final double[] subImgData = subImage.get(y1, x1); origImage.put(y2, x2, subImgData); }//from w ww . jav a 2s. co m } }
From source file:cctvanalization.FXMLDocumentController.java
private static void saveFrame(Mat currentFrame, String fileName, String ext) { File file = new File(fileName + "." + ext); BufferedImage image = new BufferedImage(currentFrame.width(), currentFrame.height(), BufferedImage.TYPE_BYTE_GRAY); try {/*from www . j a v a 2s . c o m*/ ImageIO.write(image, ext, file); // ignore returned boolean } catch (IOException e) { System.out.println("Write error for " + file.getPath() + ": " + e.getMessage()); } }
From source file:ch.hslu.pren.t37.camera.BildAuswertungKorb.java
public int bildAuswerten() { //Bild in dem gesucht werden soll String inFile = "../camera.jpg"; //das Bild dass im infile gesucht wird String templateFile = "../Bilder/korb.jpg"; //Lsung wird in diesem Bild prsentiert String outFile = "../LoesungsBild.jpg"; //berprfungswert wird gesetzt int match_method = Imgproc.TM_CCOEFF_NORMED; //das original Bild und das zu suchende werden geladen Mat img = Highgui.imread(inFile, Highgui.CV_LOAD_IMAGE_COLOR); Mat templ = Highgui.imread(templateFile, Highgui.CV_LOAD_IMAGE_COLOR); // Lsungsmatrix generieren int result_cols = img.cols() - templ.cols() + 1; int result_rows = img.rows() - templ.rows() + 1; Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1); // Suchen und normalisieren Imgproc.matchTemplate(img, templ, result, match_method); Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat()); // Mit MinMax Logik wird der beste "Match" gesucht Core.MinMaxLocResult mmr = Core.minMaxLoc(result); Point matchLoc;//from w ww .j a v a 2 s. co m if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) { matchLoc = mmr.minLoc; } else { matchLoc = mmr.maxLoc; } // Darstellen Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(), matchLoc.y + templ.rows()), new Scalar(0, 255, 0), 10); // Alle 4 Eckpunkte speichern Point topLeft = new Point(matchLoc.x, matchLoc.y); Point topRight = new Point(matchLoc.x + templ.cols(), matchLoc.y); Point downLeft = new Point(matchLoc.x, matchLoc.y + templ.rows()); Point downRight = new Point(matchLoc.x + templ.cols(), matchLoc.y + templ.rows()); // Lsungsbild speichern Highgui.imwrite(outFile, img); //Mittelpunkt berechnen double mittePicture; double mitteKorb; double differnez; Mat sol = Highgui.imread(outFile, Highgui.CV_LOAD_IMAGE_COLOR); mittePicture = sol.width() / 2; mitteKorb = (topRight.x - topLeft.x) / 2; mitteKorb = topLeft.x + mitteKorb; differnez = mitteKorb - mittePicture; logger.log(PrenLogger.LogLevel.DEBUG, "Mitte Korb: " + mitteKorb); logger.log(PrenLogger.LogLevel.DEBUG, "Mitte Bild: " + mittePicture); logger.log(PrenLogger.LogLevel.DEBUG, "Differenz: " + differnez + "\nWenn Differnez negativ, nach rechts drehen"); return (int) differnez; }
From source file:Clases.Analizador.java
public BufferedImage analizarMelanoma(Mat img) { int totalP, areaM, tamImg = img.width() * img.height(); Mat imagenCopia = img.clone();//from w ww .j av a 2s .com Mat imagenCopia1 = img.clone(); img = segmentador.dameLaDona(img); Mat img2 = segmentador.dameLaDona(imagenCopia); img2 = segmentador.descompCanImg(img2, 1); img2 = segmentador.umbralizarImg(img2, 254, 255); img = segmentador.blurearImg(img, 25); img = segmentador.descompCanImg(img, 1); //Ventana v = new Ventana(convertir(img2), 0, 0); totalP = tamImg - countNonZero(img2); img = segmentador.umbralizarImg(img, 30, 150); img = segmentador.umbralizarImg(img, 0, 255); areaM = tamImg - countNonZero(img); resultadoMelanoma = (areaM * 100) / (double) totalP; resultadoMelanoma = (double) ((int) (resultadoMelanoma * 100.00) / 100); System.out.println("El area afectada por melanoma es: " + resultadoMelanoma + "%"); System.out.println("El area de iris abarca: " + totalP); System.out.println("El total de pixeles es: " + tamImg); addWeighted(segmentador.descompCanImg(imagenCopia1, 2), 1, img, 0.3, 0.0, img); //Ventana v1 = new Ventana(segmentador.convertir(img), 1, 0); return segmentador.Mat2BufferedImage(img); }
From source file:classes.TextExtractor.java
public void extractText(Rect roi, double roiAngle) throws Exception { Point roiTopLeft = roi.tl();/*from w ww .j a va 2 s . c om*/ double radians = Math.toRadians(roiAngle); double sin = Math.abs(Math.sin(radians)); double cos = Math.abs(Math.cos(radians)); int newWidth = (int) (image.width() * cos + image.height() * sin); int newHeight = (int) (image.width() * sin + image.height() * cos); int[] newWidthHeight = { newWidth, newHeight }; int pivotX = newWidthHeight[0] / 2; int pivotY = newWidthHeight[1] / 2; Point center = new Point(pivotX, pivotY); Size targetSize = new Size(newWidthHeight[0], newWidthHeight[1]); Mat intermediateImage = new Mat(targetSize, image.type()); int offsetX = (newWidthHeight[0] - image.width()) / 2; int offsetY = (newWidthHeight[1] - image.height()) / 2; Point paddedTopLeft = new Point(roiTopLeft.x + offsetX, roiTopLeft.y + offsetY); Mat containerImage = intermediateImage.submat(offsetY, offsetY + image.height(), offsetX, offsetX + image.width()); image.copyTo(containerImage); Mat rotationMatrix = Imgproc.getRotationMatrix2D(center, roiAngle, 1.0); Point transformedTopLeft = transformPoint(paddedTopLeft, rotationMatrix); Mat rotatedImage = new Mat(); Imgproc.warpAffine(intermediateImage, rotatedImage, rotationMatrix, targetSize, Imgproc.INTER_LINEAR, Imgproc.BORDER_CONSTANT, new Scalar(0)); ImageUtils.saveImage(rotatedImage, imageID + "_rotatedImage.png", request); double adjustedWidth = roi.size().width; double adjustedHeight = roi.size().height; if (transformedTopLeft.x + adjustedWidth > rotatedImage.width()) { adjustedWidth = rotatedImage.width() - transformedTopLeft.x; } if (transformedTopLeft.y + adjustedHeight > rotatedImage.height()) { adjustedHeight = rotatedImage.height() - transformedTopLeft.y; } Rect newROI = new Rect(transformedTopLeft, new Size(adjustedWidth, adjustedHeight)); Mat extractedROI = new Mat(rotatedImage, newROI); String fileName = ImageUtils.saveImage(extractedROI, imageID + "_ROI.png", request); extractText(fileName); }