使用OpenCV warpPerspective时如何显示整个图像

时间:2012-10-25 06:57:17

标签: python image-processing opencv numpy computer-vision

我这里有2个测试图像。我的问题是,如何将第一张图像中的正方形映射到第二张图像中的四边形而不裁剪图像。

图片1: image 1

图片2: image 2

这是我当前使用openCV warpPerspective函数的代码。

import cv2
import numpy as np

img1_square_corners = np.float32([[253,211], [563,211], [563,519],[253,519]])
img2_quad_corners = np.float32([[234,197], [520,169], [715,483], [81,472]])

h, mask = cv2.findHomography(img1_square_corners, img2_quad_corners)
im = cv2.imread("image1.png")
out = cv2.warpPerspective(im, h, (800,800))
cv2.imwrite("result.png", out)

结果: result

正如您所看到的,由于warpPerspective函数中的dsize =(800,800)参数,我无法获得图像1的完整视图。如果我调整dsize,则方形将无法正确映射。有没有办法调整输出图像的大小,以便我可以得到图像1的全貌?

3 个答案:

答案 0 :(得分:33)

我的解决方案是计算结果图像大小,然后进行翻译。

def warpTwoImages(img1, img2, H):
    '''warp img2 to img1 with homograph H'''
    h1,w1 = img1.shape[:2]
    h2,w2 = img2.shape[:2]
    pts1 = float32([[0,0],[0,h1],[w1,h1],[w1,0]]).reshape(-1,1,2)
    pts2 = float32([[0,0],[0,h2],[w2,h2],[w2,0]]).reshape(-1,1,2)
    pts2_ = cv2.perspectiveTransform(pts2, H)
    pts = concatenate((pts1, pts2_), axis=0)
    [xmin, ymin] = int32(pts.min(axis=0).ravel() - 0.5)
    [xmax, ymax] = int32(pts.max(axis=0).ravel() + 0.5)
    t = [-xmin,-ymin]
    Ht = array([[1,0,t[0]],[0,1,t[1]],[0,0,1]]) # translate

    result = cv2.warpPerspective(img2, Ht.dot(H), (xmax-xmin, ymax-ymin))
    result[t[1]:h1+t[1],t[0]:w1+t[0]] = img1
    return result

dst_pts = float32([kp1[m.queryIdx].pt for m in good]).reshape(-1,1,2)
src_pts = float32([kp2[m.trainIdx].pt for m in good]).reshape(-1,1,2)
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)

result = warpTwoImages(img1_color, img2_color, M)

enter image description here

答案 1 :(得分:8)

是的,但您应该意识到输出图像可能非常大。我很快写了下面的Python代码,但即使是3000 x 3000的图像也不能适应输出,但由于转换,它太大了。虽然,这是我的代码,但我希望它对您有用。

import cv2
import numpy as np
import cv           #the old cv interface

img1_square_corners = np.float32([[253,211], [563,211], [563,519],[253,519]])
img2_quad_corners = np.float32([[234,197], [520,169], [715,483], [81,472]])

h, mask = cv2.findHomography(img1_square_corners, img2_quad_corners)
im = cv2.imread("image1.png")

在这里创建一个输出图像,我用(3000,3000)作为例子。

out_2 = cv.fromarray(np.zeros((3000,3000,3),np.uint8))

通过使用旧的cv接口,我直接写入输出,因此不会被裁剪。我使用cv2界面尝试了这个,但由于某些原因它没有用......也许有人可以对此有所了解?

cv.WarpPerspective(cv.fromarray(im), out_2, cv.fromarray(h))
cv.ShowImage("test", out_2)
cv.SaveImage("result.png", out_2)
cv2.waitKey()

无论如何,这会产生一个非常大的图像,其中包含原始图像1,扭曲。如果指定输出图像足够大,则整个图像将可见。 (确实可能非常大!)

我希望此代码可以为您提供帮助。

答案 2 :(得分:0)

首先,按照早期的解决方案来计算单应矩阵。在获得单应矩阵后,您需要相对于Homography矩阵扭曲图像。最后,合并扭曲的图像。

在这里,我将分享另一个可用于合并扭曲图像的想法。 (早期的答案,使用一系列索引进行叠加,这里我使用的是投资回报率掩盖)

屏蔽感兴趣区域(ROI)和黑色图像。然后使用ROI添加图像。 (见OpenCV Bitmask Tutorial

def copyOver(source, destination):
    result_grey = cv2.cvtColor(source, cv2.COLOR_BGR2GRAY)
    ret, mask = cv2.threshold(result_grey, 10, 255, cv2.THRESH_BINARY)
    mask_inv = cv2.bitwise_not(mask)
    roi = cv2.bitwise_and(source, source, mask=mask)
    im2 = cv2.bitwise_and(destination, destination, mask=mask_inv)
    result = cv2.add(im2, roi)
    return result


warpedImageB = cv2.warpPerspective(imageB, H, (imageA.shape[1], imageA.shape[0]))
result = copyOver(imageA, warpedImageB)

第一张图片:

First

第二张图片:

Second

缝合图像: Stitched