2011-12-22 174 views
2

如何使用perspectiveTransform函數?opencv perspectiveTransform函數異常

當我的代碼運行時,會產生以下異常:

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

誰能幫我?

我下面的代碼:

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; 

回答

3

你檢查你的源圖像,它檢查的要求?

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. 

注:

函數變換稀疏集合2D或3D矢量。如果要使用透視轉換來轉換圖像,請使用warpPerspective()。

查詢詳細的文檔:http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html?highlight=perspectivetransform#cv2.perspectiveTransform

希望這有助於。

+0

謝謝。有用。 – user432653 2011-12-22 08:59:42

1

在我的情況下,我也被卡住了相同的錯誤,問題是與InputArray mtx的類型。將CvMat * obj的類型更改爲CV_32FC1而不是CV_8UC1後解決了它!