Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make perspective transform of point with x and y coordinate

Tags:

python

opencv

So I wrote this little program which allows me to select 4 points on two images.

Usign those points I get a transformation matrix. After that I select a point on one of the images and want to get visualization of where that point will be on other image.

Say my point is marked like this -> (x,y) - so it's a tuple. How should I format this "position" on image so it can be possible to transform it.

I have looked at documentation for perspectiveTransform() method and figured that I should be storing it in following shape:

numpy.array([
        [self.points[self.length-1][0]],
        [self.points[self.length-1][1]]
        ], dtype="float32")

Which would give me on a single click this format:

Point= [[ 2300.]
        [  634.]]

This format doesn't seem to work, I use this Transformation matrix:

M = [[ -1.71913123e+00  -4.76850572e+00   5.27968944e+03]
     [  2.07693562e-01  -1.09738424e+01   6.35222770e+03]
     [  1.02865125e-04  -4.80067600e-03   1.00000000e+00]]

in this method (and get following error):

cv2.perspectiveTransform(src, M)
OpenCV Error: Assertion failed (scn + 1 == m.cols) in cv::perspectiveTransform, file C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\core\src\matmul.cpp

Any advice or tip is welcome.

like image 346
danchy Avatar asked Apr 12 '16 21:04

danchy


People also ask

What is the transformation matrix for perspective projection?

The projection matrix is typically a scale and perspective projection. The projection transformation converts the viewing frustum into a cuboid shape. The near end of the viewing frustum is smaller than the far end, which has the effect of expanding objects that are near to the camera.

How does perspective transform work?

A perspective transformation is simply a fractional linear equation that is solved from a matrix formation. The fractional linear equation is of the form that is linear in the numerator and linear in the denominator, i.e. first order terms at the highest in both numerator and denominator.

What is warp perspective?

We make use of a function called warpPerspective() function to fit the size of the resulting image by using the getPerspectiveTransform() function to the size of the original image or video. The warpPerspective() function returns an image or video whose size is the same as the size of the original image or video.


1 Answers

I figured out the answer.

Found it on this link

The key is to put your point like this:

 pts = numpy.array([[x,y]], dtype = "float32")

And then call another numpy.array on existing variable pts:

 pts = numpy.array([pts])

The procedure is the same after this.

like image 148
danchy Avatar answered Sep 18 '22 04:09

danchy