Ask Your Question

contourArea is undefined error in Visual Studio

asked 2015-03-21 04:36:02 -0500

Dp gravatar image

I am getting an error:-"contourArea is undefined" when i am using contourArea() to calculate the area of a particular object in my input image.I am using OpenCv with Visual Studio.How to remove this error?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-03-21 06:05:30 -0500

matman gravatar image

updated 2015-03-21 07:22:24 -0500

Ok I switch on my crystal ball and it says to me that either you try to make contourArea() on your whole image or you do the contourArea() function on the whole vector of your found contours. So use contourArea() like this:

//This should include most of the OpenCV functions
#include <opencv2/opencv.hpp> // When using OpenCV 3
//#include <opencv2/imgproc/imgproc.hpp> // When using OpenCV 2

using namespace cv;

Mat img = imread("My/image/path.bmp", IMREAD_ANYCOLOR);
Mat imgThresh;
threshold(img, imgThresh, nThreshold, 255, THRESH_BINARY);

vector<Mat> myContours; // This is the same as std::vector<std::vector<Point>> myContours
findContours(imgThresh, myContours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0,0));

for(int i=0; i<myContours.rows; i++) { // or myContours.size() if std::vector<std::vector<Point>> is used
    double area = contourArea(myContours[i]); // Perhabs it is contourArea(Mat(myContours[i])) when using std::vector<std::vector<Points>>

The negative vote from me is for switching on the crystal ball. Next time write some example code in your question.

Edit: Added #include and using namespace to code snippet

edit flag offensive delete link more



at least add some more hints, like including opencv2/imgproc/imgproc.hpp and either using namespace cv; or cv::contourArea()

berak gravatar imageberak ( 2015-03-21 06:07:58 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-03-21 04:36:02 -0500

Seen: 249 times

Last updated: Mar 21 '15