Java Code Examples for org.opencv.core.MatOfPoint.total()

The following are Jave code examples for showing how to use total() of the org.opencv.core.MatOfPoint class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: kronometer   File: SketchRecognition.java   View Source Code Vote up 5 votes
public void findRectangle() {
        Imgproc.cvtColor(originalImage, image, Imgproc.COLOR_BGR2GRAY);
        setFilter();
        this.rects.clear();

        //Find Contours
        Imgproc.findContours(image, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));

        //For conversion later on
        MatOfPoint2f approxCurve = new MatOfPoint2f();

        //For each contour found
        for (int i = 0; i < contours.size(); i++) {

            //Convert contours from MatOfPoint to MatOfPoint2f
            MatOfPoint2f contour2f = new MatOfPoint2f(contours.get(i).toArray());
            //Processing on mMOP2f1 which is in type MatOfPoint2f
            double approxDistance = Imgproc.arcLength(contour2f, true) * 0.02;

            if (approxDistance > 1) {
                //Find Polygons
                Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);

                //Convert back to MatOfPoint
                MatOfPoint points = new MatOfPoint(approxCurve.toArray());

                //Rectangle Checks - Points, area, convexity
                if (points.total() == 4 && Math.abs(Imgproc.contourArea(points)) > 1000 && Imgproc.isContourConvex(points)) {
                    double cos = 0;
                    double mcos = 0;
                    for (int sc = 2; sc < 5; sc++) {
                        // TO-DO Figure a way to check angle
                        if (cos > mcos) {
                            mcos = cos;
                        }
                    }
                    if (mcos < 0.3) {
                        // Get bounding rect of contour
                        Rect rect = Imgproc.boundingRect(points);

//                        if (Math.abs(rect.height - rect.width) < 1000) {
                            System.out.println(i + "| x: " + rect.x + " + width("+rect.width+"), y: " + rect.y + "+ width("+rect.height+")");
                            rects.add(rect);
                            Core.rectangle(originalImage, rect.tl(), rect.br(), new Scalar(20, 20, 20), -1, 4, 0);
                            Imgproc.drawContours(originalImage, contours, i, new Scalar(0, 255, 0, .8), 2);
                            
                            // Highgui.imwrite("detected_layers"+i+".png", originalImage);
//                        }
                    }
                }
            }
        }
        // Pass raw parameters
        ImageDetection id = new ImageDetection();
        HyperTextBuilder.rects = this.rects;
        HyperTextBuilder.rect_height = this.HEIGHT;
        HyperTextBuilder.rect_width = this.WIDTH;
        id.setData(Utility.matToBufferedImage(originalImage));
    }
 
Example 2
Project: FlashLib   File: CvProcessing.java   View Source Code Vote up 5 votes
/**
 * Sorts the contours by their size and removes contours for the bottom of the list if the list contains
 * too many contours. 
 * 
 * @param contours list of contours
 * @param amount the maximum amount of contours that should remain after the operation
 * @see #filterByComparator(List, int, Comparator)
 */
public static void filterForLargestContours(List<MatOfPoint> contours, int amount){
	Comparator<MatOfPoint> sizeComparer = (MatOfPoint o1, MatOfPoint o2) ->{
		if(o1.total() < o2.total()) return 1;
		else if(o2.total() < o1.total()) return -1;
		return 0;
	};
	filterByComparator(contours, amount, sizeComparer);
}
 
Example 3
Project: FlashLib   File: CvProcessing.java   View Source Code Vote up 5 votes
/**
 * Sorts the contours by their size and removes contours for the bottom of the list if the list contains
 * too many contours. 
 * 
 * @param contours list of contours
 * @param amount the maximum amount of contours that should remain after the operation
 * @see #filterByComparator(List, int, Comparator)
 */
public static void filterForSmallestContours(List<MatOfPoint> contours, int amount){
	Comparator<MatOfPoint> sizeComparer = (MatOfPoint o1, MatOfPoint o2) ->{
		if(o1.total() > o2.total()) return 1;
		else if(o2.total() > o1.total()) return -1;
		return 0;
	};
	filterByComparator(contours, amount, sizeComparer);
}