Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

If contourArea doesn't give you expected result, you can follow this approach:

  1. Copy the contour in a new empty Mat
  2. Then countNonZero applied on the new submatrix will give you the number of non-black pixel

        //1. Copy the contour in a new empty Mat
        Rect cRect = boundingRect(*it);
        Mat subImg = dilatedImg(cRect);
        double cArea = countNonZero(subImg);
    

Note: if you doesn't have a binary image, you may need to threshold it before to count only the right pixels.

If contourArea doesn't give you expected result, you can follow this approach:

  1. Copy the contour in a new empty Mat
  2. Then countNonZero applied on the new submatrix will give you the number of non-black pixel

        //1. Copy the contour in a new empty Mat
        Rect cRect = boundingRect(*it);
        Mat subImg = dilatedImg(cRect);
        double cArea = countNonZero(subImg);
    

Note: if you doesn't have a binary image, you may need to threshold it before to count only the right pixels.

If contourArea doesn't give you expected result, you can follow this approach:

  1. Copy the contour in a new empty Mat
  2. Then Apply countNonZero applied on the new submatrix and you will give you get the number of non-black pixel

        //1. Copy the contour in a new empty Mat
        Rect cRect = boundingRect(*it);
    boundingRect(*it); //it is an iterator for your contours vector
        Mat subImg = dilatedImg(cRect);
        double cArea = countNonZero(subImg);
    

Note: if you doesn't have a binary image, you may need to threshold it before to count only the right pixels.