Javacv: Access to the points of contour

Created on 18 Oct 2019  路  6Comments  路  Source: bytedeco/javacv

How is it possible to access all points of contour?

Mat image = imread(filename);
threshold(image, image, 64, 255, CV_THRESH_BINARY);
MatVector contours = new MatVector();
findContours(grayImage, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
long n = contours.size();
for (long i = 0; i < n; i++) {
    Mat contour = contours.get(i);
   // how to access the points of the contour?
}
question

All 6 comments

Those are the points of the contours. Maybe what you're looking for is an
indexer? http://bytedeco.org/news/2014/12/23/third-release/

Thanks for response. I think my questions was to general.

I use approxPolyDP to get the polygon of the contours. Afterwards I want to get the edges of polygon to use some patterns to filter them. Therefore, I need the points of each polygon to get the edges (pair of points). Then I can filter them for similar length, approx orthogonal etc.

I know how to do it with opencv in python but can't comprehend how to it with javacv.

JavaCPP maps the C++ API, so it looks closer to that API, but it's pretty much the same thing anyway. There is nothing weird about it. If you could provide an example of what you're having problems with, it would help to help you.

I want to port this python code to java:

    // removed loading image and doing thresholding etc.
    _, contours, _ = cv2.findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    polygons = [cv2.approxPolyDP(rc, 6.0, True) for rc in contours]
    edge_sets = map(self._polygons_to_edges, polygons)

    // filter edge_sets afterwards

    @staticmethod
    def _polygons_to_edges(vertex_list):
        """Return a list of edges based on the given list of vertices. """
        return list(self._pairs_circular(vertex_list))

    @staticmethod
    def _pairs_circular(iterable):
        """ Generate pairs from an iterable. Best illustrated by example:
        >>> list(pairs_circular('abcd'))
        [('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'a')]
        """
        iterator = iter(iterable)
        x = next(iterator)
        zeroth = x
        while True:
            try:
                y = next(iterator)
                yield((x, y))
            except StopIteration:  # Iterator is exhausted so wrap around to start.
                try:
                    yield((y, zeroth))
                except UnboundLocalError:  # Iterable has one element. No pairs.
                    pass
                break
            x = y

My kotlin code so far:

    // removed loading image and doing thresholding etc.
    val contours = MatVector()
    val hierarchy = Mat()
    findContours(image, contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE)
    val n = contours.size()

     val polygons = ArrayList<Mat>()
     for (i in 0 until n) {
           val contour = contours.get(i)
           val approxCurve = Mat()
           approxPolyDP(contour, approxCurve, 6.0, true)
           polygons.add(approxCurve)
      }

      // build edge set
      val edgeSet = ArrayList<Set<Int>>()
      polygons.forEach {
          // here I'm stuck getting the edges of the polygon
       }

It's basically the same thing, something like this with an indexer:

IntIndexer idx = approxCurve.createIndexer();
for (long i = 0; i < idx.rows(); i++) {
    int x = idx.get(i, 0);
    int y = idx.get(i, 1);
    // do whatever
}

Works perfect thanks alot for you help! Now all makes sense to me :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ahmedaomda picture ahmedaomda  路  4Comments

Bahramudin picture Bahramudin  路  3Comments

UpAndDownAgain picture UpAndDownAgain  路  3Comments

kongqw picture kongqw  路  4Comments

fif10 picture fif10  路  3Comments