user432653
user432653

Reputation: 135

opencv perspectiveTransform function exception

how to use the perspectiveTransform function?

when run my code, Will produce the following exception:

OpenCV Error: Assertion failed (scn + 1 == m.cols && (depth == CV_32F || depth == CV_64F)) in perspectiveTransform, file /Users/donbe/Documents/opencv/opencv/modules/core/src/matmul.cpp, line 1916

Who can help me?

My code below:

Point2f srcTri[4];
Point2f dstTri[4];

Mat warp_mat;
Mat src;

/// Load the image
src = imread( argv[1], 1 );

srcTri[0] = Point2f(0,0);
srcTri[1] = Point2f(src.cols,0);
srcTri[2] = Point2f(src.cols,src.rows);
srcTri[3] = Point2f(0,src.rows);

dstTri[0] = Point2f(0,0);
dstTri[1] = Point2f(src.cols/2,0);
dstTri[2] = Point2f(src.cols/2,src.rows);
dstTri[3] = Point2f(0,src.rows);


warp_mat =  getPerspectiveTransform(srcTri, dstTri);

Mat warp_dst(src.size(), src.type());    

//There will produce a exception.
perspectiveTransform(src, warp_dst, warp_mat);

namedWindow( "Warp", CV_WINDOW_AUTOSIZE );
imshow( "Warp", warp_dst );

waitKey(0);
return 0;

Upvotes: 7

Views: 15603

Answers (3)

Jan Pokorný
Jan Pokorný

Reputation: 1868

When I had this issue in Python, I have discovered that it's because the input is supposed to be 3-dimensional:

>>> cv2.perspectiveTransform(np.float32([[1,1]]), np.float32([[1,0,0],[0,1,0],[0,0,1]]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.5.1) /tmp/pip-req-build-1syr35c1/opencv/modules/core/src/matmul.dispatch.cpp:531: error: (-215:Assertion failed) scn + 1 == m.cols in function 'perspectiveTransform'

>>> cv2.perspectiveTransform(np.float32([[[1,1]]]), np.float32([[1,0,0],[0,1,0],[0,0,1]]))
array([[[1., 1.]]], dtype=float32)

This is terribly communicated in the docs which just state "src: input two-channel or three-channel floating-point array; each element is a 2D/3D vector to be transformed", so the confusion is to be expected.

In order for the function to behave as expected -- i.e. operate on array of points -- call it like this:

dst = cv2.perspectiveTransform(src[np.newaxis], m)[0]

Upvotes: 1

Wahib Ul Haq
Wahib Ul Haq

Reputation: 4385

In my case, I was also stuck with same error and problem was with type of InputArray mtx. After changing the type of CvMat *obj to CV_32FC1 instead of CV_8UC1 solved it !

Upvotes: 1

Adrian
Adrian

Reputation: 2036

Have you checked your source image that it checks the requirements?

void perspectiveTransform(InputArray src, OutputArray dst, InputArray mtx)

Parameters: 

src – Source two-channel or three-channel floating-point array. Each element is a 2D/3D vector to be transformed.
dst – Destination array of the same size and type as src .
mtx – 3x3 or 4x4 floating-point transformation matrix.

Note:

The function transforms a sparse set of 2D or 3D vectors. If you want to transform an image using perspective transformation, use warpPerspective() .

Check the documentation for more details: http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html?highlight=perspectivetransform#cv2.perspectiveTransform

Hope this helps.

Upvotes: 6

Related Questions