在C++中使用OpenCV矩阵中的特征将图像旋转90度



如何使用 OpenCV 矩阵中的特征将图像旋转 90 度,然后在 C++ 中将旋转的图像转换回 OpenCV 矩阵。OpenCV的rotate功能需要时间,我想尽快完成。我尝试在Python中使用Numpyrot90函数,与C++中的OpenCVrotate函数相比,它非常快。不幸的是,Numpy 不适用于C++。我读过C++还有其他库,如 Eigen 和 Armadillo,可以快速完成这些矩阵运算。这就是我想使用特征旋转图像并检查计时的原因。

我在Windows 10的i5机器上测试了Visual Studio 2019中的功能。Python 中的 numpyrot90函数大约比 C++ 中的 OpenCVrotate函数快 10 倍。

我想函数warpAffine更快。至少你应该比较检查。
这里有一个例子:
https://docs.opencv.org/master/dd/d52/tutorial_js_geometric_transformations.html。

cuda
:提供了相同类型的函数 https://docs.opencv.org/master/db/d29/group__cudawarping.html

编辑:
OpenCV中的warpAffine实际上可以使用英特尔性能基元库中的ippiWarpAffine*函数。这可能是可以获得的最快性能。如果您可以在带有 nvidia GPU 的平台上运行软件,预计 cuda 版本会更快。性能取决于您使用的数据类型。如果你可以使用8位无符号图像,你可以更快。

编辑2: 在评论说warpAffine较慢之后,我进行了一些测试,有时可能会更快。然而,当与 numpy 的旋转相比时,没有什么可比的,即使是 cv2.flip 或 cv2.transpose 也慢得多。因此,我建议在英特尔的开发人员专区查看此建议,即使用 ippiRotate 和 ippiMirror 函数执行 90 次旋转。如果您真的有兴趣从英特尔 CPU 获得最佳性能,那将是我的猜测。还要注意多线程,有些函数在IPP中可以多线程。最后,这取决于您是否正在寻找一种解决方案来旋转单个大图像或多个大图像,数据类型,通道数。使用 IPP,至少您可以为您的数据类型使用最佳功能。
接下来在python中进行了一些试验,以与numpy的rot90函数进行比较。当然,结果可以随参数而变化,但与 numpy 仍然存在很大差异。从我的试验中也看不出cv2.rotate 如此之快。

100x np.rot90 time       : 0.001626729965209961
100x cv2.rotate time     : 0.21501994132995605
100x cv2.transpose time  : 0.18512678146362305
100x cv2.remap time      : 0.6473801136016846
100x cv2.warpAffine time : 0.11946868896484375
import cv2
import numpy as np
import time
img = np.random.randint(0, 255, (1000, 1000, 3)).astype(np.uint8)
##################################
start = time.time()
for i in range(100):
rotated = np.rot90(img)
end = time.time()
print("100x np.rot90 time       :", end - start)
##################################
start = time.time()
for i in range(100):
rotated = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
end = time.time()
print("100x cv2.rotate time     :", end - start)
##################################
start = time.time()
for i in range(100):
rotated = cv2.transpose(img, 1)
end = time.time()
print("100x cv2.transpose time  :", end - start)
##################################
mapx, mapy = np.meshgrid(np.arange(0, img.shape[1]), np.arange(0, img.shape[0]))
mapx = mapx.transpose()
mapy = mapy.transpose()
start = time.time()
for i in range(100):
rotated = cv2.remap(img, mapx.astype(np.float32), mapy.astype(np.float32), cv2.INTER_NEAREST)
end = time.time()
print("100x cv2.remap time      :", end - start)
##################################
rows = img.shape[0]
cols = img.shape[1]
M = cv2.getRotationMatrix2D((rows / 2, cols / 2), 90, 1)
M[0, 2] = 0
M[1, 2] = cols
start = time.time()
for i in range(100):
rotated = cv2.warpAffine(img, M, (rows, cols), flags=cv2.INTER_NEAREST)
end = time.time()
print("100x cv2.warpAffine time :", end - start)

我希望这有帮助!

最新更新