2017-06-20 68 views
0

我想使用getPerspectiveTransform函數,但它只接受Mat作爲參數,並且我有一個數組中的座標數據。所以,我將它們轉換成分數,然後墊上如下:Opencv4Android getPerspectiveTransform錯誤

List<Point> l = new ArrayList<Point>(); 
for (int i = 0; i < pts_1.length; i++) { 
    Point pt = new Point(pts_1[0][i], pts_1[1][i]); 
    l.add(i,pt); 
} 
Mat mat1 = Converters.vector_Point2f_to_Mat(l); 
for (int i = 0; i < pts_2.length; i++) { 
    Point pt = new Point(pts_2[0][i], pts_2[1][i]); 
    l.add(i,pt); 
    } 
Mat mat2 = Converters.vector_Point2f_to_Mat(l); 
Mat perspectiveTransform = Imgproc.getPerspectiveTransform(mat1,mat2); 

但是當我運行我的應用程序,那麼它給了我錯誤「出事了」,並在logcat中我收到以下錯誤:

E/cv::error(): OpenCV Error: Assertion failed (src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4) in cv::Mat cv::getPerspectiveTransform(cv::InputArray, cv::InputArray), file /build/master_pack-android/opencv/modules/imgproc/src/imgwarp.cpp, line 6748 
E/org.opencv.imgproc: imgproc::getPerspectiveTransform_10() caught cv::Exception: /build/master_pack-android/opencv/modules/imgproc/src/imgwarp.cpp:6748: error: (-215) src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4 in function cv::Mat cv::getPerspectiveTransform(cv::InputArray, cv::InputArray) 

我是OpenCV4Android的新手,所以我不明白爲什麼會發生這種情況。如何解決它?有沒有其他更好的方法來做到這一點?

感謝您的幫助!

注意:我知道這裏有一個類似的程序:Can't get OpenCV's warpPerspective to work on Android但他沒有得到這個錯誤,所以我在這裏發佈它。

+0

@AlexanderReynolds我沒有得到你,但我的Android工作室告訴我'Imgproc.getPerspectiveTransform(Mat src,Mat dst)'的格式爲 – Ank

+0

對不起---不習慣Java的實現。我懂了。你有四點? 'getPerspectiveTransform'恰好使用四點。如果你有四個以上的點,你應該使用'findHomography()'。 –

+0

@AlexanderReynolds yup正好四點 – Ank

回答

1

正如評論討論OP的帖子(所有爭光)提到的,問題出在列表l

List<Point> l = new ArrayList<Point>(); 
for (int i = 0; i < pts_1.length; i++) { 
    Point pt = new Point(pts_1[0][i], pts_1[1][i]); 
    l.add(i,pt); 
} 
Mat mat1 = Converters.vector_Point2f_to_Mat(l); 

如果我們看一看在List<Point> l的內容:

for (Point pt : l) 
    System.out.println("(" + pt.x + ", " + p.ty + ")"); 

(x0, y0) 
(x1, y1) 
(x2, y2) 
(x3, y3) 

而移動到下一個矩陣:

for (int i = 0; i < pts_2.length; i++) { 
    Point pt = new Point(pts_2[0][i], pts_2[1][i]); 
    l.add(i,pt); 
    } 
Mat mat2 = Converters.vector_Point2f_to_Mat(l); 

再看看內容List<Point> l

for (Point pt : l) 
    System.out.println("(" + pt.x + ", " + p.ty + ")"); 

(x4, y4) 
(x5, y5) 
(x6, y6) 
(x7, y7) 
(x0, y0) 
(x1, y1) 
(x2, y2) 
(x3, y3) 

所以這是罪魁禍首;你的第二個矩陣將有8個點。

Java docs for ArrayList

Parameters: index - index at which the specified element is to be inserted

使用l.add插入而不是覆蓋舊的列表。所以解決方案是爲每個轉換矩陣創建一個新列表。