extracting a quadrilateral image to a rectangle

后端 未结 3 1274
温柔的废话
温柔的废话 2020-11-29 02:27

\"A

BOUNTY UPDATE

3条回答
  •  迷失自我
    2020-11-29 02:56

    What you want is called planar rectification, and it's not all that simple, I'm afraid. What you need to do is recover the homography that maps this oblique view of the van side onto the front-facing view. Photoshop / etc. have tools to do this for you given some control points; if you want to implement it for yourself you'll have to start delving into computer vision.

    Edit - OK, here you go: a Python script to do the warping, using the OpenCV library which has convenient functions to calculate the homography and warp the image for you:

    import cv
    
    def warpImage(image, corners, target):
        mat = cv.CreateMat(3, 3, cv.CV_32F)
        cv.GetPerspectiveTransform(corners, target, mat)
        out = cv.CreateMat(height, width, cv.CV_8UC3)
        cv.WarpPerspective(image, out, mat, cv.CV_INTER_CUBIC)
        return out
    
    if __name__ == '__main__':
        width, height = 400, 250
        corners = [(171,72),(331,93),(333,188),(177,210)]
        target = [(0,0),(width,0),(width,height),(0,height)]
        image = cv.LoadImageM('fries.jpg')
        out = warpImage(image, corners, target)
        cv.SaveImage('fries_warped.jpg', out)
    

    And the output:
    Warped image

    OpenCV also has C and C++ bindings, or you can use EmguCV for a .NET wrapper; the API is fairly consistent across all languages so you can replicate this in whichever language suits your fancy.

提交回复
热议问题