Filter tuple list of contour coordinates











up vote
0
down vote

favorite












I am a bit confused on how I can filter out the contour coordinates (which are in a tuple) and keep only the part of the contour that corresponds to the top segmented layer of this image.



My code is this



img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
blur = cv2.GaussianBlur(img, (1, 1), 1000)
flag, thresh = cv2.threshold(blur, 120, 255, cv2.THRESH_BINARY)
thresh = cv2.dilate(thresh, None)
thresh = cv2.dilate(thresh, None)
thresh = cv2.erode(thresh, None)
thresh = cv2.erode(thresh, None)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:1]
perimeters = [cv2.arcLength(contours[i], True) for i in range(len(contours))]
listindex = [i for i in range(1) if perimeters[i] > perimeters[0] / 2]
num_of_layers = len(listindex)
imgcont = img.copy()
[cv2.drawContours(imgcont, [contours[i]], 0, (0, 255, 0), 5) for i in listindex]
M = cv2.moments(contours[0])
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
print(cx, cy)
plt.imshow(imgcont, cmap='gray')


The image










share|improve this question


























    up vote
    0
    down vote

    favorite












    I am a bit confused on how I can filter out the contour coordinates (which are in a tuple) and keep only the part of the contour that corresponds to the top segmented layer of this image.



    My code is this



    img = cv2.imread(image_path)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    blur = cv2.GaussianBlur(img, (1, 1), 1000)
    flag, thresh = cv2.threshold(blur, 120, 255, cv2.THRESH_BINARY)
    thresh = cv2.dilate(thresh, None)
    thresh = cv2.dilate(thresh, None)
    thresh = cv2.erode(thresh, None)
    thresh = cv2.erode(thresh, None)
    im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    contours = sorted(contours, key=cv2.contourArea, reverse=True)[:1]
    perimeters = [cv2.arcLength(contours[i], True) for i in range(len(contours))]
    listindex = [i for i in range(1) if perimeters[i] > perimeters[0] / 2]
    num_of_layers = len(listindex)
    imgcont = img.copy()
    [cv2.drawContours(imgcont, [contours[i]], 0, (0, 255, 0), 5) for i in listindex]
    M = cv2.moments(contours[0])
    cx = int(M['m10'] / M['m00'])
    cy = int(M['m01'] / M['m00'])
    print(cx, cy)
    plt.imshow(imgcont, cmap='gray')


    The image










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am a bit confused on how I can filter out the contour coordinates (which are in a tuple) and keep only the part of the contour that corresponds to the top segmented layer of this image.



      My code is this



      img = cv2.imread(image_path)
      img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
      blur = cv2.GaussianBlur(img, (1, 1), 1000)
      flag, thresh = cv2.threshold(blur, 120, 255, cv2.THRESH_BINARY)
      thresh = cv2.dilate(thresh, None)
      thresh = cv2.dilate(thresh, None)
      thresh = cv2.erode(thresh, None)
      thresh = cv2.erode(thresh, None)
      im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
      contours = sorted(contours, key=cv2.contourArea, reverse=True)[:1]
      perimeters = [cv2.arcLength(contours[i], True) for i in range(len(contours))]
      listindex = [i for i in range(1) if perimeters[i] > perimeters[0] / 2]
      num_of_layers = len(listindex)
      imgcont = img.copy()
      [cv2.drawContours(imgcont, [contours[i]], 0, (0, 255, 0), 5) for i in listindex]
      M = cv2.moments(contours[0])
      cx = int(M['m10'] / M['m00'])
      cy = int(M['m01'] / M['m00'])
      print(cx, cy)
      plt.imshow(imgcont, cmap='gray')


      The image










      share|improve this question













      I am a bit confused on how I can filter out the contour coordinates (which are in a tuple) and keep only the part of the contour that corresponds to the top segmented layer of this image.



      My code is this



      img = cv2.imread(image_path)
      img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
      blur = cv2.GaussianBlur(img, (1, 1), 1000)
      flag, thresh = cv2.threshold(blur, 120, 255, cv2.THRESH_BINARY)
      thresh = cv2.dilate(thresh, None)
      thresh = cv2.dilate(thresh, None)
      thresh = cv2.erode(thresh, None)
      thresh = cv2.erode(thresh, None)
      im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
      contours = sorted(contours, key=cv2.contourArea, reverse=True)[:1]
      perimeters = [cv2.arcLength(contours[i], True) for i in range(len(contours))]
      listindex = [i for i in range(1) if perimeters[i] > perimeters[0] / 2]
      num_of_layers = len(listindex)
      imgcont = img.copy()
      [cv2.drawContours(imgcont, [contours[i]], 0, (0, 255, 0), 5) for i in listindex]
      M = cv2.moments(contours[0])
      cx = int(M['m10'] / M['m00'])
      cy = int(M['m01'] / M['m00'])
      print(cx, cy)
      plt.imshow(imgcont, cmap='gray')


      The image







      python contour






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 20:42









      laza

      500722




      500722





























          active

          oldest

          votes











          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',
          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%2f53243224%2ffilter-tuple-list-of-contour-coordinates%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243224%2ffilter-tuple-list-of-contour-coordinates%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

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma