图像的几何扭曲在python中 [英] Geometric warp of image in python

查看:1479
本文介绍了图像的几何扭曲在python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用python对图像执行几何变换,以拉直或纠正沿给定曲线的图像。似乎scikit-image ProjectiveTransform() warp()对此非常好,但文档很稀疏。我按照文档这里,但我无法让它在样本案例中正常工作。

I would like to use python to perform a geometric transform over an image, to 'straighten' or rectify an image along a given curve. It seems that scikit-image ProjectiveTransform() and warp() are very good for this, but the documentation is sparse. I followed the documentation here, but I couldn't get it to work properly for a sample case.

这是一个例子:我将创建一个有两个同心圆的图像,目标是纠正一个这些圆的四分之一,使得得到的图像是两条平行线。以下是示例数据:

Here's an example: I'll create an image with two concentric circles, and the goal is to rectify one quarter of these circles, so that the resulting image are two parallel lines. Here is the sample data:

import numpy as np
a = np.zeros((500, 500))

# create two concentric circles with a thickness of a few pixels:
for i in range(500):
    for j in range(500):
        r = np.sqrt((i - 250)**2 + (j - 250)**2) 
        if r > 50 and r < 52:
            a[i, j] = 10
        if r > 100 and r < 102:
            a[i, j] = 10
# now create the coordinates of the control points in the original image:
(x0, y0) = (250, 250)
r = 30   # inner circle
x = np.linspace(250 - r, 250, 50)
y = np.sqrt(r ** 2 - (x - x0) ** 2) + x0
r2 = 120   # outer circle
x2 = np.linspace(250 - r2, 250, 50)
y2 = np.sqrt(r2 ** 2 - (x2 - x0) ** 2) + x0
dst = np.concatenate((np.array([x, y]).T, np.array([x2, y2]).T))

这可以绘制,例如:

imshow(a, cmap='gist_gray_r')
plot(x, y, 'r.')
plot(x2, y2, 'r.')

所以我的目标是纠正红色控制点给出的象限中的图像。 (在这种情况下,这与笛卡尔到极坐标变换相同。)使用文档示例中的scikit图像,我已经完成了:

So my goal is to rectify the image in the quadrant given by the red control points. (In this case, this is the same as a Cartesian to polar transformation.) Using scikit image from the documentation example, I've done:

# create corresponding coordinates for control points in final image:
xi = np.linspace(0, 100, 50)
yi = np.zeros(50)
xi2 = xi
yi2 = yi + (r2 - r)
src = np.concatenate((np.array([xi, yi]).T, np.array([xi2, yi2]).T))

# transform image
from skimage import transform, data
tform3 = transform.ProjectiveTransform()
tform3.estimate(src, dst)
warped = transform.warp(a, tform3)

我原以为翘曲图像显示两条平行线,但我得到:

I was expecting this warped image to show two parallel lines, but instead I get:

我在这里做错了什么?

What am I doing wrong here?

请注意,虽然在这种情况下它是笛卡尔到极坐标变换,但在最常见的情况下,我正在寻找从某个任意曲线的变换。如果有人知道使用其他包装的更好方法,请告诉我。我可以通过使用 ndimage.map_coordinates 来解决这个问题,但是我正在寻找更优雅的东西。

Note that while in this case it is a Cartesian to polar transform, in the most general case I'm looking for a transformation from some arbitrary curve. If someone knows of a better way using some other package, please let me know. I can solve this problem by using ndimage.map_coordinates for a bunch of radial lines, but was looking for something more elegant.

推荐答案

A ProjectiveTransform 是线性变换,无法与变形方案匹配。可能有更好的选项,但对于任意曲线,您可以使用 PiecewiseAffineTransform ,它将通过细分线性变换来匹配您抛出的任何内容。如果您只是更改代码中转换的名称,这就是我得到的输出:

A ProjectiveTransform is a linear transformation, and cannot match your deformation scheme. There may be better options, but for arbitrary curves you can make it work with a PiecewiseAffineTransform, which will match anything you throw at it by tessellating linear transformations. If you simply change the name of the transform in your code, this is the output I get:

所以你可能需要稍微调整它以获得你想要的东西,但是至少它产生了你在转换定义良好的区域中期望的两条平行线。

So you'll probably need to tweak it a little bit to get what you are after, but at least it produces the two parallel lines you were expecting in the area where your transformation is well defined.

这篇关于图像的几何扭曲在python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆