List of usage examples for org.opencv.core Mat copyTo
public void copyTo(Mat m, Mat mask)
From source file:contador_de_moedas.Segmentacao.java
/** * Apply Canny/*from w ww .j a v a2s. c o m*/ * * @param img the current frame * @return an image elaborated with Canny */ private Mat doCanny(Mat img) { // inicia Mat grayImage = new Mat(); Mat detectedEdges = new Mat(); // Reduzir o rudo com um kernel 3x3 Imgproc.blur(grayImage, detectedEdges, new Size(3, 3)); //Detector canny, com relao de menor: limite de superior de 3: 1 Imgproc.Canny(detectedEdges, detectedEdges, this.threshold.getValue(), this.threshold.getValue() * 3); //Usando Canny e exibindo o resultadosada como uma mscara. Mat dest = new Mat(); img.copyTo(dest, detectedEdges); return dest; }
From source file:cv.recon.controller.OutputDisplayController.java
License:Open Source License
/** * Subtract background using BackgroundSubtractorMOG2 * @param src Source Mat/*from w ww . j a v a 2s.co m*/ */ private void subtractBackground(Mat src) { if (bsmog != null) { bsmog.apply(src, fgMask); Imgproc.erode(fgMask, fgMask, kernel); Imgproc.dilate(fgMask, fgMask, kernel); output.setTo(new Scalar(0)); src.copyTo(output, fgMask); if (isFirstFrame) { nonZeroCount = 0; isFirstFrame = false; } else { nonZeroCount = Core.countNonZero(fgMask); } nonZeroLabel.setText("" + nonZeroCount); } }
From source file:cv.recon.util.CannyDetector.java
License:Open Source License
/** * Apply Canny detector to Mat./*from ww w .j a v a2 s .c om*/ * @param src Input Mat * @param dst Output Mat */ public void detect(Mat src, Mat dst) { Mat src_gray = new Mat(); Mat detected_edges = new Mat(); Imgproc.cvtColor(src, src_gray, Imgproc.COLOR_RGB2GRAY); Imgproc.blur(src_gray, detected_edges, new Size(3, 3)); Imgproc.Canny(detected_edges, detected_edges, lowThreshold, lowThreshold * ratio, kernel_size, true); dst.setTo(new Scalar(0)); src.copyTo(dst, detected_edges); }
From source file:fuzzycv.MainFrame.java
private Mat docanny(Mat frame) { int treshValue = thresholdSlider.getValue(); Mat cvtImg = new Mat(); Mat detectedEdges = new Mat(); Imgproc.cvtColor(frame, cvtImg, Imgproc.COLOR_BGR2GRAY); Imgproc.blur(cvtImg, detectedEdges, new Size(3.0, 3.0)); Imgproc.Canny(detectedEdges, detectedEdges, treshValue, treshValue * 3, 3, false); Mat mask = new Mat(); Core.add(mask, Scalar.all(0), mask); frame.copyTo(mask, detectedEdges); return mask;// w w w. j a va2s . c o m }
From source file:fuzzycv.MainFrame.java
private Mat removeBG(Mat frame) { Mat hsvImg = new Mat(); List<Mat> hsvPlanes = new ArrayList<>(); Mat thresholdImg = new Mat(); //threshold the image with the histogram average value hsvImg.create(frame.size(), CvType.CV_8U); Imgproc.cvtColor(frame, hsvImg, Imgproc.COLOR_BGR2HSV); Core.split(hsvImg, hsvPlanes);/* w w w . j av a2s .co m*/ double threshValue = getHistoAvg(hsvImg, hsvPlanes.get(0)); if (inverseCheckBox.isSelected()) { Imgproc.threshold(hsvPlanes.get(0), thresholdImg, threshValue, 179.0, Imgproc.THRESH_BINARY_INV); } else { Imgproc.threshold(hsvPlanes.get(0), thresholdImg, threshValue, 179.0, Imgproc.THRESH_BINARY); } Imgproc.blur(thresholdImg, thresholdImg, new Size(5, 5)); // dilate to fill gaps, erode to smooth edges Imgproc.dilate(thresholdImg, thresholdImg, new Mat(), new Point(-1, 1), 6); Imgproc.erode(thresholdImg, thresholdImg, new Mat(), new Point(-1, 1), 6); Imgproc.threshold(thresholdImg, thresholdImg, threshValue, 179.0, Imgproc.THRESH_BINARY); // create the new image Mat foreground = new Mat(frame.size(), CvType.CV_8UC3, new Scalar(255, 255, 255)); frame.copyTo(foreground, thresholdImg); return foreground; }
From source file:javafx1.JavaFX1.java
private Mat doCanny(Mat frame) { // init//from w w w .j ava 2 s. c o m Mat grayImage = new Mat(); Mat detectedEdges = new Mat(); // convert to grayscale Imgproc.cvtColor(frame, grayImage, Imgproc.COLOR_BGR2GRAY); // reduce noise with a 3x3 kernel Imgproc.blur(grayImage, detectedEdges, new Size(2, 2)); // canny detector, with ratio of lower:upper threshold of 3:1 // Imgproc.Canny(detectedEdges, detectedEdges, 2, 2 * 3); // using Canny's output as a mask, display the result Mat dest = new Mat(); frame.copyTo(dest, detectedEdges); return dest; }
From source file:javafx1.JavaFX1.java
private Mat doBackgroundRemoval(Mat frame) { // init/*from www . j av a 2 s . c o m*/ Mat hsvImg = new Mat(); List<Mat> hsvPlanes = new ArrayList<>(); Mat thresholdImg = new Mat(); int thresh_type = Imgproc.THRESH_BINARY_INV; //inverse thresh_type = Imgproc.THRESH_BINARY; // threshold the image with the average hue value hsvImg.create(frame.size(), CvType.CV_8U); Imgproc.cvtColor(frame, hsvImg, Imgproc.COLOR_BGR2HSV); Core.split(hsvImg, hsvPlanes); // get the average hue value of the image double threshValue = this.getHistAverage(hsvImg, hsvPlanes.get(0)); Imgproc.threshold(hsvPlanes.get(0), thresholdImg, threshValue, 179.0, thresh_type); Imgproc.blur(thresholdImg, thresholdImg, new Size(5, 5)); // dilate to fill gaps, erode to smooth edges Imgproc.dilate(thresholdImg, thresholdImg, new Mat(), new Point(-1, -1), 1); Imgproc.erode(thresholdImg, thresholdImg, new Mat(), new Point(-1, -1), 3); Imgproc.threshold(thresholdImg, thresholdImg, threshValue, 179.0, Imgproc.THRESH_BINARY); // create the new image Mat foreground = new Mat(frame.size(), CvType.CV_8UC3, new Scalar(255, 255, 255)); frame.copyTo(foreground, thresholdImg); return foreground; }
From source file:org.ar.rubik.MonoChromatic.java
License:Open Source License
/** * Use mask operation and then min max./*from w w w .ja v a 2 s . com*/ * This solution consumes about 20 minutes per frame! * * @param original_image * @return */ @SuppressWarnings("unused") private static Mat monochromaticMedianImageFilterUtilizingOpenCv2(Mat original_image) { final Size imageSize = original_image.size(); final int numColumns = (int) original_image.size().width; final int numRows = (int) original_image.size().height; final int bufferSize = numColumns * numRows; final int span = (int) 7; final int accuracy = (int) 5; Mat hsv_image = new Mat(imageSize, CvType.CV_8UC3); Imgproc.cvtColor(original_image, hsv_image, Imgproc.COLOR_RGB2HLS); List<Mat> channels = new LinkedList<Mat>(); Core.split(hsv_image, channels); Mat hueMat = channels.get(0); Mat lumMat = channels.get(1); Mat satMat = channels.get(2); // Output byte array for speed efficiency Mat monochromatic_image = new Mat(imageSize, CvType.CV_8UC1); byte[] monochromaticByteArray = new byte[bufferSize]; Mat mask = Mat.zeros(numRows, numColumns, CvType.CV_8UC1); Log.i(Constants.TAG, "Begin MonoChromatic CV"); for (int row = 0; row < numRows; row++) { byte result_pixel = 0; for (int col = 0; col < numColumns; col++) { if (col < span || (col >= numColumns - span)) result_pixel = 0; // Just put in black else if (row < span || (row >= numRows - span)) result_pixel = 0; // Just put in black else { // Log.i(Constants.TAG, "Creating Mask at " + row +"," + col); Core.rectangle(mask, new Point(row, col), new Point(row + span, col + span), new Scalar(1, 1, 1)); // Core.MinMaxLocResult minMaxResult = Core.minMaxLoc(hueMat, mask); Mat subset = new Mat(); hueMat.copyTo(subset, mask); Core.MinMaxLocResult minMaxResult = Core.minMaxLoc(subset); if (((minMaxResult.maxVal - minMaxResult.maxVal) < accuracy)) //&& (lum_max - lum_min < accuracy) && (sat_max - sat_min < accuracy) ) result_pixel = (byte) 128; else result_pixel = (byte) 0; // Log.i(Constants.TAG, "Completed Mask at " + row +"," + col); Core.rectangle(mask, new Point(row, col), new Point(row + span, col + span), new Scalar(0, 0, 0)); } if ((col >= span / 2) && (row >= span / 2)) monochromaticByteArray[(row - span / 2) * numColumns + (col - span / 2)] = result_pixel; } Log.i(Constants.TAG, "Completed Row: " + row); } monochromatic_image.put(0, 0, monochromaticByteArray); Log.i(Constants.TAG, "Completed MonoChromatic CV"); // System.exit(0); return monochromatic_image; }
From source file:processdata.ExperimentalDataProcessingUI.java
private void jButtonProcessImageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonProcessImageActionPerformed try {/* ww w.j av a 2 s .c o m*/ // TODO add your handling code here: //load library System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // TODO add your handling code here: folderName = textField2.getText(); int currentFrameIndex = Integer.parseInt(initialFrameIndexBox.getText()) - 1; datasetIndex = Integer.parseInt(textField1.getText()); String videoImageFileName = "./videoFrames//" + folderName + "//" + "frame_outVideo_" + currentFrameIndex + ".jpg"; String depthFrameFileName = initialImagePath + datasetIndex + "//" + folderName + "//" + "depthData//" + "outDepthByte_" + currentFrameIndex; rgbFrame = Highgui.imread(videoImageFileName, Highgui.CV_LOAD_IMAGE_GRAYSCALE); depthFrame = depthDataProcessingUtilities.processDepthDataFile(depthFrameFileName, jSlider2.getValue(), jSlider1.getValue()); Mat[] backgroundFrames = readBackground(); rgbBackgroundFrame = backgroundFrames[0]; depthBackgroundFrame = backgroundFrames[1]; //subtract depth background Mat depthFrameBackgroundSubtracted = new Mat(); Core.subtract(depthBackgroundFrame, depthFrame, depthFrameBackgroundSubtracted); Imgproc.threshold(depthFrameBackgroundSubtracted, depthFrameBackgroundSubtracted, 0, 255, Imgproc.THRESH_BINARY); displayImage(Mat2BufferedImage( videoProcessingUtilities.resizeImage(depthFrameBackgroundSubtracted, new Size(448, 234))), depthBckgSubtractedFrames); //remove the red-colored elements from depth image and leave only blue ones Mat depthImageCleaned = new Mat(); Core.inRange(depthFrameBackgroundSubtracted, new Scalar(253, 0, 0), new Scalar(255, 0, 0), depthImageCleaned); //apply morphologic opening to remove noise Imgproc.morphologyEx(depthImageCleaned, depthImageCleaned, 2, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3))); displayImage( Mat2BufferedImage(videoProcessingUtilities.resizeImage(depthImageCleaned, new Size(448, 234))), depthCleanedFramesPanel); //apply the homographic transform to cleaned depth image Mat hDepthImageCleaned = videoProcessingUtilities.performHomographyTransformation(depthImageCleaned, new Size(1920, 1080)); //extract all contours //sort all extracted contours and choose top 2 //overlay top 2 contours on the image and fill them with white color //mask the rgb frame // do all necessary rotation operations //offer user to save the image //extract all suitable contours between MIN and MAX areas: MatOfPoint[] contours = videoProcessingUtilities.extractLargestContours(hDepthImageCleaned, 100000, 160000); System.out.println("Number of contorus extracted " + contours.length); //draw contours List<MatOfPoint> tempContours = new ArrayList<MatOfPoint>(); Mat hDepthImageCleanedContours = hDepthImageCleaned.clone(); for (MatOfPoint cnt : contours) { System.out.println("Extracted Contour Area is " + Imgproc.contourArea(cnt)); tempContours.add(cnt); } Imgproc.cvtColor(hDepthImageCleanedContours, hDepthImageCleanedContours, Imgproc.COLOR_GRAY2BGR); Imgproc.drawContours(hDepthImageCleanedContours, tempContours, -1, new Scalar(0, 0, 255), 5); displayImage( Mat2BufferedImage( videoProcessingUtilities.resizeImage(hDepthImageCleanedContours, new Size(448, 234))), extractedContoursPanel); //prepare final mask Mat hDepthImageFilledContours = new Mat(hDepthImageCleaned.rows(), hDepthImageCleaned.cols(), hDepthImageCleaned.type()); Imgproc.drawContours(hDepthImageFilledContours, tempContours, -1, new Scalar(255, 255, 255), -1); displayImage( Mat2BufferedImage( videoProcessingUtilities.resizeImage(hDepthImageFilledContours, new Size(448, 234))), maskedContoursPanel); //subtract video background // Mat rgbFrameBackgroundSubtracted = new Mat(); // Core.subtract(rgbBackgroundFrame,rgbFrame, rgbFrameBackgroundSubtracted, hDepthImageCleaned); // displayImage(Mat2BufferedImage(videoProcessingUtilities.resizeImage(rgbFrameBackgroundSubtracted, new Size(448,234))),videoBckgSubtractedFrames); // //mask Mat preMaskedRGBFrame = new Mat(); rgbFrame.copyTo(preMaskedRGBFrame, hDepthImageCleaned); displayImage( Mat2BufferedImage(videoProcessingUtilities.resizeImage(preMaskedRGBFrame, new Size(448, 234))), videoBckgSubtractedFrames); //postmask Mat betterMaskedRGBFrame = new Mat(); rgbFrame.copyTo(betterMaskedRGBFrame, hDepthImageFilledContours); displayImage( Mat2BufferedImage( videoProcessingUtilities.resizeImage(betterMaskedRGBFrame, new Size(448, 234))), videoMaskedPanel); //clear ArrayList containig all processed images finalImages.clear(); javax.swing.JLabel[] jLabArray = { extractedShoePanel1, extractedShoePanel2 }; //segment all images int panelIdx = 0; for (MatOfPoint contour : tempContours) { MatOfPoint2f newMatOfPoint2fContour = new MatOfPoint2f(contour.toArray()); RotatedRect finalROI = Imgproc.minAreaRect(newMatOfPoint2fContour); Mat newMask = videoProcessingUtilities.getContourMasked(hDepthImageFilledContours.clone(), contour); Mat imageROIRegistred = new Mat(); betterMaskedRGBFrame.copyTo(imageROIRegistred, newMask); Mat maskedRGBFrameROI = videoProcessingUtilities.rotateExtractedShoeprint(imageROIRegistred, finalROI, new Size(500, 750), 2); finalImages.add(maskedRGBFrameROI); displayImage( Mat2BufferedImage( videoProcessingUtilities.resizeImage(maskedRGBFrameROI, new Size(203, 250))), jLabArray[panelIdx]); panelIdx++; } //MatOfInt parameters = new MatOfInt(); //parameters.fromArray(Highgui.CV_IMWRITE_JPEG_QUALITY, 100); //Highgui.imwrite(".//backgrounds//"+"test.jpg", depthFrameBackgroundSubtracted, parameters); } catch (FileNotFoundException ex) { Logger.getLogger(ExperimentalDataProcessingUI.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:src.model.filters.CannyFilter.java
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("**______________CANNY_______________**"); try {/* w ww . j a va2 s . c o m*/ String imgInput = request.getParameter("name").toString(); String savePath = savePath(request); //____________________________________ Mat source = Imgcodecs.imread(savePath); Mat destination = new Mat(source.rows(), source.cols(), source.type()); Mat det = new Mat(source.rows(), source.cols(), source.type()); Imgproc.cvtColor(source, destination, Imgproc.COLOR_BGR2GRAY); Imgproc.blur(destination, det, new Size(3, 3)); Imgproc.Canny(det, det, 5, 15, 3, false); Mat dest = new Mat(); Core.add(dest, Scalar.all(0), dest); source.copyTo(dest, det); String output = savePath.substring(0, savePath.lastIndexOf(".")) + "_CA_temp.jpg"; imgInput = request.getParameter("name").toString(); String imgOutput = imgInput.substring(0, imgInput.lastIndexOf(".")) + "_CA_temp.jpg"; Imgcodecs.imwrite(output, dest); //____________________________________ System.out.println("output: " + output); System.out.println("imgOutput: " + imgOutput); publishImg(response, imgOutput); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } }