我使用OpenCV的嘗試獲得此圖像的鳥瞰投影:
我先找到了棋盤的所有內角和吸引他們,如下所示
我然後使用warpPerspective(),但它會產生一個非常小的變形圖像,如所示。任何人都可以弄清楚是什麼造成了這個?鳥瞰投影誤差
這裏是我的代碼:
#include <ros/ros.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <vector>
using namespace cv ;
int main(int argc, char* argv[]) {
ros::init(argc, argv, "bird_view") ;
int board_w = atoi(argv[1]);
int board_h = atoi(argv[2]);
cv::Size board_sz(board_w, board_h);
cv::Mat image = cv::imread("image.jpg") ;
cv::Mat gray_image, tmp, H , birds_image;
cv::Point2f objPts[4], imgPts[4] ;
std::vector<cv::Point2f> corners ;
float Z = 1 ; //have experimented from values as low as .1 and as high as 100
int key = 0 ;
int found = cv::findChessboardCorners(image, board_sz, corners) ;
if (found) {
cv::drawChessboardCorners(image, board_sz, corners, 1) ;
cvtColor(image, gray_image, CV_RGB2GRAY) ;
cornerSubPix(gray_image, corners, Size(11, 11), Size(-1, -1),
TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1)) ;
cv::resize(image, tmp, Size(), .5, .5) ;
namedWindow("IMAGE") ;
cv::imshow("IMAGE" , tmp) ;
waitKey(0) ;
}
objPts[0].x = 0 ;
objPts[0].y = 0 ;
objPts[1].x = board_w-1 ;
objPts[0].y = 0 ;
objPts[0].x = 0 ;
objPts[0].y = board_h-1 ;
objPts[0].x = board_w-1 ;
objPts[0].y = board_h-1 ;
imgPts[0] = corners.at(0) ;
imgPts[1] = corners.at(board_w-1) ;
imgPts[2] = corners.at((board_h-1) * board_w) ;
imgPts[3] = corners.at((board_h-1) * board_w + board_w-1) ;
H = cv::getPerspectiveTransform(objPts, imgPts) ;
birds_image = image ;
while (key != 27) {
H.at<float>(2,2) = Z ;
cv::warpPerspective(image, birds_image, H, Size(2 * image.cols, 2 * tmp.rows) ,
CV_INTER_LINEAR | CV_WARP_INVERSE_MAP | CV_WARP_FILL_OUTLIERS) ;
cv::imshow("IMAGE", birds_image) ;
cv::waitKey(0) ;
}
return 0 ;
}
所有這些代碼是基於O'Reilly的OpenCV的書的鳥瞰投影例子。我認爲所得到的圖片是正確的,但我肯定不能確定。
謝謝,我添加了一些代碼,現在它工作正常! – joshualan 2013-04-28 21:00:31