how to extract x,y coordinates from OpenCV “cv2.keypoint” object?












5















I tried to use the following code:



    xCoordinate=point.x


(point is type of cv2.keyPoint)
It gives me error saying cv2.keyPoint has no attribute 'x'










share|improve this question



























    5















    I tried to use the following code:



        xCoordinate=point.x


    (point is type of cv2.keyPoint)
    It gives me error saying cv2.keyPoint has no attribute 'x'










    share|improve this question

























      5












      5








      5


      3






      I tried to use the following code:



          xCoordinate=point.x


      (point is type of cv2.keyPoint)
      It gives me error saying cv2.keyPoint has no attribute 'x'










      share|improve this question














      I tried to use the following code:



          xCoordinate=point.x


      (point is type of cv2.keyPoint)
      It gives me error saying cv2.keyPoint has no attribute 'x'







      python opencv numpy






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 9 '16 at 6:21









      mengmengxyzmengmengxyz

      1442510




      1442510
























          5 Answers
          5






          active

          oldest

          votes


















          7














          You can use:



          import numpy as np

          pts = np.float([kp[idx].pt for idx in len(kp)]).reshape(-1, 1, 2)


          pts will be an array of keypoints.






          share|improve this answer































            9














            point.pt is a tuple(x,y)`.



            So,



            x = point.pt[0]
            y = point.pt[1]


            or,



            (x,y) = point.pt





            share|improve this answer

































              2














              Read the docs.



              class KeyPoint



              Data structure for salient point detectors.




              • Point2f pt
                -- coordinates of the keypoint


              • float size
                -- diameter of the meaningful keypoint neighborhood


              • float angle ...¶



              So point.pt is a Point2f.



              Try x,y= point.pt






              share|improve this answer


























              • point.pt.x is invalid, please correct it as: x, y = point.pt

                – Nirmal
                Oct 26 '18 at 5:30



















              1














              Here is my take (runable code):



              import cv2, os
              import numpy as np
              import matplotlib.pyplot as plt

              # INITIALISATION
              filename = os.path.join('foo', 'bar.jpg')
              img0 = cv2.imread(filename) # original image
              gray = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY) # convert to grayscale
              sift = cv2.xfeatures2d.SIFT_create() # initialize SIFT
              f, (ax1, ax2) = plt.subplots(1, 2) # create subplots

              # DETECT AND DRAW KEYPOINTS
              # sift.detect() returns a list of keypoints
              # keypoint is a standard class of opencv (not just SIFT-related)
              kp = sift.detect(gray,None) # calculates SIFT points
              img1=cv2.drawKeypoints(gray,kp, None) # mae new image with keypoints drawn
              ax1.imshow(img1) # plot

              # RETREIVE KEYPOINTS COORDINATES AND DRAW MANUALLY
              # Reade these and make numpy array
              pts = np.asarray([[p.pt[0], p.pt[1]] for p in kp])
              cols = pts[:,0]
              rows = pts[:,1]
              ax2.imshow(cv2.cvtColor(img0, cv2.COLOR_BGR2RGB))
              ax2.scatter(cols, rows)

              plt.show()





              share|improve this answer

































                0














                I solved your problem like this.



                kp,des = surf.detectAndCompute(img,None) 
                pts = [p.pt for p in kp]


                Now you get a list of x,y co-ordinates for all keypoints in your image.






                share|improve this answer

























                  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%2f35884409%2fhow-to-extract-x-y-coordinates-from-opencv-cv2-keypoint-object%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  7














                  You can use:



                  import numpy as np

                  pts = np.float([kp[idx].pt for idx in len(kp)]).reshape(-1, 1, 2)


                  pts will be an array of keypoints.






                  share|improve this answer




























                    7














                    You can use:



                    import numpy as np

                    pts = np.float([kp[idx].pt for idx in len(kp)]).reshape(-1, 1, 2)


                    pts will be an array of keypoints.






                    share|improve this answer


























                      7












                      7








                      7







                      You can use:



                      import numpy as np

                      pts = np.float([kp[idx].pt for idx in len(kp)]).reshape(-1, 1, 2)


                      pts will be an array of keypoints.






                      share|improve this answer













                      You can use:



                      import numpy as np

                      pts = np.float([kp[idx].pt for idx in len(kp)]).reshape(-1, 1, 2)


                      pts will be an array of keypoints.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 9 '16 at 8:40









                      Francesco NazzaroFrancesco Nazzaro

                      1,809617




                      1,809617

























                          9














                          point.pt is a tuple(x,y)`.



                          So,



                          x = point.pt[0]
                          y = point.pt[1]


                          or,



                          (x,y) = point.pt





                          share|improve this answer






























                            9














                            point.pt is a tuple(x,y)`.



                            So,



                            x = point.pt[0]
                            y = point.pt[1]


                            or,



                            (x,y) = point.pt





                            share|improve this answer




























                              9












                              9








                              9







                              point.pt is a tuple(x,y)`.



                              So,



                              x = point.pt[0]
                              y = point.pt[1]


                              or,



                              (x,y) = point.pt





                              share|improve this answer















                              point.pt is a tuple(x,y)`.



                              So,



                              x = point.pt[0]
                              y = point.pt[1]


                              or,



                              (x,y) = point.pt






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Dec 22 '18 at 4:45









                              skumhest

                              54




                              54










                              answered May 6 '17 at 8:13









                              saikat sarkarsaikat sarkar

                              9114




                              9114























                                  2














                                  Read the docs.



                                  class KeyPoint



                                  Data structure for salient point detectors.




                                  • Point2f pt
                                    -- coordinates of the keypoint


                                  • float size
                                    -- diameter of the meaningful keypoint neighborhood


                                  • float angle ...¶



                                  So point.pt is a Point2f.



                                  Try x,y= point.pt






                                  share|improve this answer


























                                  • point.pt.x is invalid, please correct it as: x, y = point.pt

                                    – Nirmal
                                    Oct 26 '18 at 5:30
















                                  2














                                  Read the docs.



                                  class KeyPoint



                                  Data structure for salient point detectors.




                                  • Point2f pt
                                    -- coordinates of the keypoint


                                  • float size
                                    -- diameter of the meaningful keypoint neighborhood


                                  • float angle ...¶



                                  So point.pt is a Point2f.



                                  Try x,y= point.pt






                                  share|improve this answer


























                                  • point.pt.x is invalid, please correct it as: x, y = point.pt

                                    – Nirmal
                                    Oct 26 '18 at 5:30














                                  2












                                  2








                                  2







                                  Read the docs.



                                  class KeyPoint



                                  Data structure for salient point detectors.




                                  • Point2f pt
                                    -- coordinates of the keypoint


                                  • float size
                                    -- diameter of the meaningful keypoint neighborhood


                                  • float angle ...¶



                                  So point.pt is a Point2f.



                                  Try x,y= point.pt






                                  share|improve this answer















                                  Read the docs.



                                  class KeyPoint



                                  Data structure for salient point detectors.




                                  • Point2f pt
                                    -- coordinates of the keypoint


                                  • float size
                                    -- diameter of the meaningful keypoint neighborhood


                                  • float angle ...¶



                                  So point.pt is a Point2f.



                                  Try x,y= point.pt







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Nov 20 '18 at 2:05

























                                  answered Mar 9 '16 at 6:37









                                  roadrunner66roadrunner66

                                  3,74321834




                                  3,74321834













                                  • point.pt.x is invalid, please correct it as: x, y = point.pt

                                    – Nirmal
                                    Oct 26 '18 at 5:30



















                                  • point.pt.x is invalid, please correct it as: x, y = point.pt

                                    – Nirmal
                                    Oct 26 '18 at 5:30

















                                  point.pt.x is invalid, please correct it as: x, y = point.pt

                                  – Nirmal
                                  Oct 26 '18 at 5:30





                                  point.pt.x is invalid, please correct it as: x, y = point.pt

                                  – Nirmal
                                  Oct 26 '18 at 5:30











                                  1














                                  Here is my take (runable code):



                                  import cv2, os
                                  import numpy as np
                                  import matplotlib.pyplot as plt

                                  # INITIALISATION
                                  filename = os.path.join('foo', 'bar.jpg')
                                  img0 = cv2.imread(filename) # original image
                                  gray = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY) # convert to grayscale
                                  sift = cv2.xfeatures2d.SIFT_create() # initialize SIFT
                                  f, (ax1, ax2) = plt.subplots(1, 2) # create subplots

                                  # DETECT AND DRAW KEYPOINTS
                                  # sift.detect() returns a list of keypoints
                                  # keypoint is a standard class of opencv (not just SIFT-related)
                                  kp = sift.detect(gray,None) # calculates SIFT points
                                  img1=cv2.drawKeypoints(gray,kp, None) # mae new image with keypoints drawn
                                  ax1.imshow(img1) # plot

                                  # RETREIVE KEYPOINTS COORDINATES AND DRAW MANUALLY
                                  # Reade these and make numpy array
                                  pts = np.asarray([[p.pt[0], p.pt[1]] for p in kp])
                                  cols = pts[:,0]
                                  rows = pts[:,1]
                                  ax2.imshow(cv2.cvtColor(img0, cv2.COLOR_BGR2RGB))
                                  ax2.scatter(cols, rows)

                                  plt.show()





                                  share|improve this answer






























                                    1














                                    Here is my take (runable code):



                                    import cv2, os
                                    import numpy as np
                                    import matplotlib.pyplot as plt

                                    # INITIALISATION
                                    filename = os.path.join('foo', 'bar.jpg')
                                    img0 = cv2.imread(filename) # original image
                                    gray = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY) # convert to grayscale
                                    sift = cv2.xfeatures2d.SIFT_create() # initialize SIFT
                                    f, (ax1, ax2) = plt.subplots(1, 2) # create subplots

                                    # DETECT AND DRAW KEYPOINTS
                                    # sift.detect() returns a list of keypoints
                                    # keypoint is a standard class of opencv (not just SIFT-related)
                                    kp = sift.detect(gray,None) # calculates SIFT points
                                    img1=cv2.drawKeypoints(gray,kp, None) # mae new image with keypoints drawn
                                    ax1.imshow(img1) # plot

                                    # RETREIVE KEYPOINTS COORDINATES AND DRAW MANUALLY
                                    # Reade these and make numpy array
                                    pts = np.asarray([[p.pt[0], p.pt[1]] for p in kp])
                                    cols = pts[:,0]
                                    rows = pts[:,1]
                                    ax2.imshow(cv2.cvtColor(img0, cv2.COLOR_BGR2RGB))
                                    ax2.scatter(cols, rows)

                                    plt.show()





                                    share|improve this answer




























                                      1












                                      1








                                      1







                                      Here is my take (runable code):



                                      import cv2, os
                                      import numpy as np
                                      import matplotlib.pyplot as plt

                                      # INITIALISATION
                                      filename = os.path.join('foo', 'bar.jpg')
                                      img0 = cv2.imread(filename) # original image
                                      gray = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY) # convert to grayscale
                                      sift = cv2.xfeatures2d.SIFT_create() # initialize SIFT
                                      f, (ax1, ax2) = plt.subplots(1, 2) # create subplots

                                      # DETECT AND DRAW KEYPOINTS
                                      # sift.detect() returns a list of keypoints
                                      # keypoint is a standard class of opencv (not just SIFT-related)
                                      kp = sift.detect(gray,None) # calculates SIFT points
                                      img1=cv2.drawKeypoints(gray,kp, None) # mae new image with keypoints drawn
                                      ax1.imshow(img1) # plot

                                      # RETREIVE KEYPOINTS COORDINATES AND DRAW MANUALLY
                                      # Reade these and make numpy array
                                      pts = np.asarray([[p.pt[0], p.pt[1]] for p in kp])
                                      cols = pts[:,0]
                                      rows = pts[:,1]
                                      ax2.imshow(cv2.cvtColor(img0, cv2.COLOR_BGR2RGB))
                                      ax2.scatter(cols, rows)

                                      plt.show()





                                      share|improve this answer















                                      Here is my take (runable code):



                                      import cv2, os
                                      import numpy as np
                                      import matplotlib.pyplot as plt

                                      # INITIALISATION
                                      filename = os.path.join('foo', 'bar.jpg')
                                      img0 = cv2.imread(filename) # original image
                                      gray = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY) # convert to grayscale
                                      sift = cv2.xfeatures2d.SIFT_create() # initialize SIFT
                                      f, (ax1, ax2) = plt.subplots(1, 2) # create subplots

                                      # DETECT AND DRAW KEYPOINTS
                                      # sift.detect() returns a list of keypoints
                                      # keypoint is a standard class of opencv (not just SIFT-related)
                                      kp = sift.detect(gray,None) # calculates SIFT points
                                      img1=cv2.drawKeypoints(gray,kp, None) # mae new image with keypoints drawn
                                      ax1.imshow(img1) # plot

                                      # RETREIVE KEYPOINTS COORDINATES AND DRAW MANUALLY
                                      # Reade these and make numpy array
                                      pts = np.asarray([[p.pt[0], p.pt[1]] for p in kp])
                                      cols = pts[:,0]
                                      rows = pts[:,1]
                                      ax2.imshow(cv2.cvtColor(img0, cv2.COLOR_BGR2RGB))
                                      ax2.scatter(cols, rows)

                                      plt.show()






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 2 '18 at 12:58

























                                      answered Nov 2 '18 at 10:40









                                      quickbugquickbug

                                      1,65931117




                                      1,65931117























                                          0














                                          I solved your problem like this.



                                          kp,des = surf.detectAndCompute(img,None) 
                                          pts = [p.pt for p in kp]


                                          Now you get a list of x,y co-ordinates for all keypoints in your image.






                                          share|improve this answer






























                                            0














                                            I solved your problem like this.



                                            kp,des = surf.detectAndCompute(img,None) 
                                            pts = [p.pt for p in kp]


                                            Now you get a list of x,y co-ordinates for all keypoints in your image.






                                            share|improve this answer




























                                              0












                                              0








                                              0







                                              I solved your problem like this.



                                              kp,des = surf.detectAndCompute(img,None) 
                                              pts = [p.pt for p in kp]


                                              Now you get a list of x,y co-ordinates for all keypoints in your image.






                                              share|improve this answer















                                              I solved your problem like this.



                                              kp,des = surf.detectAndCompute(img,None) 
                                              pts = [p.pt for p in kp]


                                              Now you get a list of x,y co-ordinates for all keypoints in your image.







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Oct 6 '17 at 9:06









                                              Shaido

                                              12.6k122541




                                              12.6k122541










                                              answered Oct 6 '17 at 8:49









                                              Alexander HuntAlexander Hunt

                                              1




                                              1






























                                                  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%2f35884409%2fhow-to-extract-x-y-coordinates-from-opencv-cv2-keypoint-object%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

                                                  Guess what letter conforming each word

                                                  Port of Spain

                                                  Run scheduled task as local user group (not BUILTIN)