Ask Your Question

Revision history [back]

The code is doing exactly what you asked it to, drawContours is drawing the green blob over the countour's area.

What you probably want to do instead of drawing the filled contour into srcMat, is to draw it on a mask matrix, then apply the mask matrix to srcMat to 'zero' all the cells outside the contour area.

  1. Create maskMat, the same dimensions as srcMat of CV8U.
  2. Fill maskMat with zeros.
  3. Use drawContours to draw a value of 1 into maskMat in the filled shape of the contour.
  4. do a pixel by pixel logical AND of srcMat with maskMat (in C++ it's srcMat = srcMat & maskMat).

This will zero the srcMat pixels which are outside the contour.