summaryrefslogtreecommitdiff
path: root/WPIJavaCV/src/edu/wpi/first/wpijavacv/WPIContour.java
blob: 5cd9fc45d405eb3028d92660db4f74fe503a9876 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.wpi.first.wpijavacv;

import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
/**
 * This is a class that represents contours, it can only be obtained from
 * the findContours() method of WpiBinaryImage
 * @author Greg Granito
 */
public class WPIContour extends WPIDisposable {

    CvSeq contours;
    CvRect rect;

    WPIContour(CvSeq contours) {
        this.contours = contours;
//        memStorage = contours.storage();

    }

    CvSeq getCVSeq(){
        return contours;
    }

    /**
     *
     * @return the height of the bounding rectangle of the contour
     */
    public int getHeight(){
        if(rect == null || rect.isNull())
            rect = cvBoundingRect(contours, 1);

        return rect.height();
    }

    /**
     *
     * @return the width of the bounding rectangle of the contour
     */
    public int getWidth(){
        if(rect == null || rect.isNull())
            rect = cvBoundingRect(contours, 1);

        return rect.width();
    }

    /**
     *
     * @return the x coord of the top left corner of the bounding rectangle
     */
    public int getX(){
        if(rect == null || rect.isNull())
            rect = cvBoundingRect(contours, 1);

        return rect.x();
    }

    /**
     *
     * @return the y coord of the top left corner of the bounding rectangle
     */
    public int getY(){
        if(rect == null || rect.isNull())
            rect = cvBoundingRect(contours, 1);

        return rect.y();
    }

    /**
     *
     * @param percentAccuracy the percentage the perimeter of the polygon can be off
     * the perimeter of the contour. The higher the value, the fewer points the polygon
     * will have. A value of 4-5 is recommended.
     * @return the approximated WpiPolygon
     */
    public synchronized WPIPolygon approxPolygon(double percentAccuracy){
        WPIPolygon polygon = new WPIPolygon(cvApproxPoly(contours, contours.header_size(), contours.storage(), CV_POLY_APPROX_DP, percentAccuracy, 0));
        if (getPool() != null) {
            getPool().addToPool(polygon);
        }
        return polygon;
    }

    /**
     *
     * @return the perimeter of the contour
     */
    public int getlength(){
        return (int) cvContourPerimeter(contours);
    }

    public void disposed() {
        contours.deallocate();
    }
}