Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Opencv_contourArea

HI,

When I calculate the area of via the contourArea function in binary image, I'm not getting the correct contour area. Below the snippet code, why the area is calculated incorrectly ? I have used a contour mask but it doesn't solve the problem.

vector<vec4i> hierarchy; vector<vector<point> > contours;

//findContours(bw, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
findContours(bw, contours, hierarchy, CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
Mat mask = Mat::zeros(bw.size(), CV_8UC1);

for (size_t i = 0; i < contours.size(); ++i)
{

drawContours(mask, contours, i, Scalar(255, 255, 255),
                 CV_FILLED, 8, hierarchy, 0, Point());

vector<Point> all_pixels;   // output, locations of non-zero pixels
cv::findNonZero(mask, all_pixels);

vector<Point> all_pixels;
cv::findNonZero(mask, all_pixels);
int inside_phase = all_pixels.size() - contours.size();
    // Calculate the area of each contour
    double area = contourArea(contours[i], true);

    cout << " Area#" << i <<": " << area << endl;
    cout << " inside: " << inside_phase << endl;
}

Opencv_contourArea

HI,

When I calculate the area of via the contourArea function in binary image, I'm not getting the correct contour area. Below the snippet code, why the area is calculated incorrectly ? I have used a contour mask but it doesn't solve the problem.

vector<vec4i> hierarchy; vector<vector<point> > contours;

contours;
    //findContours(bw, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
 findContours(bw, contours, hierarchy, CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
 Mat mask = Mat::zeros(bw.size(), CV_8UC1);

CV_8UC1);

for (size_t i = 0; i < contours.size(); ++i)
{

drawContours(mask, contours, i, Scalar(255, 255, 255),
                 CV_FILLED, 8, hierarchy, 0, Point());

vector<Point> all_pixels;   // output, locations of non-zero pixels
cv::findNonZero(mask, all_pixels);

vector<Point> all_pixels;
cv::findNonZero(mask, all_pixels);
int inside_phase = all_pixels.size() - contours.size();
    // Calculate the area of each contour
    double area = contourArea(contours[i], true);

    cout << " Area#" << i <<": " << area << endl;
    cout << " inside: " << inside_phase << endl;
}

Opencv_contourArea

HI,

When I calculate the area of via the contourArea function in binary image, I'm not getting the correct contour area. Below the snippet code, why the area is calculated incorrectly ? I have used a contour mask but it doesn't solve the problem.

vector<vec4i>

vector<Vec4i> hierarchy;
 vector<vector<point> vector<vector<Point> > contours;
  //findContours(bw, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
 findContours(bw, contours, hierarchy, CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
 Mat mask = Mat::zeros(bw.size(), CV_8UC1);

CV_8UC1);
for (size_t i = 0; i < contours.size(); ++i)
{
drawContours(mask, contours, i, Scalar(255, 255, 255),
CV_FILLED, 8, hierarchy, 0, Point());
vector<Point> all_pixels; // output, locations of non-zero pixels
cv::findNonZero(mask, all_pixels);
vector<Point> all_pixels;
cv::findNonZero(mask, all_pixels);
int inside_phase = all_pixels.size() - contours.size();
// Calculate the area of each contour
double area = contourArea(contours[i], true);
cout << " Area#" << i <<": " << area << endl;
cout << " inside: " << inside_phase << endl;
}