Ask Your Question

Revision history [back]

findContours function in C++ not giving same number as Python

I am trying to get contours for an image using OpenCV4 and C++. I have defined the contours as vector of vector of points. When I use the function it is only giving me one set of contours.

vector<vector<cv::Point> > contours;
vector<cv::Vec4i> hierarchy;

findContours(border_image, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

cout<<"Size of contours -->"<< contours.size()<<endl;

border_image --> cv::Mat and has each pixel as CV_8UC1

Above code is returning size(length of vector of vector of points) as 1.

If I do the same in Python, I am getting the required number of contours. What am I doing wrong?

Python Version:

contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

Above returns contours of length 112 thresh above is same image as border_image above in C++.

Expected contours(Vector of Vector of Points) of length 112. I only got contours of length 1 which is nothing but first element(Vector of Points) in result obtained by Python.

What am I doing wrong? Someone help me out.