当前位置: 首页>>代码示例>>Java>>正文


Java Imgproc.approxPolyDP方法代码示例

本文整理汇总了Java中org.opencv.imgproc.Imgproc.approxPolyDP方法的典型用法代码示例。如果您正苦于以下问题:Java Imgproc.approxPolyDP方法的具体用法?Java Imgproc.approxPolyDP怎么用?Java Imgproc.approxPolyDP使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.opencv.imgproc.Imgproc的用法示例。


在下文中一共展示了Imgproc.approxPolyDP方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: gridDetection

import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
public static void gridDetection(Mat img){
       List<MatOfPoint> contours = new ArrayList<>();
       Imgproc.findContours(img,contours,new Mat(),Imgproc.RETR_TREE,Imgproc.CHAIN_APPROX_SIMPLE);

       double maxArea = 0;
       MatOfPoint max_contour = new MatOfPoint();

       Iterator<MatOfPoint> iterator = contours.iterator();
       while (iterator.hasNext()){
           MatOfPoint contour = iterator.next();
           double area = Imgproc.contourArea(contour);
           if(area > maxArea){
               maxArea = area;
               max_contour = contour;
           }
       }

       double epsilon = 0.1*Imgproc.arcLength(new MatOfPoint2f(max_contour.toArray()),true);
       MatOfPoint2f approx = new MatOfPoint2f();
       Imgproc.approxPolyDP(new MatOfPoint2f(max_contour.toArray()),approx,epsilon,true);
	
	RotatedRect rect = Imgproc.minAreaRect(new MatOfPoint2f(max_contour.toArray()));
	
	Mat grid = Thresholding.orderPoints(approx);
	
	Thresholding.approx = approx;
	Thresholding.grid = grid;
	Thresholding.rect = rect;
}
 
开发者ID:Sanahm,项目名称:SudoCAM-Ku,代码行数:30,代码来源:Thresholding.java

示例2: detectContoursByShape

import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
/**
 * Filters contours by shape. Iterates through the list of contours and approximates their shape. 
 * Compares the vertices of the shape to the desired vertices and removes the contour if they do not match.
 * 
 * @param contours list of contours
 * @param vertices vertices of the desired shape
 * @param accuracy the accuracy of approximation
 * @see Imgproc#approxPolyDP(MatOfPoint2f, MatOfPoint2f, double, boolean)
 */
public static void detectContoursByShape(List<MatOfPoint> contours, int vertices, double accuracy){
	MatOfPoint2f matOfPoint2f = new MatOfPoint2f();
	MatOfPoint2f approxCurve = new MatOfPoint2f();
	
	for(int idx = contours.size() - 1; idx >= 0; idx--){
		MatOfPoint contour = contours.get(idx);
	   
	    matOfPoint2f.fromList(contour.toList());
	    Imgproc.approxPolyDP(matOfPoint2f, approxCurve, Imgproc.arcLength(matOfPoint2f, true) * accuracy, true);
	    long total = approxCurve.total();
	    
	    if (total != vertices)
	    	contours.remove(idx);
	}
}
 
开发者ID:Flash3388,项目名称:FlashLib,代码行数:25,代码来源:CvProcessing.java

示例3: findRectangle

import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
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));
    }
 
开发者ID:lupino22,项目名称:kronometer,代码行数:60,代码来源:SketchRecognition.java

示例4: getFingers

import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
public static List<Point> getFingers(Mat im, Mat hand, List<MatOfPoint> contourOutput) {
    /**
     * @param: im: A YCrCb image with same shape as `hand`
     * @param: hand: A binary image indicating which pixel is part of hand
     * @param: contourOutput:
     *      If is not null, contours will be saved for debug
     * @return: A list of points indicating the detected finger tip points
     *      This function will not change `in` or `hand`
     */

    // assert im.size().height == Config.IM_HEIGHT;
    // assert im.size().height == hand.size().height

    List<MatOfPoint> contours = Util.largeContours(hand, Config.HAND_AREA_MIN);
    Imgproc.dilate(hand, hand, Mat.ones(new Size(5, 5), CvType.CV_8UC1));

    if (contours.isEmpty()) {
        return new ArrayList<>();
    }

    ArrayList<Point> fingerTips = new ArrayList<>();

    for (int i = 0; i < contours.size(); ++i) {
        // apply polygon approximation
        MatOfPoint cnt = contours.get(i);

        double epsilon = 5;
        MatOfPoint2f approx = new MatOfPoint2f(), cntCvt = new MatOfPoint2f();

        cnt.convertTo(cntCvt, CvType.CV_32FC2);
        Imgproc.approxPolyDP(cntCvt, approx, epsilon, true);
        approx.convertTo(cnt, CvType.CV_32S);

        // apply polygon approximation
        fingerTips.addAll(findFingerTips(approx.toList(), hand));
    }

    if (contourOutput != null) {
        contourOutput.clear();
        contourOutput.addAll(contours);
    }
    return fingerTips;
}
 
开发者ID:hgs1217,项目名称:Paper-Melody,代码行数:44,代码来源:FingerDetector.java


注:本文中的org.opencv.imgproc.Imgproc.approxPolyDP方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。