List of usage examples for org.opencv.imgproc Imgproc convexHull
public static void convexHull(MatOfPoint points, MatOfInt hull, boolean clockwise)
From source file:OCV_ConvexHull.java
License:Open Source License
@Override public void run(ImageProcessor ip) { byte[] byteArray = (byte[]) ip.getPixels(); int w = ip.getWidth(); int h = ip.getHeight(); ArrayList<Point> lstPt = new ArrayList<Point>(); MatOfPoint pts = new MatOfPoint(); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if (byteArray[x + w * y] != 0) { lstPt.add(new Point((double) x, (double) y)); }/*ww w . j av a2 s. c o m*/ } } if (lstPt.isEmpty()) { return; } pts.fromList(lstPt); MatOfInt hull = new MatOfInt(); Imgproc.convexHull(pts, hull, enCW); showData(pts, hull); }
From source file:edu.ucue.tfc.Modelo.VideoProcessor.java
/** * Processes {@code firstFrame} and {@code secondFrame}. * @param firstFrame the first frame of a cycle. *///from ww w . j a va 2 s . co m private void processFrame(Mat firstFrame) { double contourArea = 0; int position = 0; try { /** * Redimensiona el el cuadro actual * */ Imgproc.resize(firstFrame, firstFrame, frameSize); /** * Convierte el cuadro por segundo a escala de grises */ Imgproc.cvtColor(firstFrame, firstGrayImage, Imgproc.COLOR_BGR2GRAY); /** * Lee el siguiente cuadro, lo redimensiona y convierte a escala de grises */ video.read(secondFrame); Imgproc.resize(secondFrame, secondFrame, frameSize); Imgproc.cvtColor(secondFrame, secondGrayImage, Imgproc.COLOR_BGR2GRAY); /** * Obtiene la diferencia absoluta por pixel de los cuadros anteriores. */ Core.absdiff(firstGrayImage, secondGrayImage, differenceOfImages); Imgproc.threshold(differenceOfImages, thresholdImage, 25, 255, Imgproc.THRESH_BINARY); Imgproc.blur(thresholdImage, thresholdImage, new Size(12, 12)); Imgproc.threshold(thresholdImage, thresholdImage, 20, 255, Imgproc.THRESH_BINARY); ///// for (int i = 0; i < contours.size(); ++i) { contours.get(i).release(); } contours.clear(); /** * La linea Horizontal */ Imgproc.line(firstFrame, controlPoints.get(6), controlPoints.get(7), new Scalar(255, 0, 0), Imgproc.LINE_4); Imgproc.findContours(thresholdImage, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); for (int i = 0; i < hullPoints.size(); ++i) { hullPoints.get(i).release(); } hullPoints.clear(); for (int i = 0; i < contours.size(); i++) { MatOfInt tmp = new MatOfInt(); Imgproc.convexHull(contours.get(i), tmp, false); hullPoints.add(tmp); } /** * Busca el contorno con el rea ms grande */ if (contours.size() > 0) { for (int i = 0; i < contours.size(); i++) { if (Imgproc.contourArea(contours.get(i)) > contourArea) { contourArea = Imgproc.contourArea(contours.get(i)); position = i; boundingRectangle = Imgproc.boundingRect(contours.get(i)); } } } secondFrame.release(); hierarchy.release(); secondGrayImage.release(); firstGrayImage.release(); thresholdImage.release(); differenceOfImages.release(); } catch (Exception e) { System.out.println(e.getMessage()); } if (controlPoints.get(6).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(0), controlPoints.get(1), new Scalar(0, 0, 255), 2); wasAtLeftPoint = true; } else if (!controlPoints.get(6).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(0), controlPoints.get(1), new Scalar(0, 255, 0), 2); } if (controlPoints.get(8).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(2), controlPoints.get(3), new Scalar(0, 0, 255), 2); wasAtCenterPoint = true; } else if (!controlPoints.get(8).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(2), controlPoints.get(3), new Scalar(0, 255, 0), 2); } if (controlPoints.get(7).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(4), controlPoints.get(5), new Scalar(0, 0, 255), 2); wasAtRightPoint = true; } else if (!controlPoints.get(7).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(4), controlPoints.get(5), new Scalar(0, 255, 0), 2); } if (wasAtCenterPoint && wasAtLeftPoint && wasAtRightPoint) { detectedCarsCount++; wasDetected = true; wasAtCenterPoint = false; wasAtLeftPoint = false; wasAtRightPoint = false; } if (contourArea > 3000) { Imgproc.drawContours(frame, contours, position, new Scalar(255, 255, 255)); } }
From source file:hu.unideb.fksz.VideoProcessor.java
License:Open Source License
/** * Processes {@code firstFrame} and {@code secondFrame}. * @param firstFrame the first frame of a cycle. */// w w w . j a va2s . c om private void processFrame(Mat firstFrame) { double contourArea = 0; int position = 0; try { /** * Resizes the {@code firstFrame} to {@code frameSize}. * */ Imgproc.resize(firstFrame, firstFrame, frameSize); /** * Convert the frame in grayscale color space. */ Imgproc.cvtColor(firstFrame, firstGrayImage, Imgproc.COLOR_BGR2GRAY); /** * {@code video} reads the second frame. */ video.read(secondFrame); Imgproc.resize(secondFrame, secondFrame, frameSize); Imgproc.cvtColor(secondFrame, secondGrayImage, Imgproc.COLOR_BGR2GRAY); /** * Getting the absolute per-pixel difference of the two frames into {@code differenceOfImages}. */ Core.absdiff(firstGrayImage, secondGrayImage, differenceOfImages); Imgproc.threshold(differenceOfImages, thresholdImage, 25, 255, Imgproc.THRESH_BINARY); Imgproc.blur(thresholdImage, thresholdImage, new Size(12, 12)); Imgproc.threshold(thresholdImage, thresholdImage, 20, 255, Imgproc.THRESH_BINARY); ///// for (int i = 0; i < contours.size(); ++i) { contours.get(i).release(); } contours.clear(); /** * The horizontal line. */ Imgproc.line(firstFrame, controlPoints.get(6), controlPoints.get(7), new Scalar(255, 0, 0), Imgproc.LINE_4); Imgproc.findContours(thresholdImage, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); for (int i = 0; i < hullPoints.size(); ++i) { hullPoints.get(i).release(); } hullPoints.clear(); for (int i = 0; i < contours.size(); i++) { MatOfInt tmp = new MatOfInt(); Imgproc.convexHull(contours.get(i), tmp, false); hullPoints.add(tmp); } /** * Searches for the contour with the greatest area. */ if (contours.size() > 0) { for (int i = 0; i < contours.size(); i++) { if (Imgproc.contourArea(contours.get(i)) > contourArea) { contourArea = Imgproc.contourArea(contours.get(i)); position = i; boundingRectangle = Imgproc.boundingRect(contours.get(i)); } } } secondFrame.release(); hierarchy.release(); secondGrayImage.release(); firstGrayImage.release(); thresholdImage.release(); differenceOfImages.release(); } catch (Exception e) { logger.error(e.getMessage()); } /** * Checking whether the control point on the left is * inside of {@code boundingRectangle}, which is a {@code Rect}, * bounding the greatest contour. */ if (controlPoints.get(6).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(0), controlPoints.get(1), new Scalar(0, 0, 255), 2); wasAtLeftPoint = true; } else if (!controlPoints.get(6).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(0), controlPoints.get(1), new Scalar(0, 255, 0), 2); } /** * Checking whether the control point on the middle is * inside of {@code boundingRectangle}, which is a {@code Rect}, * bounding the greatest contour. */ if (controlPoints.get(8).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(2), controlPoints.get(3), new Scalar(0, 0, 255), 2); wasAtCenterPoint = true; } else if (!controlPoints.get(8).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(2), controlPoints.get(3), new Scalar(0, 255, 0), 2); } /** * Checking whether the control point on the right is * inside of {@code boundingRectangle}, which is a {@code Rect}, * bounding the greatest contour. */ if (controlPoints.get(7).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(4), controlPoints.get(5), new Scalar(0, 0, 255), 2); wasAtRightPoint = true; } else if (!controlPoints.get(7).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(4), controlPoints.get(5), new Scalar(0, 255, 0), 2); } /** * If the three control points have were inside the {@code boundingRectangle}, * it means that a "car" has passed. */ if (wasAtCenterPoint && wasAtLeftPoint && wasAtRightPoint) { detectedCarsCount++; wasAtCenterPoint = false; wasAtLeftPoint = false; wasAtRightPoint = false; logger.info("Detected " + detectedCarsCount + " car(s)"); } /** * If the contour is big enough, draw it. */ if (contourArea > 3000) { Imgproc.drawContours(frame, contours, position, new Scalar(255, 255, 255)); } }
From source file:opencv_ext.TGG_OpenCV_Util.java
public static Point[][] getExternalConvexHullPoints(BufferedImage bufferedImage) { // Initialize Mat hierarchy = new Mat(); Mat image = TGG_OpenCV_Util.bufferedImage2Mat(bufferedImage); Point[][] pointResults;//from w w w .j av a2 s . com // Get Contours MatOfInt points = new MatOfInt(); ArrayList<MatOfPoint> contours = new ArrayList<>(); Imgproc.findContours(image, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); // Initialize rows of point results pointResults = new Point[contours.size()][0]; // Iterate over contours for (int contour = 0; contour < contours.size(); contour++) { // Get convex hull from contour Imgproc.convexHull(contours.get(contour), points, true); // Convert convex hull to mat of points MatOfPoint mopOut = hull2Points(points, contours.get(contour)); // Convert mat of points to list of points ArrayList<Point> pointList = new ArrayList<>(); Converters.Mat_to_vector_Point2d(mopOut, pointList); // Set column size for contour pointResults[contour] = new Point[pointList.size()]; // Fill result array with points for (int p = 0; p < pointList.size(); p++) { pointResults[contour][p] = pointList.get(p); } } // Return result return pointResults; }