zl程序教程

您现在的位置是:首页 >  后端

当前栏目

opencv remap matlab,如何使用OpenCV的remap函数?

OpencvMATLAB 使用 如何 函数 remap
2023-06-13 09:15:11 时间

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

remap()没有做的是获取源图像的坐标,变换点,然后插值。remap()所做的是,对于目的地图像中的每个像素,查找它来自源图像中的位置,然后分配一个插值值。它需要这样工作,因为为了插值,它需要查看每个像素处源图像周围的值。让我扩展一下(可能会重复一下,但不要误会)。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, y的dst中。它们应该和你想把它们扭曲成的图像形状一样。请注意文档中显示的公式:dst(x,y) = src(map_x(x,y),map_y(x,y))

这里map_x(x, y)在x, y给定的行和列上查找map_x。然后在这些点对图像进行求值。它查找src中x, y的映射坐标,然后将该值赋给dst中的x, y。如果你盯着它看够久,它就开始有意义了。在新目标图像中的像素(0, 0)处,我查看map_x和map_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()

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/230612.html原文链接:https://javaforall.cn