How to use `cv2.perspectiveTransform` to apply homography on a set of points in Python OpenCV?How do I use the relationships between Flann matches to determine a sensible homography?How can I safely create a nested directory in Python?How to get the current time in PythonHow can I make a time delay in Python?How do I get the number of elements in a list in Python?How do I concatenate two lists in Python?How do I lowercase a string in Python?opencv perspectiveTransform function exceptionopencv error Assertion failed pythonopencv, python and RaspberryPiOpenCV perspectiveTransform broken function

Has any country ever had 2 former presidents in jail simultaneously?

How to hide some fields of struct in C?

Does malloc reserve more space while allocating memory?

Picking the different solutions to the time independent Schrodinger eqaution

Terse Method to Swap Lowest for Highest?

Calculate sum of polynomial roots

Add big quotation marks inside my colorbox

Does the Linux kernel need a file system to run?

Lowest total scrabble score

Recommended PCB layout understanding - ADM2572 datasheet

Unexpected behavior of the procedure `Area` on the object 'Polygon'

Invalid date error by date command

Is there a way to get `mathscr' with lower case letters in pdfLaTeX?

Redundant comparison & "if" before assignment

Is there a RAID 0 Equivalent for RAM?

Why is so much work done on numerical verification of the Riemann Hypothesis?

Yosemite Fire Rings - What to Expect?

Mixing PEX brands

What should you do when eye contact makes your subordinate uncomfortable?

Calculating total slots

Is this toilet slogan correct usage of the English language?

Extract more than nine arguments that occur periodically in a sentence to use in macros in order to typset

PTIJ: Haman's bad computer

Temporarily disable WLAN internet access for children, but allow it for adults



How to use `cv2.perspectiveTransform` to apply homography on a set of points in Python OpenCV?


How do I use the relationships between Flann matches to determine a sensible homography?How can I safely create a nested directory in Python?How to get the current time in PythonHow can I make a time delay in Python?How do I get the number of elements in a list in Python?How do I concatenate two lists in Python?How do I lowercase a string in Python?opencv perspectiveTransform function exceptionopencv error Assertion failed pythonopencv, python and RaspberryPiOpenCV perspectiveTransform broken function













1















I want to apply homography to the following points:



array([[-7.4894, 1.8873],
[-7.4973, 1.8543],
[-7.5375, 1.6725],
[-7.5681, 1.522 ],
[-7.5961, 1.371 ],
[-7.6252, 1.2013],
[-7.6504, 1.031 ],
[-7.667 , 0.8985],
[-7.6817, 0.7657],
[-7.6954, 0.613 ],
[-7.7054, 0.4786],
[-7.7124, 0.3452],
[-7.7182, 0.1931],
[-7.7215, 0.0866],
[-7.7716, 0.0872],
[-7.7715, 0.0929],
[-7.7651, 0.2884],
[-7.7587, 0.4269],
[-7.7528, 0.5233],
[-7.7418, 0.6616],
[-7.7275, 0.8116],
[-7.7048, 1.0032],
[-7.6916, 1.0988],
[-7.6686, 1.2478],
[-7.6352, 1.4379],
[-7.6091, 1.5741],
[-7.5784, 1.7219],
[-7.538 , 1.8995],
[-7.4894, 1.8873]], dtype=float32)


My camera homography matrix is like this:



array([[ 3.9643041e-04, 6.5913662e-07, 3.1965813e-03],
[ 7.4297395e-07, -3.9652368e-04, -4.4492882e-04],
[-9.3076696e-06, -3.5773560e-06, 1.0000000e+00]], dtype=float32)


When I try to apply homography with cv2.perspectiveTransform i get the following error:



`cv2.error: OpenCV(4.0.0) C:projectsopencv-pythonopencvmodulescoresrcmatmul.cpp:2270: error: (-215:Assertion failed) scn + 1 == m.cols in function 'cv::perspectiveTransform'`


I suspect i need another dimension for each point. But I am not sure how to add this dimension with numpy.



What is the correct way to solve this problem and is there a way to determine the root cause? The error message does not make much sense to me.










share|improve this question




























    1















    I want to apply homography to the following points:



    array([[-7.4894, 1.8873],
    [-7.4973, 1.8543],
    [-7.5375, 1.6725],
    [-7.5681, 1.522 ],
    [-7.5961, 1.371 ],
    [-7.6252, 1.2013],
    [-7.6504, 1.031 ],
    [-7.667 , 0.8985],
    [-7.6817, 0.7657],
    [-7.6954, 0.613 ],
    [-7.7054, 0.4786],
    [-7.7124, 0.3452],
    [-7.7182, 0.1931],
    [-7.7215, 0.0866],
    [-7.7716, 0.0872],
    [-7.7715, 0.0929],
    [-7.7651, 0.2884],
    [-7.7587, 0.4269],
    [-7.7528, 0.5233],
    [-7.7418, 0.6616],
    [-7.7275, 0.8116],
    [-7.7048, 1.0032],
    [-7.6916, 1.0988],
    [-7.6686, 1.2478],
    [-7.6352, 1.4379],
    [-7.6091, 1.5741],
    [-7.5784, 1.7219],
    [-7.538 , 1.8995],
    [-7.4894, 1.8873]], dtype=float32)


    My camera homography matrix is like this:



    array([[ 3.9643041e-04, 6.5913662e-07, 3.1965813e-03],
    [ 7.4297395e-07, -3.9652368e-04, -4.4492882e-04],
    [-9.3076696e-06, -3.5773560e-06, 1.0000000e+00]], dtype=float32)


    When I try to apply homography with cv2.perspectiveTransform i get the following error:



    `cv2.error: OpenCV(4.0.0) C:projectsopencv-pythonopencvmodulescoresrcmatmul.cpp:2270: error: (-215:Assertion failed) scn + 1 == m.cols in function 'cv::perspectiveTransform'`


    I suspect i need another dimension for each point. But I am not sure how to add this dimension with numpy.



    What is the correct way to solve this problem and is there a way to determine the root cause? The error message does not make much sense to me.










    share|improve this question


























      1












      1








      1








      I want to apply homography to the following points:



      array([[-7.4894, 1.8873],
      [-7.4973, 1.8543],
      [-7.5375, 1.6725],
      [-7.5681, 1.522 ],
      [-7.5961, 1.371 ],
      [-7.6252, 1.2013],
      [-7.6504, 1.031 ],
      [-7.667 , 0.8985],
      [-7.6817, 0.7657],
      [-7.6954, 0.613 ],
      [-7.7054, 0.4786],
      [-7.7124, 0.3452],
      [-7.7182, 0.1931],
      [-7.7215, 0.0866],
      [-7.7716, 0.0872],
      [-7.7715, 0.0929],
      [-7.7651, 0.2884],
      [-7.7587, 0.4269],
      [-7.7528, 0.5233],
      [-7.7418, 0.6616],
      [-7.7275, 0.8116],
      [-7.7048, 1.0032],
      [-7.6916, 1.0988],
      [-7.6686, 1.2478],
      [-7.6352, 1.4379],
      [-7.6091, 1.5741],
      [-7.5784, 1.7219],
      [-7.538 , 1.8995],
      [-7.4894, 1.8873]], dtype=float32)


      My camera homography matrix is like this:



      array([[ 3.9643041e-04, 6.5913662e-07, 3.1965813e-03],
      [ 7.4297395e-07, -3.9652368e-04, -4.4492882e-04],
      [-9.3076696e-06, -3.5773560e-06, 1.0000000e+00]], dtype=float32)


      When I try to apply homography with cv2.perspectiveTransform i get the following error:



      `cv2.error: OpenCV(4.0.0) C:projectsopencv-pythonopencvmodulescoresrcmatmul.cpp:2270: error: (-215:Assertion failed) scn + 1 == m.cols in function 'cv::perspectiveTransform'`


      I suspect i need another dimension for each point. But I am not sure how to add this dimension with numpy.



      What is the correct way to solve this problem and is there a way to determine the root cause? The error message does not make much sense to me.










      share|improve this question
















      I want to apply homography to the following points:



      array([[-7.4894, 1.8873],
      [-7.4973, 1.8543],
      [-7.5375, 1.6725],
      [-7.5681, 1.522 ],
      [-7.5961, 1.371 ],
      [-7.6252, 1.2013],
      [-7.6504, 1.031 ],
      [-7.667 , 0.8985],
      [-7.6817, 0.7657],
      [-7.6954, 0.613 ],
      [-7.7054, 0.4786],
      [-7.7124, 0.3452],
      [-7.7182, 0.1931],
      [-7.7215, 0.0866],
      [-7.7716, 0.0872],
      [-7.7715, 0.0929],
      [-7.7651, 0.2884],
      [-7.7587, 0.4269],
      [-7.7528, 0.5233],
      [-7.7418, 0.6616],
      [-7.7275, 0.8116],
      [-7.7048, 1.0032],
      [-7.6916, 1.0988],
      [-7.6686, 1.2478],
      [-7.6352, 1.4379],
      [-7.6091, 1.5741],
      [-7.5784, 1.7219],
      [-7.538 , 1.8995],
      [-7.4894, 1.8873]], dtype=float32)


      My camera homography matrix is like this:



      array([[ 3.9643041e-04, 6.5913662e-07, 3.1965813e-03],
      [ 7.4297395e-07, -3.9652368e-04, -4.4492882e-04],
      [-9.3076696e-06, -3.5773560e-06, 1.0000000e+00]], dtype=float32)


      When I try to apply homography with cv2.perspectiveTransform i get the following error:



      `cv2.error: OpenCV(4.0.0) C:projectsopencv-pythonopencvmodulescoresrcmatmul.cpp:2270: error: (-215:Assertion failed) scn + 1 == m.cols in function 'cv::perspectiveTransform'`


      I suspect i need another dimension for each point. But I am not sure how to add this dimension with numpy.



      What is the correct way to solve this problem and is there a way to determine the root cause? The error message does not make much sense to me.







      python numpy opencv homography






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 8 at 2:43









      Kinght 金

      8,52031942




      8,52031942










      asked Mar 8 at 1:56









      aramnhammeraramnhammer

      425




      425






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Make sure the pts shape is (n, 1, 2) or (1,n,2):



           pts = np.float32(pts).reshape(-1,1,2)
          #pts = np.array([pts], np.float32)
          cv2.perspectiveTransform(pts, M)



          For example:



          pts = np.array([[1,2,],[3,4]], np.float32)
          M = np.array([[ 3.9643041e-04, 6.5913662e-07, 3.1965813e-03],
          [ 7.4297395e-07, -3.9652368e-04, -4.4492882e-04],
          [-9.3076696e-06, -3.5773560e-06, 1.0000000e+00]], dtype=np.float32)

          ## (n, 1, 2)
          pts1 = pts.reshape(-1,1,2).astype(np.float32)
          dst1 = cv2.perspectiveTransform(pts1, M)

          ## (1, n, 2)
          pts2 = np.array([pts], np.float32)
          dst2 = cv2.perspectiveTransform(pts2, M)



          Result:



          >>> print(pts1)
          [[[1. 2.]]

          [[3. 4.]]]
          >>> print(dst1)
          [[[ 0.00359439 -0.00123725]]

          [[ 0.00438869 -0.00202888]]]
          >>> print(pts2)
          [[[1. 2.]
          [3. 4.]]]
          >>> print(dst2)
          [[[ 0.00359439 -0.00123725]
          [ 0.00438869 -0.00202888]]]



          Here is another example:



          How do I use the relationships between Flann matches to determine a sensible homography?






          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%2f55055655%2fhow-to-use-cv2-perspectivetransform-to-apply-homography-on-a-set-of-points-in%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














            Make sure the pts shape is (n, 1, 2) or (1,n,2):



             pts = np.float32(pts).reshape(-1,1,2)
            #pts = np.array([pts], np.float32)
            cv2.perspectiveTransform(pts, M)



            For example:



            pts = np.array([[1,2,],[3,4]], np.float32)
            M = np.array([[ 3.9643041e-04, 6.5913662e-07, 3.1965813e-03],
            [ 7.4297395e-07, -3.9652368e-04, -4.4492882e-04],
            [-9.3076696e-06, -3.5773560e-06, 1.0000000e+00]], dtype=np.float32)

            ## (n, 1, 2)
            pts1 = pts.reshape(-1,1,2).astype(np.float32)
            dst1 = cv2.perspectiveTransform(pts1, M)

            ## (1, n, 2)
            pts2 = np.array([pts], np.float32)
            dst2 = cv2.perspectiveTransform(pts2, M)



            Result:



            >>> print(pts1)
            [[[1. 2.]]

            [[3. 4.]]]
            >>> print(dst1)
            [[[ 0.00359439 -0.00123725]]

            [[ 0.00438869 -0.00202888]]]
            >>> print(pts2)
            [[[1. 2.]
            [3. 4.]]]
            >>> print(dst2)
            [[[ 0.00359439 -0.00123725]
            [ 0.00438869 -0.00202888]]]



            Here is another example:



            How do I use the relationships between Flann matches to determine a sensible homography?






            share|improve this answer





























              1














              Make sure the pts shape is (n, 1, 2) or (1,n,2):



               pts = np.float32(pts).reshape(-1,1,2)
              #pts = np.array([pts], np.float32)
              cv2.perspectiveTransform(pts, M)



              For example:



              pts = np.array([[1,2,],[3,4]], np.float32)
              M = np.array([[ 3.9643041e-04, 6.5913662e-07, 3.1965813e-03],
              [ 7.4297395e-07, -3.9652368e-04, -4.4492882e-04],
              [-9.3076696e-06, -3.5773560e-06, 1.0000000e+00]], dtype=np.float32)

              ## (n, 1, 2)
              pts1 = pts.reshape(-1,1,2).astype(np.float32)
              dst1 = cv2.perspectiveTransform(pts1, M)

              ## (1, n, 2)
              pts2 = np.array([pts], np.float32)
              dst2 = cv2.perspectiveTransform(pts2, M)



              Result:



              >>> print(pts1)
              [[[1. 2.]]

              [[3. 4.]]]
              >>> print(dst1)
              [[[ 0.00359439 -0.00123725]]

              [[ 0.00438869 -0.00202888]]]
              >>> print(pts2)
              [[[1. 2.]
              [3. 4.]]]
              >>> print(dst2)
              [[[ 0.00359439 -0.00123725]
              [ 0.00438869 -0.00202888]]]



              Here is another example:



              How do I use the relationships between Flann matches to determine a sensible homography?






              share|improve this answer



























                1












                1








                1







                Make sure the pts shape is (n, 1, 2) or (1,n,2):



                 pts = np.float32(pts).reshape(-1,1,2)
                #pts = np.array([pts], np.float32)
                cv2.perspectiveTransform(pts, M)



                For example:



                pts = np.array([[1,2,],[3,4]], np.float32)
                M = np.array([[ 3.9643041e-04, 6.5913662e-07, 3.1965813e-03],
                [ 7.4297395e-07, -3.9652368e-04, -4.4492882e-04],
                [-9.3076696e-06, -3.5773560e-06, 1.0000000e+00]], dtype=np.float32)

                ## (n, 1, 2)
                pts1 = pts.reshape(-1,1,2).astype(np.float32)
                dst1 = cv2.perspectiveTransform(pts1, M)

                ## (1, n, 2)
                pts2 = np.array([pts], np.float32)
                dst2 = cv2.perspectiveTransform(pts2, M)



                Result:



                >>> print(pts1)
                [[[1. 2.]]

                [[3. 4.]]]
                >>> print(dst1)
                [[[ 0.00359439 -0.00123725]]

                [[ 0.00438869 -0.00202888]]]
                >>> print(pts2)
                [[[1. 2.]
                [3. 4.]]]
                >>> print(dst2)
                [[[ 0.00359439 -0.00123725]
                [ 0.00438869 -0.00202888]]]



                Here is another example:



                How do I use the relationships between Flann matches to determine a sensible homography?






                share|improve this answer















                Make sure the pts shape is (n, 1, 2) or (1,n,2):



                 pts = np.float32(pts).reshape(-1,1,2)
                #pts = np.array([pts], np.float32)
                cv2.perspectiveTransform(pts, M)



                For example:



                pts = np.array([[1,2,],[3,4]], np.float32)
                M = np.array([[ 3.9643041e-04, 6.5913662e-07, 3.1965813e-03],
                [ 7.4297395e-07, -3.9652368e-04, -4.4492882e-04],
                [-9.3076696e-06, -3.5773560e-06, 1.0000000e+00]], dtype=np.float32)

                ## (n, 1, 2)
                pts1 = pts.reshape(-1,1,2).astype(np.float32)
                dst1 = cv2.perspectiveTransform(pts1, M)

                ## (1, n, 2)
                pts2 = np.array([pts], np.float32)
                dst2 = cv2.perspectiveTransform(pts2, M)



                Result:



                >>> print(pts1)
                [[[1. 2.]]

                [[3. 4.]]]
                >>> print(dst1)
                [[[ 0.00359439 -0.00123725]]

                [[ 0.00438869 -0.00202888]]]
                >>> print(pts2)
                [[[1. 2.]
                [3. 4.]]]
                >>> print(dst2)
                [[[ 0.00359439 -0.00123725]
                [ 0.00438869 -0.00202888]]]



                Here is another example:



                How do I use the relationships between Flann matches to determine a sensible homography?







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 8 at 2:40

























                answered Mar 8 at 2:23









                Kinght 金Kinght 金

                8,52031942




                8,52031942





























                    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%2f55055655%2fhow-to-use-cv2-perspectivetransform-to-apply-homography-on-a-set-of-points-in%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

                    How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

                    Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                    List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229