Ask Your Question
1

If "contours" is vector<vector<Point> what are its elements?

asked 2018-02-03 17:21:40 -0500

Marko5280 gravatar image

updated 2018-02-05 12:58:55 -0500

I am working with "findcontours". But, cannot get the type correct. Can someone please help? This code hits a error at:

 for (cont : contours);

The test code is as follows:

// ConsoleApplication 1-25-2018.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


/**
* file Smoothing.cpp
* brief Sample code for simple filters
* author OpenCV team
*/

#include <iostream>
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"

using namespace std;
using namespace cv;
Mat frame;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
vector<Point>  cont;

/**
* function main
*/
int main(int argc, char ** argv)
{
    Mat frame;
    frame = imread("lena.jpg");
    imshow("Lena.jpg", frame);
    waitKey();
    dilate(frame, frame, 1);
    imshow("dilate", frame);
    waitKey();
    findContours(frame, contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);

    for (cont : contours);
    {
        Moments mu = moments(cont, false);
        if (mu.get_m00() > 100.0) {}
    }
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
5

answered 2018-02-03 17:31:00 -0500

findContours returns a vector<vector<Point>>. When using an enhanced for-loop (like the one you have in your code) to iterate over a container, it goes inside and extracts the element and stores it in your variable cont.

For your case though, when it tries to iterate over contours, it finds out that there is another vector inside it hence the failure. To fix this, you need to state what kind of element is inside the container.

for(vector<Point>cont: contours)
{
    Moments mu = moments(cont, false);
}

Thumb Rule: When dealing with container of container; like contours, and using an enhanced for-loop to access its elements, always state the type of container it will be extracting.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-02-03 17:21:40 -0500

Seen: 829 times

Last updated: Feb 03 '18