cv2.findContours() acts strangely?












1















I'm finding contours in an image. With every contour I found, I print out its bouding rect and area and then draw it to the image. Funnily, I found that 5 contours that have been drawed while there were only 4 contours printed. Anyone knows what happened here?



>>contour 1
>>(0, 0, 314, 326)
>>101538.5
>>contour 2
>>(75, 117, 60, 4)
>>172.0
>>contour 3
>>(216, 106, 3, 64)
>>124.0
>>contour 4
>>(62, 18, 138, 9)
>>383.5


enter image description hereenter image description here



import cv2 
import numpy as np

img = cv2.imread('1.png')

imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)

_, contours, hier = cv2.findContours(thresh, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)

for i,c in enumerate(contours):
rect = cv2.boundingRect(c)
area = cv2.contourArea(c)
print("contour " + str(i+1))
print(rect)
print(area)

cv2.drawContours(img, contours, -1, (0,255,0), 1)

cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()









share|improve this question



























    1















    I'm finding contours in an image. With every contour I found, I print out its bouding rect and area and then draw it to the image. Funnily, I found that 5 contours that have been drawed while there were only 4 contours printed. Anyone knows what happened here?



    >>contour 1
    >>(0, 0, 314, 326)
    >>101538.5
    >>contour 2
    >>(75, 117, 60, 4)
    >>172.0
    >>contour 3
    >>(216, 106, 3, 64)
    >>124.0
    >>contour 4
    >>(62, 18, 138, 9)
    >>383.5


    enter image description hereenter image description here



    import cv2 
    import numpy as np

    img = cv2.imread('1.png')

    imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    ret,thresh = cv2.threshold(imgray,127,255,0)

    _, contours, hier = cv2.findContours(thresh, cv2.RETR_TREE,
    cv2.CHAIN_APPROX_SIMPLE)

    for i,c in enumerate(contours):
    rect = cv2.boundingRect(c)
    area = cv2.contourArea(c)
    print("contour " + str(i+1))
    print(rect)
    print(area)

    cv2.drawContours(img, contours, -1, (0,255,0), 1)

    cv2.imshow('img', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()









    share|improve this question

























      1












      1








      1








      I'm finding contours in an image. With every contour I found, I print out its bouding rect and area and then draw it to the image. Funnily, I found that 5 contours that have been drawed while there were only 4 contours printed. Anyone knows what happened here?



      >>contour 1
      >>(0, 0, 314, 326)
      >>101538.5
      >>contour 2
      >>(75, 117, 60, 4)
      >>172.0
      >>contour 3
      >>(216, 106, 3, 64)
      >>124.0
      >>contour 4
      >>(62, 18, 138, 9)
      >>383.5


      enter image description hereenter image description here



      import cv2 
      import numpy as np

      img = cv2.imread('1.png')

      imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
      ret,thresh = cv2.threshold(imgray,127,255,0)

      _, contours, hier = cv2.findContours(thresh, cv2.RETR_TREE,
      cv2.CHAIN_APPROX_SIMPLE)

      for i,c in enumerate(contours):
      rect = cv2.boundingRect(c)
      area = cv2.contourArea(c)
      print("contour " + str(i+1))
      print(rect)
      print(area)

      cv2.drawContours(img, contours, -1, (0,255,0), 1)

      cv2.imshow('img', img)
      cv2.waitKey(0)
      cv2.destroyAllWindows()









      share|improve this question














      I'm finding contours in an image. With every contour I found, I print out its bouding rect and area and then draw it to the image. Funnily, I found that 5 contours that have been drawed while there were only 4 contours printed. Anyone knows what happened here?



      >>contour 1
      >>(0, 0, 314, 326)
      >>101538.5
      >>contour 2
      >>(75, 117, 60, 4)
      >>172.0
      >>contour 3
      >>(216, 106, 3, 64)
      >>124.0
      >>contour 4
      >>(62, 18, 138, 9)
      >>383.5


      enter image description hereenter image description here



      import cv2 
      import numpy as np

      img = cv2.imread('1.png')

      imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
      ret,thresh = cv2.threshold(imgray,127,255,0)

      _, contours, hier = cv2.findContours(thresh, cv2.RETR_TREE,
      cv2.CHAIN_APPROX_SIMPLE)

      for i,c in enumerate(contours):
      rect = cv2.boundingRect(c)
      area = cv2.contourArea(c)
      print("contour " + str(i+1))
      print(rect)
      print(area)

      cv2.drawContours(img, contours, -1, (0,255,0), 1)

      cv2.imshow('img', img)
      cv2.waitKey(0)
      cv2.destroyAllWindows()






      python opencv contour






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 8:31









      Ha BomHa Bom

      1,1182518




      1,1182518
























          1 Answer
          1






          active

          oldest

          votes


















          1














          cv2.RETR_TREE is the reason you are getting this. It retrieves all the contours and creates a full family hierarchy list. In contour detection you are expected to use white objects in black background. Otherwise because of hierarchy list you would get results as you are getting now. For more details check documentation.



          So make sure you find contours of white objects in black background. Add cv2.bitwise_not() function to convert the image.



          .
          .
          .



          imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
          cv2.bitwise_not(imgray,imgray)


          .
          .
          .



          OUTPUT:





          4
          contour 1
          (76, 118, 58, 2)
          56.0
          contour 2
          (217, 107, 1, 62)
          0.0
          contour 3
          (63, 19, 136, 7)
          110.5
          contour 4
          (248, 1, 66, 45)
          55.5





          share|improve this answer
























          • Thanks for your answer. But I still wonder why it draws contour of the line but don't print it out.

            – Ha Bom
            Nov 14 '18 at 10:05











          • In your case also there are only 4 contours. Top right contour is attached to the boundary of the image. So It is considered as a single contour along with the boundary.

            – Ishara Madhawa
            Nov 14 '18 at 10:30













          • oh I see. thank you very much

            – Ha Bom
            Nov 15 '18 at 1:16











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295892%2fcv2-findcontours-acts-strangely%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          cv2.RETR_TREE is the reason you are getting this. It retrieves all the contours and creates a full family hierarchy list. In contour detection you are expected to use white objects in black background. Otherwise because of hierarchy list you would get results as you are getting now. For more details check documentation.



          So make sure you find contours of white objects in black background. Add cv2.bitwise_not() function to convert the image.



          .
          .
          .



          imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
          cv2.bitwise_not(imgray,imgray)


          .
          .
          .



          OUTPUT:





          4
          contour 1
          (76, 118, 58, 2)
          56.0
          contour 2
          (217, 107, 1, 62)
          0.0
          contour 3
          (63, 19, 136, 7)
          110.5
          contour 4
          (248, 1, 66, 45)
          55.5





          share|improve this answer
























          • Thanks for your answer. But I still wonder why it draws contour of the line but don't print it out.

            – Ha Bom
            Nov 14 '18 at 10:05











          • In your case also there are only 4 contours. Top right contour is attached to the boundary of the image. So It is considered as a single contour along with the boundary.

            – Ishara Madhawa
            Nov 14 '18 at 10:30













          • oh I see. thank you very much

            – Ha Bom
            Nov 15 '18 at 1:16
















          1














          cv2.RETR_TREE is the reason you are getting this. It retrieves all the contours and creates a full family hierarchy list. In contour detection you are expected to use white objects in black background. Otherwise because of hierarchy list you would get results as you are getting now. For more details check documentation.



          So make sure you find contours of white objects in black background. Add cv2.bitwise_not() function to convert the image.



          .
          .
          .



          imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
          cv2.bitwise_not(imgray,imgray)


          .
          .
          .



          OUTPUT:





          4
          contour 1
          (76, 118, 58, 2)
          56.0
          contour 2
          (217, 107, 1, 62)
          0.0
          contour 3
          (63, 19, 136, 7)
          110.5
          contour 4
          (248, 1, 66, 45)
          55.5





          share|improve this answer
























          • Thanks for your answer. But I still wonder why it draws contour of the line but don't print it out.

            – Ha Bom
            Nov 14 '18 at 10:05











          • In your case also there are only 4 contours. Top right contour is attached to the boundary of the image. So It is considered as a single contour along with the boundary.

            – Ishara Madhawa
            Nov 14 '18 at 10:30













          • oh I see. thank you very much

            – Ha Bom
            Nov 15 '18 at 1:16














          1












          1








          1







          cv2.RETR_TREE is the reason you are getting this. It retrieves all the contours and creates a full family hierarchy list. In contour detection you are expected to use white objects in black background. Otherwise because of hierarchy list you would get results as you are getting now. For more details check documentation.



          So make sure you find contours of white objects in black background. Add cv2.bitwise_not() function to convert the image.



          .
          .
          .



          imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
          cv2.bitwise_not(imgray,imgray)


          .
          .
          .



          OUTPUT:





          4
          contour 1
          (76, 118, 58, 2)
          56.0
          contour 2
          (217, 107, 1, 62)
          0.0
          contour 3
          (63, 19, 136, 7)
          110.5
          contour 4
          (248, 1, 66, 45)
          55.5





          share|improve this answer













          cv2.RETR_TREE is the reason you are getting this. It retrieves all the contours and creates a full family hierarchy list. In contour detection you are expected to use white objects in black background. Otherwise because of hierarchy list you would get results as you are getting now. For more details check documentation.



          So make sure you find contours of white objects in black background. Add cv2.bitwise_not() function to convert the image.



          .
          .
          .



          imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
          cv2.bitwise_not(imgray,imgray)


          .
          .
          .



          OUTPUT:





          4
          contour 1
          (76, 118, 58, 2)
          56.0
          contour 2
          (217, 107, 1, 62)
          0.0
          contour 3
          (63, 19, 136, 7)
          110.5
          contour 4
          (248, 1, 66, 45)
          55.5






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 9:24









          Ishara MadhawaIshara Madhawa

          2,12641028




          2,12641028













          • Thanks for your answer. But I still wonder why it draws contour of the line but don't print it out.

            – Ha Bom
            Nov 14 '18 at 10:05











          • In your case also there are only 4 contours. Top right contour is attached to the boundary of the image. So It is considered as a single contour along with the boundary.

            – Ishara Madhawa
            Nov 14 '18 at 10:30













          • oh I see. thank you very much

            – Ha Bom
            Nov 15 '18 at 1:16



















          • Thanks for your answer. But I still wonder why it draws contour of the line but don't print it out.

            – Ha Bom
            Nov 14 '18 at 10:05











          • In your case also there are only 4 contours. Top right contour is attached to the boundary of the image. So It is considered as a single contour along with the boundary.

            – Ishara Madhawa
            Nov 14 '18 at 10:30













          • oh I see. thank you very much

            – Ha Bom
            Nov 15 '18 at 1:16

















          Thanks for your answer. But I still wonder why it draws contour of the line but don't print it out.

          – Ha Bom
          Nov 14 '18 at 10:05





          Thanks for your answer. But I still wonder why it draws contour of the line but don't print it out.

          – Ha Bom
          Nov 14 '18 at 10:05













          In your case also there are only 4 contours. Top right contour is attached to the boundary of the image. So It is considered as a single contour along with the boundary.

          – Ishara Madhawa
          Nov 14 '18 at 10:30







          In your case also there are only 4 contours. Top right contour is attached to the boundary of the image. So It is considered as a single contour along with the boundary.

          – Ishara Madhawa
          Nov 14 '18 at 10:30















          oh I see. thank you very much

          – Ha Bom
          Nov 15 '18 at 1:16





          oh I see. thank you very much

          – Ha Bom
          Nov 15 '18 at 1:16


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295892%2fcv2-findcontours-acts-strangely%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine