如何使用OpenCV的remap函数?

2024-09-27 22:19:13 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是remap()最简单的测试用例:

import cv2
import numpy as np
inimg = np.arange(2*2).reshape(2,2).astype(np.float32)
inmap = np.array([[0,0],[0,1],[1,0],[1,1]]).astype(np.float32)
outmap = np.array([[10,10],[10,20],[20,10],[20,20]]).astype(np.float32)
outimg = cv2.remap(inimg,inmap,outmap,cv2.INTER_LINEAR)
print "inimg:",inimg
print "inmap:",inmap
print "outmap:",outmap
print "outimg:", outimg

结果如下:

inimg: [[ 0.  1.]
 [ 2.  3.]]
inmap: [[ 0.  0.]
 [ 0.  1.]
 [ 1.  0.]
 [ 1.  1.]]
outmap: [[ 10.  10.]
 [ 10.  20.]
 [ 20.  10.]
 [ 20.  20.]]
outimg: [[ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]]

如您所见,outimg产生0,0,而且它的形状甚至不正确。我希望是20x20或10x10图像,插值范围为0到3。

我看过所有的文件。它和everyone on SO状态下,您输入一个数组(一个映射)开始点,一个映射结束点,然后remap()将把img中的所有值放入它们的新位置,插入任何空格。我这么做了,但没用。为什么?大多数例子是C++。是Python破的吗?


Tags: importnumpyasnp测试用例arraycv2print
1条回答
网友
1楼 · 发布于 2024-09-27 22:19:13

这只是对文件的一个简单的误解,我不怪你——我也花了几次摸索才明白。文档很清楚,但是这个函数可能没有按您预期的方式工作;事实上,它在与我最初预期相反的方向工作。

remap()没有做的是获取源图像的坐标,变换点,然后插值。remap()所做的是,对于目的地图像中的每个像素,查找它来自源图像中的位置,然后分配一个插值值。它需要这样工作,因为为了插值,它需要查看每个像素处源图像周围的值。让我扩展一下(可能会重复一下,但不要误会)。

^{} docs

map1 – The first map of either (x,y) points or just x values having the type CV_16SC2 , CV_32FC1 , or CV_32FC2 . See convertMaps() for details on converting a floating point representation to fixed-point for speed.

map2 – The second map of y values having the type CV_16UC1 , CV_32FC1 , or none (empty map if map1 is (x,y) points), respectively.

这里关于map1的“Thefirstmap of…”的措辞有些误导。记住,这些是图像从映射的坐标……这些点从map_x(x, y), map_y(x, y)src映射,然后放在x, ydst中。它们应该和你想把它们扭曲成的图像形状一样。请注意文档中显示的公式:

dst(x,y) =  src(map_x(x,y),map_y(x,y))

这里map_x(x, y)x, y给定的行和列上查找map_x。然后在这些点对图像进行求值。它查找srcx, y的映射坐标,然后将该值赋给dst中的x, y。如果你盯着它看够久,它就开始有意义了。在新目标图像中的像素(0, 0)处,我查看map_xmap_y,它们告诉我源图像中相应像素的位置,然后通过查看源图像中的接近值,可以在目标图像中的(0, 0)处分配插值。这就是remap()以这种方式工作的基本原因;它需要知道像素从哪里来,以便它可以看到要插值的相邻像素。

做作的小例子

img = np.uint8(np.random.rand(8, 8)*255)
#array([[230,  45, 153, 233, 172, 153,  46,  29],
#       [172, 209, 186,  30, 197,  30, 251, 200],
#       [175, 253, 207,  71, 252,  60, 155, 124],
#       [114, 154, 121, 153, 159, 224, 146,  61],
#       [  6, 251, 253, 123, 200, 230,  36,  85],
#       [ 10, 215,  38,   5, 119,  87,   8, 249],
#       [  2,   2, 242, 119, 114,  98, 182, 219],
#       [168,  91, 224,  73, 159,  55, 254, 214]], dtype=uint8)

map_y = np.array([[0, 1], [2, 3]], dtype=np.float32)
map_x = np.array([[5, 6], [7, 10]], dtype=np.float32)
mapped_img = cv2.remap(img, map_x, map_y, cv2.INTER_LINEAR)
#array([[153, 251],
#       [124,   0]], dtype=uint8)

那这里发生了什么?记住,这些是img的索引,将映射到它们所在的行和列。在这种情况下,最容易检查矩阵:

map_y
=====
0  1
2  3

map_x
=====
5  6
7  10

因此(0,0)处的目标图像与map_y(0, 0), map_x(0, 0) = 0, 5处的源图像具有相同的值,第0行和第5列处的源图像是153。请注意,在目标图像中mapped_img[0, 0] = 153。这里没有插值,因为我的地图坐标是精确的整数。此外,我还包含了一个越界索引(map_x[1, 1] = 10,它大于图像宽度),注意,当它越界时,它只是被赋值为0

完整用例示例

下面是一个完整的代码示例,使用地面真值单应,手动扭曲像素位置,然后使用remap()从转换点映射图像。注意,这里我的单应式将true_dst转换为src。因此,我建立了一个任意多个点的集合,然后通过用单应变换计算这些点在源图像中的位置。然后使用remap()查找源图像中的这些点,并将它们映射到目标图像中。

import numpy as np
import cv2

# read images
true_dst = cv2.imread("img1.png")
src = cv2.imread("img2.png")

# ground truth homography from true_dst to src
H = np.array([
    [8.7976964e-01,   3.1245438e-01,  -3.9430589e+01],
    [-1.8389418e-01,   9.3847198e-01,   1.5315784e+02],
    [1.9641425e-04,  -1.6015275e-05,   1.0000000e+00]])

# create indices of the destination image and linearize them
h, w = true_dst.shape[:2]
indy, indx = np.indices((h, w), dtype=np.float32)
lin_homg_ind = np.array([indx.ravel(), indy.ravel(), np.ones_like(indx).ravel()])

# warp the coordinates of src to those of true_dst
map_ind = H.dot(lin_homg_ind)
map_x, map_y = map_ind[:-1]/map_ind[-1]  # ensure homogeneity
map_x = map_x.reshape(h, w).astype(np.float32)
map_y = map_y.reshape(h, w).astype(np.float32)

# remap!
dst = cv2.remap(src, map_x, map_y, cv2.INTER_LINEAR)
blended = cv2.addWeighted(true_dst, 0.5, dst, 0.5, 0)
cv2.imshow('blended.png', blended)
cv2.waitKey()

Remap for warping

来自Visual Geometry Group at Oxford的图像和基本真实同音字。

相关问题 更多 >

    热门问题