2012-11-26 74 views
10

Tôi đã hai hình ảnh (A và B) hơi méo một từ khác, nơi có dịch, luân chuyển và quy mô khác nhau giữa chúng (ví dụ, những bức ảnh này :)LÀM THẾ NÀO ĐỂ sử dụng Homography để chuyển đổi hình ảnh trong OpenCV?

ORIGINAL LENA DISTORTED LENA


Ssoooooooo những gì tôi cần là để áp dụng một loại biến đổi trong pic B nên nó bù đắp sự biến dạng/dịch/luân chuyển mà tồn tại để làm cho cả hai hình ảnh với kích thước tương tự, định hướng và không có dịch

tôi đã đã trích xuất các điểm và tìm thấy Homography, như được hiển thị dưới đây. Nhưng tôi không biết làm thế nào để sử dụng Homography để biến đổi Mat img_B vì vậy nó trông giống như Mat img_A. Bất kỳ ý tưởng?

//-- Localize the object from img_1 in img_2 
std::vector<Point2f> obj; 
std::vector<Point2f> scene; 

for (unsigned int i = 0; i < good_matches.size(); i++) { 
    //-- Get the keypoints from the good matches 
    obj.push_back(keypoints_object[good_matches[i].queryIdx].pt); 
    scene.push_back(keypoints_scene[good_matches[i].trainIdx].pt); 
} 

Mat H = findHomography(obj, scene, CV_RANSAC); 

Chúc mừng,

Trả lời

5

Bạn muốn warpPerspective chức năng. Quy trình này tương tự với hướng dẫn được trình bày trong hướng dẫn this (đối với biến đổi affine và warps)

7

Bạn không cần đồng ý cho vấn đề này. Thay vào đó, bạn có thể tính toán biến đổi affine. Tuy nhiên, nếu bạn muốn sử dụng homography cho các mục đích khác, bạn có thể kiểm tra mã dưới đây. Bản sao được sao chép từ this bài viết chi tiết trên homography.

C++ Ví dụ

// pts_src and pts_dst are vectors of points in source 
// and destination images. They are of type vector<Point2f>. 
// We need at least 4 corresponding points. 

Mat h = findHomography(pts_src, pts_dst); 

// The calculated homography can be used to warp 
// the source image to destination. im_src and im_dst are 
// of type Mat. Size is the size (width,height) of im_dst. 

warpPerspective(im_src, im_dst, h, size); 

Python Ví dụ

''' 
pts_src and pts_dst are numpy arrays of points 
in source and destination images. We need at least 
4 corresponding points. 
''' 
h, status = cv2.findHomography(pts_src, pts_dst) 

''' 
The calculated homography can be used to warp 
the source image to destination. Size is the 
size (width,height) of im_dst 
''' 

im_dst = cv2.warpPerspective(im_src, h, size) 
Các vấn đề liên quan