我猜函数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 也慢得多。因此,我建议查看this recommendation on Intel's developer zone,它使用 ippiRotate 和 ippiMirror 函数来执行 90 次旋转。如果您真的有兴趣从 Intel 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)
我希望这会有所帮助!