关闭。这个问题需要details or clarity .它目前不接受答案。
想改进这个问题?通过 editing this post 添加详细信息并澄清问题.
2年前关闭。
Improve this question
如何使用 OpenCV Matrix 中的 Eigen 将图像旋转 90 度,然后将旋转后的图像转换回 C++ 中的 OpenCV 矩阵。 rotate
OpenCV 的功能需要时间,我想尽快完成。我试过使用 Numpy rot90
Python 中的函数,与 OpenCV 相比非常快 rotate
C++ 中的函数。不幸的是,Numpy 不适用于 C++。我读过 C++ 中还有其他库,如 Eigen 和 Armadillo,它们可以快速执行这些矩阵运算。这就是我想使用 Eigen 旋转图像并检查时间的原因。
我在 Windows 10 的 i5 机器上测试了 Visual Studio 2019 中的函数。numpy rot90
Python 中的函数比 OpenCV rotate
快大约 10 倍C++ 中的函数。
最佳答案
我猜这个函数 warpAffine
是比较快的。至少你应该比较检查。
这里有一个例子:
https://docs.opencv.org/master/dd/d52/tutorial_js_geometric_transformations.html .
cuda 提供相同类型的功能:
https://docs.opencv.org/master/db/d29/group__cudawarping.html
编辑:warpAffine
在 OpenCV 中实际上可以使用 ippiWarpAffine*
Intel Performance Primitives 库中的函数。这可能是可以获得的最快性能。如果您可以在具有 nvidia gpu 的平台上运行您的软件,cuda 版本预计会更快。性能取决于您使用的数据类型。如果您可以使用 8 位未签名图像,则速度会快得多。
编辑2:
在评论说 warpAffine 速度较慢后,我进行了一些测试,有时它可能会更快。但是,与 numpy 的旋转相比,没有什么可比的,即使是 cv2.flip 或 cv2.transpose 也慢得多。因此,我建议查看 this recommendation on Intel's developer zone也就是使用 ippiRotate 和 ippiMirror 函数进行 90 次旋转。如果您真的有兴趣从 Intel cpu 中获得最佳性能,那将是我的猜测。还要注意多线程,一些功能可以在 IPP 中多线程。最后,这取决于您是否寻找一种解决方案来旋转单个大图像或多个大图像,数据类型, channel 数。至少使用 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)
我希望这有帮助!
关于python - 在 C++ 中使用来自 OpenCV 矩阵的特征将图像旋转 90 度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59612684/