15

一张照片

赏金更新

按照Denis 的链接,这是如何使用threeblindmiceandamonkey代码:

// the destination rect is our 'in' quad
int dw = 300, dh = 250;
double in[4][4] = {{0,0},{dw,0},{dw,dh},{0,dh}};
    // the quad in the source image is our 'out'
double out[4][5] = {{171,72},{331,93},{333,188},{177,210}};
double homo[3][6];
const int ret = mapQuadToQuad(in,out,homo);
    // homo can be used for calculating the x,y of any destination point
// in the source, e.g.
for(int i=0; i<4; i++) {
    double p1[3] = {out[i][0],out[i][7],1};
    double p2[3];
    transformMatrix(p1,p2,homo);
    p2[0] /= p2[2]; // x
    p2[1] /= p2[2]; // y
    printf("\t%2.2f\t%2.2f\n",p2[0],p2[1]);
}

这为将目标中的点转换为源提供了一种转换 - 您当然可以反过来做,但是能够为混合做到这一点很整洁:

for(int y=0; y<dh; y++) {
    for(int x=0; x<dw; x++) {
        // calc the four corners in source for this
        // destination pixel, and mix

对于混合,我使用带有随机点的超级采样;即使源区域和目标区域存在很大差异,它也能很好地工作


背景问题

在顶部的图像中,货车侧面的标志没有面向相机。我想尽我所能用我拥有的像素来计算它看起来像什么。

我知道图像中四边形的角坐标,以及目标矩形的大小。

我想这是通过 x 和 y 轴的某种循环,同时在两个维度上执行 Bresenham 线,随着源图像和目标图像中的像素重叠而进行某种混合 - 某种亚像素混合?

有哪些方法,如何混合像素?

对此有标准方法吗?

4

4 回答 4

18

你想要的叫做平面整流,恐怕不是那么简单。您需要做的是恢复将面包车侧面的斜视图映射到前视图的单应性。Photoshop / etc.有工具可以为您提供一些控制点;如果你想自己实现它,你必须开始钻研计算机视觉。

编辑- 好的,你去:一个 Python 脚本来做变形,使用OpenCV库,它有方便的函数来计算单应性和变形图像:

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)

和输出:
扭曲的图像

OpenCV 还具有 C 和 C++ 绑定,或者您可以将EmguCV用于 .NET 包装器;该 API 在所有语言中都相当一致,因此您可以使用任何适合您的语言来复制它。

于 2010-06-07T20:09:42.040 回答
4

向上看"quad to quad" transform,例如 threeblindmiceandamonkey 。 2d 齐次坐标上的 3x3 变换可以将任何 4 个点(一个四边形)转换为任何其他四边形;相反,任何 fromquad 和 toquad,例如卡车的角和目标矩形,都会产生 3 x 3 的变换。

Qt 有quadToQuad 并且可以用它转换像素图,但我猜你没有 Qt ?
6 月 10 日添加:来自labs.trolltech.com/page/Graphics/Examples 有一个很好的演示,它在您移动角落时将像素图四对四:

替代文字

6 月 11 日添加:@Will,这是 Python 中的 translate.h(您知道一点?“”“...”“”是多行注释。)
perstrans()是关键;希望这是有道理的,如果不问的话。

顺便说一句,您可以将像素一张一张地映射,mapQuadToQuad(target rect, orig quad),但是如果没有像素插值,它看起来会很糟糕;OpenCV 可以做到这一切。

#!/usr/bin/env python
""" square <-> quad maps
    from http://threeblindmiceandamonkey.com/?p=16 matrix.h
"""

from __future__ import division
import numpy as np

__date__ = "2010-06-11 jun denis"

def det2(a, b, c, d):
    return a*d - b*c

def mapSquareToQuad( quad ):  # [4][2]
    SQ = np.zeros((3,3))
    px = quad[0,0] - quad[1,0] + quad[2,0] - quad[3,0]
    py = quad[0,1] - quad[1,1] + quad[2,1] - quad[3,1]
    if abs(px) < 1e-10 and abs(py) < 1e-10:
        SQ[0,0] = quad[1,0] - quad[0,0]
        SQ[1,0] = quad[2,0] - quad[1,0]
        SQ[2,0] = quad[0,0]
        SQ[0,1] = quad[1,1] - quad[0,1]
        SQ[1,1] = quad[2,1] - quad[1,1]
        SQ[2,1] = quad[0,1]
        SQ[0,2] = 0.
        SQ[1,2] = 0.
        SQ[2,2] = 1.
        return SQ
    else:
        dx1 = quad[1,0] - quad[2,0]
        dx2 = quad[3,0] - quad[2,0]
        dy1 = quad[1,1] - quad[2,1]
        dy2 = quad[3,1] - quad[2,1]
        det = det2(dx1,dx2, dy1,dy2)
        if det == 0.:
            return None
        SQ[0,2] = det2(px,dx2, py,dy2) / det
        SQ[1,2] = det2(dx1,px, dy1,py) / det
        SQ[2,2] = 1.
        SQ[0,0] = quad[1,0] - quad[0,0] + SQ[0,2]*quad[1,0]
        SQ[1,0] = quad[3,0] - quad[0,0] + SQ[1,2]*quad[3,0]
        SQ[2,0] = quad[0,0]
        SQ[0,1] = quad[1,1] - quad[0,1] + SQ[0,2]*quad[1,1]
        SQ[1,1] = quad[3,1] - quad[0,1] + SQ[1,2]*quad[3,1]
        SQ[2,1] = quad[0,1]
        return SQ

#...............................................................................
def mapQuadToSquare( quad ):
    return np.linalg.inv( mapSquareToQuad( quad ))

def mapQuadToQuad( a, b ):
    return np.dot( mapQuadToSquare(a), mapSquareToQuad(b) )

def perstrans( X, t ):
    """ perspective transform X Nx2, t 3x3:
        [x0 y0 1] t = [a0 b0 w0] -> [a0/w0 b0/w0]
        [x1 y1 1] t = [a1 b1 w1] -> [a1/w1 b1/w1]
        ...
    """
    x1 = np.vstack(( X.T, np.ones(len(X)) ))
    y = np.dot( t.T, x1 )
    return (y[:-1] / y[-1]) .T

#...............................................................................
if __name__ == "__main__":
    np.set_printoptions( 2, threshold=100, suppress=True )  # .2f

    sq = np.array([[0,0], [1,0], [1,1], [0,1]])
    quad = np.array([[171, 72], [331, 93], [333, 188], [177, 210]])
    print "quad:", quad
    print "square to quad:", perstrans( sq, mapSquareToQuad(quad) )
    print "quad to square:", perstrans( quad, mapQuadToSquare(quad) )

    dw, dh = 300, 250
    rect = np.array([[0, 0], [dw, 0], [dw, dh], [0, dh]])
    quadquad = mapQuadToQuad( quad, rect )
    print "quad to quad transform:", quadquad
    print "quad to rect:", perstrans( quad, quadquad )
"""
quad: [[171  72]
 [331  93]
 [333 188]
 [177 210]]
square to quad: [[ 171.   72.]
 [ 331.   93.]
 [ 333.  188.]
 [ 177.  210.]]
quad to square: [[-0.  0.]
 [ 1.  0.]
 [ 1.  1.]
 [ 0.  1.]]
quad to quad transform: [[   1.29   -0.23   -0.  ]
 [  -0.06    1.79   -0.  ]
 [-217.24  -88.54    1.34]]
quad to rect: [[   0.    0.]
 [ 300.    0.]
 [ 300.  250.]
 [   0.  250.]]
"""
于 2010-06-08T17:26:37.513 回答
0

我认为您需要的是可以使用矩阵数学来完成的仿射变换。

于 2010-06-07T19:22:16.317 回答
0

而在现代,在 python 中使用 cv2。

import cv2
import numpy as np

source_image = cv2.imread('french fries in Europe.jpeg')

source_corners = np.array([(171, 72), (331, 93), (333, 188), (177, 210)])

width, height = 400, 250
target_corners = np.array([(0, 0), (width, 0), (width, height), (0, height)])

# Get matrix H that maps source_corners to target_corners
H, _ = cv2.findHomography(source_corners, target_corners, params=None)

# Apply matrix H to source image.
transformed_image = cv2.warpPerspective(
    source_image, H, (source_image.shape[1], source_image.shape[0]))

cv2.imwrite('tranformed_image.jpg', transformed_image)
于 2021-04-03T15:38:59.573 回答