C# Emgucv CvInvoke.DrawContours run very slow


This Content is from Stack Overflow. Question asked by Erixz7777

I am using DrawContours method using emgucv package, but it’s working very slow, it takes about “12” second

I do the same thing in python, it only takes maybe “2” second to do this.

  • The target is “Fill up all external contour areas that is bigger than 800 pixels on a new canvas”
    and than I will use this image as a mask

Please tell me how can I improve the time consuming problem

Here’s the C# code :

VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();             
CvInvoke.FindContours(img, contours, null, RetrType.External, ChainApproxMethod.ChainApproxSimple);
UMat mask = new UMat(img.Size, DepthType.Cv8U, 1);

int minArea = 800;
for (int i = 0; i < contours.Size; i++)
    int area = CvInvoke.ContourArea(contours[i]);
    if (area > minArea)
        CvInvoke.DrawContours(mask, contours, i, new MCvScalar(255), -1);

also show my python code :

contours,_ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

C = len(contours)

for i in range(0, C):
    if cv2.contourArea(contours[i]) > 800 :
        cv2.drawContours(mask, contours, i, (255,255,255), -1)

Thank you


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?