2017-04-05 184 views
0

我想使用opencv執行圖像變形。 我已經檢測到圖像的4個角((ex)4000x4000),並在opencv中使用getPerspectiveTransform獲得了變換矩陣。 現在,我想在圖像中心扭曲圖像。 所以我用warpPerspective。 我輸入的源圖像的大小是450x450,我使用我爲整個圖像計算的變換矩陣。圖像使用opencv變形

但屏幕上沒有顯示任何內容。 任何幫助表示讚賞。

這裏是示例代碼和示例圖像。

這是源圖像。 This is source image.

該圖像被檢測到的 This image is detected

這是我從圖像要翹曲區域。(src_crop在代碼)

enter image description here

這是結果。

enter image description here

 source[0] = Point2f(lt.x, lt.y); 
     source[1] = Point2f(rt.x, rt.y); 
     source[2] = Point2f(rb.x, rb.y); 
     source[3] = Point2f(lb.x, lb.y); 

     dst[0] = Point2f(6, 10); 
     dst[1] = Point2f(3899, 7); 
     dst[2] = Point2f(3901, 3899); 
     dst[3] = Point2f(9, 3902); 

     Mat transformMatrix ; 
     transformMatrix = getPerspectiveTransform(source, dst); 



     Mat dstFrame ; 
     warpPerspective(src_crop, dstFrame, transformMatrix, Size(450, 450)); 

回答

1

,因爲你使用了錯誤的值,請getPerspectiveTransform方法您的轉換失敗。您似乎混淆瞭如何創建輸出圖像,以及如何從該圖像的數據填充目標角落。

此外,鏈接數組中的右角(左上角,右上角,左下角,右下角)非常重要,您似乎會混淆它。

這個例子將告訴你如何正確的點,並將其輸出連接在一個空的輸出圖像:

// Assuming your source image is called 'sourceImage' and you have the corner points you need: 

// Create vectors to store the corners 
vector<Point2f> originalCorners; 
vector<Point2f> destinationCorners; 

// Put the Sudoku corners in your originalCorners 
originalCorners.clear(); 
originalCorners.push_back(Point2f(lt.x, lt.y); 
originalCorners.push_back(Point2f(rt.x, rt.y); 
originalCorners.push_back(Point2f(lb.x, lb.y); 
originalCorners.push_back(Point2f(rb.x, rb.y); 

// Output image size of 450x450 
int ouputImageWidth = 450; 
int outputImageHeight = 450; 

// Create an empty image (450x450) to output your transformation result in 
Mat transformedOutputImage(ouputImageWidth, outputImageHeight, sourceImage.type()); 

// Now put the corners of the output image so the warp knows where to warp to 
destinationCorners.clear(); 
destinationCorners.push_back(Point2f(0, 0)); 
destinationCorners.push_back(Point2f(ouputImageWidth, 0)); 
destinationCorners.push_back(Point2f(0, outputImageHeight)); 
destinationCorners.push_back(Point2f(ouputImageWidth, outputImageHeight)); 

// Now we have all corners sorted, so we can create the warp matrix 
Mat warpMatrix = getPerspectiveTransform(originalCorners, destinationCorners); 

// And now we can warp the Sudoku in the new image 
warpPerspective(sourceImage, transformedOutputImage, warpMatrix, Size(ouputImageWidth, ouputImageHeight)); 

現在,這是假定你知道你要扭曲的圖像部分的角點。如果你不知道如何獲得中間廣場的積分,我建議你看看these excellent answers

但是,只要你有四個角點這個方法將工作。您甚至可以通過手動查找中間方角點並插入這些點來嘗試。

+0

感謝您的回答。我知道計算整個圖像的轉換矩陣(4000乘4000)。但我輸入src_crop(450乘450)的圖像warpPerspective作爲源圖像。所以我認爲src_crop圖像使用變換矩陣變形。是不可能的?如果我不知道src_crop圖像的角點,我想爲src_crop圖像翹曲圖像 – eonjeo

+0

您應該使用'sourceImage'(4000x4000)而不是剪裁它。裁剪將導致您的'originalCorners'數組指向不存在的角點。在翹曲之前,我從來沒有做過糾錯,但這可能會導致您的輸出完全黑色的事實? – Jurjen

+0

其實我使用裁剪圖像是因爲減少了程序的運行時間。我使用matchTemplate函數來檢測裁剪圖像的中心點。所以我猜想裁剪圖像變形而不是整個圖像變形是變形的時間很短。 – eonjeo