How to mark the center and calculate the diameter of an image using opencv - python?












1















I am working on the recognition of the center and the image rendering. I'm using cv2.findContours to delimit the edges of the image of interest. And using cv.minEnclosingCircle (cnt) to circumnavigate my region of interest. The code below I can identify the center of each ROI, but I am not able to mark in the output of the image the circle corresponding to the image that I want to calculate and also I want to mark with a pqno point the exact location where the algorithm identified the center.



import cv2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnchoredText


thresh = cv2.imread('IMD044.png',0)
_, contours,hierarchy = cv2.findContours(thresh,2,1)
print (len(contours))
cnt = contours

for i in range (len(cnt)):
(x,y),radius = cv2.minEnclosingCircle(cnt[i])
center = (int(x),int(y))
radius = int(radius)
cv2.circle(thresh,center,radius,(0,255,0),2)
print ('Circle: ' + str(i) + ' - Center: ' + str(center) + ' - Radius: ' + str(radius))
plt.text(x-15, y+10, '+', fontsize=25, color = 'red')
plt.text(10, -10, 'Centro: '+str(center), fontsize=11, color = 'red')
plt.text(340, -10, 'Diametro: '+str((radius*2)/100)+'mm', fontsize=11, color = 'red')
plt.Circle(x, y, color='red', fill=False)
plt.imshow(thresh, cmap='gray')
plt.show()


I used the Opencv documentation to demarcate the contours and get the regions, but the green circle mark does not appear.



Exit:



enter image description here



Exit expected:
enter image description here



updating the question I was able to add the information, it's only necessary to add the circle and check if the diameter is correct.










share|improve this question





























    1















    I am working on the recognition of the center and the image rendering. I'm using cv2.findContours to delimit the edges of the image of interest. And using cv.minEnclosingCircle (cnt) to circumnavigate my region of interest. The code below I can identify the center of each ROI, but I am not able to mark in the output of the image the circle corresponding to the image that I want to calculate and also I want to mark with a pqno point the exact location where the algorithm identified the center.



    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.offsetbox import AnchoredText


    thresh = cv2.imread('IMD044.png',0)
    _, contours,hierarchy = cv2.findContours(thresh,2,1)
    print (len(contours))
    cnt = contours

    for i in range (len(cnt)):
    (x,y),radius = cv2.minEnclosingCircle(cnt[i])
    center = (int(x),int(y))
    radius = int(radius)
    cv2.circle(thresh,center,radius,(0,255,0),2)
    print ('Circle: ' + str(i) + ' - Center: ' + str(center) + ' - Radius: ' + str(radius))
    plt.text(x-15, y+10, '+', fontsize=25, color = 'red')
    plt.text(10, -10, 'Centro: '+str(center), fontsize=11, color = 'red')
    plt.text(340, -10, 'Diametro: '+str((radius*2)/100)+'mm', fontsize=11, color = 'red')
    plt.Circle(x, y, color='red', fill=False)
    plt.imshow(thresh, cmap='gray')
    plt.show()


    I used the Opencv documentation to demarcate the contours and get the regions, but the green circle mark does not appear.



    Exit:



    enter image description here



    Exit expected:
    enter image description here



    updating the question I was able to add the information, it's only necessary to add the circle and check if the diameter is correct.










    share|improve this question



























      1












      1








      1








      I am working on the recognition of the center and the image rendering. I'm using cv2.findContours to delimit the edges of the image of interest. And using cv.minEnclosingCircle (cnt) to circumnavigate my region of interest. The code below I can identify the center of each ROI, but I am not able to mark in the output of the image the circle corresponding to the image that I want to calculate and also I want to mark with a pqno point the exact location where the algorithm identified the center.



      import cv2
      import numpy as np
      import matplotlib.pyplot as plt
      from matplotlib.offsetbox import AnchoredText


      thresh = cv2.imread('IMD044.png',0)
      _, contours,hierarchy = cv2.findContours(thresh,2,1)
      print (len(contours))
      cnt = contours

      for i in range (len(cnt)):
      (x,y),radius = cv2.minEnclosingCircle(cnt[i])
      center = (int(x),int(y))
      radius = int(radius)
      cv2.circle(thresh,center,radius,(0,255,0),2)
      print ('Circle: ' + str(i) + ' - Center: ' + str(center) + ' - Radius: ' + str(radius))
      plt.text(x-15, y+10, '+', fontsize=25, color = 'red')
      plt.text(10, -10, 'Centro: '+str(center), fontsize=11, color = 'red')
      plt.text(340, -10, 'Diametro: '+str((radius*2)/100)+'mm', fontsize=11, color = 'red')
      plt.Circle(x, y, color='red', fill=False)
      plt.imshow(thresh, cmap='gray')
      plt.show()


      I used the Opencv documentation to demarcate the contours and get the regions, but the green circle mark does not appear.



      Exit:



      enter image description here



      Exit expected:
      enter image description here



      updating the question I was able to add the information, it's only necessary to add the circle and check if the diameter is correct.










      share|improve this question
















      I am working on the recognition of the center and the image rendering. I'm using cv2.findContours to delimit the edges of the image of interest. And using cv.minEnclosingCircle (cnt) to circumnavigate my region of interest. The code below I can identify the center of each ROI, but I am not able to mark in the output of the image the circle corresponding to the image that I want to calculate and also I want to mark with a pqno point the exact location where the algorithm identified the center.



      import cv2
      import numpy as np
      import matplotlib.pyplot as plt
      from matplotlib.offsetbox import AnchoredText


      thresh = cv2.imread('IMD044.png',0)
      _, contours,hierarchy = cv2.findContours(thresh,2,1)
      print (len(contours))
      cnt = contours

      for i in range (len(cnt)):
      (x,y),radius = cv2.minEnclosingCircle(cnt[i])
      center = (int(x),int(y))
      radius = int(radius)
      cv2.circle(thresh,center,radius,(0,255,0),2)
      print ('Circle: ' + str(i) + ' - Center: ' + str(center) + ' - Radius: ' + str(radius))
      plt.text(x-15, y+10, '+', fontsize=25, color = 'red')
      plt.text(10, -10, 'Centro: '+str(center), fontsize=11, color = 'red')
      plt.text(340, -10, 'Diametro: '+str((radius*2)/100)+'mm', fontsize=11, color = 'red')
      plt.Circle(x, y, color='red', fill=False)
      plt.imshow(thresh, cmap='gray')
      plt.show()


      I used the Opencv documentation to demarcate the contours and get the regions, but the green circle mark does not appear.



      Exit:



      enter image description here



      Exit expected:
      enter image description here



      updating the question I was able to add the information, it's only necessary to add the circle and check if the diameter is correct.







      python image opencv image-processing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 19:29







      Tecnologia da Net

















      asked Nov 13 '18 at 18:32









      Tecnologia da NetTecnologia da Net

      979




      979
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You are using a single channel image, but trying to show 3-channel color. Add the following:



          ...
          thresh = cv2.imread('IMD016.png',0)
          _, contours,hierarchy = cv2.findContours(thresh,2,1)
          thresh = cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB)
          print (len(contours))
          ...


          Also, be extra careful when mixing the OpenCV and PyPlot drawing routines. OpenCV uses BGR by default, while PyPlot uses RGB. Also, cv2 drawing routines don't add extra channels, while the plt does. Just need to keep that in mind






          share|improve this answer


























          • I saw that I updated the question. Is the above code correct to measure the diameter of the region of interest? yes, I just need to add the circle to the plot, I'm trying here

            – Tecnologia da Net
            Nov 13 '18 at 19:30











          • I understand, I'm learning now to work with opencv. While to the diameter of the image, the code described above is correct? Can you help me about this?

            – Tecnologia da Net
            Nov 13 '18 at 19:43






          • 1





            I believe this is a diameter of the contour in terms of pixels. pixels don't automatically convert to mm

            – RafazZ
            Nov 13 '18 at 19:45











          • hey guy, I discovered that my cv2.imshow is no longer displaying any output image, it happened after I updated the version of opencv to 4. What will happen? I already researched in several corners, and some people going through it, but no answer. Eh so, I can not get outputs in the algorithm above.

            – Tecnologia da Net
            Nov 13 '18 at 21:29











          • If you want to convert the diameter to mm, you need to know the dpi of the image. diameter_in_mm = dpi * 3.93701 * diameter_in_pixels. Of course, this is assuming 1 dpi = 0.393701 pixel/cm

            – RafazZ
            Nov 13 '18 at 21:31











          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%2f53287431%2fhow-to-mark-the-center-and-calculate-the-diameter-of-an-image-using-opencv-pyt%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









          0














          You are using a single channel image, but trying to show 3-channel color. Add the following:



          ...
          thresh = cv2.imread('IMD016.png',0)
          _, contours,hierarchy = cv2.findContours(thresh,2,1)
          thresh = cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB)
          print (len(contours))
          ...


          Also, be extra careful when mixing the OpenCV and PyPlot drawing routines. OpenCV uses BGR by default, while PyPlot uses RGB. Also, cv2 drawing routines don't add extra channels, while the plt does. Just need to keep that in mind






          share|improve this answer


























          • I saw that I updated the question. Is the above code correct to measure the diameter of the region of interest? yes, I just need to add the circle to the plot, I'm trying here

            – Tecnologia da Net
            Nov 13 '18 at 19:30











          • I understand, I'm learning now to work with opencv. While to the diameter of the image, the code described above is correct? Can you help me about this?

            – Tecnologia da Net
            Nov 13 '18 at 19:43






          • 1





            I believe this is a diameter of the contour in terms of pixels. pixels don't automatically convert to mm

            – RafazZ
            Nov 13 '18 at 19:45











          • hey guy, I discovered that my cv2.imshow is no longer displaying any output image, it happened after I updated the version of opencv to 4. What will happen? I already researched in several corners, and some people going through it, but no answer. Eh so, I can not get outputs in the algorithm above.

            – Tecnologia da Net
            Nov 13 '18 at 21:29











          • If you want to convert the diameter to mm, you need to know the dpi of the image. diameter_in_mm = dpi * 3.93701 * diameter_in_pixels. Of course, this is assuming 1 dpi = 0.393701 pixel/cm

            – RafazZ
            Nov 13 '18 at 21:31
















          0














          You are using a single channel image, but trying to show 3-channel color. Add the following:



          ...
          thresh = cv2.imread('IMD016.png',0)
          _, contours,hierarchy = cv2.findContours(thresh,2,1)
          thresh = cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB)
          print (len(contours))
          ...


          Also, be extra careful when mixing the OpenCV and PyPlot drawing routines. OpenCV uses BGR by default, while PyPlot uses RGB. Also, cv2 drawing routines don't add extra channels, while the plt does. Just need to keep that in mind






          share|improve this answer


























          • I saw that I updated the question. Is the above code correct to measure the diameter of the region of interest? yes, I just need to add the circle to the plot, I'm trying here

            – Tecnologia da Net
            Nov 13 '18 at 19:30











          • I understand, I'm learning now to work with opencv. While to the diameter of the image, the code described above is correct? Can you help me about this?

            – Tecnologia da Net
            Nov 13 '18 at 19:43






          • 1





            I believe this is a diameter of the contour in terms of pixels. pixels don't automatically convert to mm

            – RafazZ
            Nov 13 '18 at 19:45











          • hey guy, I discovered that my cv2.imshow is no longer displaying any output image, it happened after I updated the version of opencv to 4. What will happen? I already researched in several corners, and some people going through it, but no answer. Eh so, I can not get outputs in the algorithm above.

            – Tecnologia da Net
            Nov 13 '18 at 21:29











          • If you want to convert the diameter to mm, you need to know the dpi of the image. diameter_in_mm = dpi * 3.93701 * diameter_in_pixels. Of course, this is assuming 1 dpi = 0.393701 pixel/cm

            – RafazZ
            Nov 13 '18 at 21:31














          0












          0








          0







          You are using a single channel image, but trying to show 3-channel color. Add the following:



          ...
          thresh = cv2.imread('IMD016.png',0)
          _, contours,hierarchy = cv2.findContours(thresh,2,1)
          thresh = cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB)
          print (len(contours))
          ...


          Also, be extra careful when mixing the OpenCV and PyPlot drawing routines. OpenCV uses BGR by default, while PyPlot uses RGB. Also, cv2 drawing routines don't add extra channels, while the plt does. Just need to keep that in mind






          share|improve this answer















          You are using a single channel image, but trying to show 3-channel color. Add the following:



          ...
          thresh = cv2.imread('IMD016.png',0)
          _, contours,hierarchy = cv2.findContours(thresh,2,1)
          thresh = cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB)
          print (len(contours))
          ...


          Also, be extra careful when mixing the OpenCV and PyPlot drawing routines. OpenCV uses BGR by default, while PyPlot uses RGB. Also, cv2 drawing routines don't add extra channels, while the plt does. Just need to keep that in mind







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 19:39

























          answered Nov 13 '18 at 19:21









          RafazZRafazZ

          1,037624




          1,037624













          • I saw that I updated the question. Is the above code correct to measure the diameter of the region of interest? yes, I just need to add the circle to the plot, I'm trying here

            – Tecnologia da Net
            Nov 13 '18 at 19:30











          • I understand, I'm learning now to work with opencv. While to the diameter of the image, the code described above is correct? Can you help me about this?

            – Tecnologia da Net
            Nov 13 '18 at 19:43






          • 1





            I believe this is a diameter of the contour in terms of pixels. pixels don't automatically convert to mm

            – RafazZ
            Nov 13 '18 at 19:45











          • hey guy, I discovered that my cv2.imshow is no longer displaying any output image, it happened after I updated the version of opencv to 4. What will happen? I already researched in several corners, and some people going through it, but no answer. Eh so, I can not get outputs in the algorithm above.

            – Tecnologia da Net
            Nov 13 '18 at 21:29











          • If you want to convert the diameter to mm, you need to know the dpi of the image. diameter_in_mm = dpi * 3.93701 * diameter_in_pixels. Of course, this is assuming 1 dpi = 0.393701 pixel/cm

            – RafazZ
            Nov 13 '18 at 21:31



















          • I saw that I updated the question. Is the above code correct to measure the diameter of the region of interest? yes, I just need to add the circle to the plot, I'm trying here

            – Tecnologia da Net
            Nov 13 '18 at 19:30











          • I understand, I'm learning now to work with opencv. While to the diameter of the image, the code described above is correct? Can you help me about this?

            – Tecnologia da Net
            Nov 13 '18 at 19:43






          • 1





            I believe this is a diameter of the contour in terms of pixels. pixels don't automatically convert to mm

            – RafazZ
            Nov 13 '18 at 19:45











          • hey guy, I discovered that my cv2.imshow is no longer displaying any output image, it happened after I updated the version of opencv to 4. What will happen? I already researched in several corners, and some people going through it, but no answer. Eh so, I can not get outputs in the algorithm above.

            – Tecnologia da Net
            Nov 13 '18 at 21:29











          • If you want to convert the diameter to mm, you need to know the dpi of the image. diameter_in_mm = dpi * 3.93701 * diameter_in_pixels. Of course, this is assuming 1 dpi = 0.393701 pixel/cm

            – RafazZ
            Nov 13 '18 at 21:31

















          I saw that I updated the question. Is the above code correct to measure the diameter of the region of interest? yes, I just need to add the circle to the plot, I'm trying here

          – Tecnologia da Net
          Nov 13 '18 at 19:30





          I saw that I updated the question. Is the above code correct to measure the diameter of the region of interest? yes, I just need to add the circle to the plot, I'm trying here

          – Tecnologia da Net
          Nov 13 '18 at 19:30













          I understand, I'm learning now to work with opencv. While to the diameter of the image, the code described above is correct? Can you help me about this?

          – Tecnologia da Net
          Nov 13 '18 at 19:43





          I understand, I'm learning now to work with opencv. While to the diameter of the image, the code described above is correct? Can you help me about this?

          – Tecnologia da Net
          Nov 13 '18 at 19:43




          1




          1





          I believe this is a diameter of the contour in terms of pixels. pixels don't automatically convert to mm

          – RafazZ
          Nov 13 '18 at 19:45





          I believe this is a diameter of the contour in terms of pixels. pixels don't automatically convert to mm

          – RafazZ
          Nov 13 '18 at 19:45













          hey guy, I discovered that my cv2.imshow is no longer displaying any output image, it happened after I updated the version of opencv to 4. What will happen? I already researched in several corners, and some people going through it, but no answer. Eh so, I can not get outputs in the algorithm above.

          – Tecnologia da Net
          Nov 13 '18 at 21:29





          hey guy, I discovered that my cv2.imshow is no longer displaying any output image, it happened after I updated the version of opencv to 4. What will happen? I already researched in several corners, and some people going through it, but no answer. Eh so, I can not get outputs in the algorithm above.

          – Tecnologia da Net
          Nov 13 '18 at 21:29













          If you want to convert the diameter to mm, you need to know the dpi of the image. diameter_in_mm = dpi * 3.93701 * diameter_in_pixels. Of course, this is assuming 1 dpi = 0.393701 pixel/cm

          – RafazZ
          Nov 13 '18 at 21:31





          If you want to convert the diameter to mm, you need to know the dpi of the image. diameter_in_mm = dpi * 3.93701 * diameter_in_pixels. Of course, this is assuming 1 dpi = 0.393701 pixel/cm

          – RafazZ
          Nov 13 '18 at 21:31


















          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%2f53287431%2fhow-to-mark-the-center-and-calculate-the-diameter-of-an-image-using-opencv-pyt%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

          Full-time equivalent

          Bicuculline

          さくらももこ