繁体   English   中英

我的OpenCV重映射()有什么问题?

[英]What is wrong with my OpenCV remap()ing?

我从OpenCV中的Image转换中获取了答案https://stackoverflow.com/a/10374811/4828720 ,并尝试将其调整为我的图像。

我的源图片: 我的源图片

在其中,我确定了方格砖中心的像素坐标,如下图所示:

来源点

我的目标分辨率是784.我计算了像素的目标坐标。 我得到的代码是这样的:

import cv2
from scipy.interpolate import griddata
import numpy as np

source = np.array([
    [315, 15],
    [962, 18],
    [526, 213],
    [754, 215],
    [516, 434],
    [761, 433],
    [225, 701],
    [1036, 694],
], dtype=int)

destination = np.array([
     [14, 14],
     [770, 14],
     [238, 238],
     [546, 238],
     [238, 546],
     [546, 546],
     [14, 770],
     [770, 770]
], dtype=int)

source_image = cv2.imread('frames.png')

grid_x, grid_y = np.mgrid[0:783:784j, 0:783:784j]
grid_z = griddata(destination, source, (grid_x, grid_y), method='cubic')
map_x = np.append([], [ar[:,1] for ar in grid_z]).reshape(784,784)
map_y = np.append([], [ar[:,0] for ar in grid_z]).reshape(784,784)
map_x_32 = map_x.astype('float32')
map_y_32 = map_y.astype('float32')
warped_image = cv2.remap(source_image, map_x_32, map_y_32, cv2.INTER_CUBIC)
cv2.imwrite("/tmp/warped2.png", warped_image)

如果我运行它,没有任何源点最终到达预定的目的地,但我得到一个扭曲的混乱。 我在这里添加了目的地点:

我的结果

我哪里错了? 我注意到我的网格和地图数组的分布不如示例中那样好。 点数太少了吗? 我是否需要在常规网格中使用它们? 我只尝试使用外角的四个点而没有运气。

如果您只有8个点用于扭曲图像中没有真正的失真,我建议使用此处所述的透视变换。

您引用的链接会尝试消除导致非直线的其他扭曲,但图像中的所有线条都是直线。

代码看起来像:

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('image.png')
rows,cols,ch = img.shape

pts1 = np.float32([
    [315, 15],
    [962, 18],
    [225, 701],
    [1036, 694],
], dtype=int)

pts2 = np.float32([
     [14, 14],
     [770, 14],
     [14, 770],
     [770, 770]
], dtype=int)

M = cv2.getPerspectiveTransform(pts1,pts2)

dst = cv2.warpPerspective(img,M,(784,784))

plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()

在此输入图像描述

整个问题是我再次被numpy的行/列索引而不是x / y弄糊涂了。 #opencv IRC频道中有人指出了这一点。 我的源和目标数组必须切换其列:

source = np.array([
    [15, 315],
    [18, 962],
    [213, 526],
    [215, 754],
    [434, 516],
    [433, 761],
    [701, 225],
    [694, 1036],
], dtype=int)

destination = np.array([
     [14, 14],
     [14, 770],
     [238, 238],
     [238, 546],
     [546, 238],
     [546, 546],
     [770, 14],
     [770, 770]
], dtype=int)

然后它按预期工作(忽略丑陋的扭曲,这是一个简化的坐标列表来找到bug):

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM