gpt4 book ai didi

python - np.rot90() 损坏 opencv 图像

转载 作者:太空狗 更新时间:2023-10-29 21:08:42 38 4
gpt4 key购买 nike

当尝试将横向图像旋转为纵向时,应用旋转后,我无法在图像上绘图。

img1 = cv2.imread('a.jpg')
cv2.circle(img1, tuple([10,10]),radius = 3, color = (255,0,0))

工作正常。

然后我试试:

img2 = np.rot90(img1,3)
cv2.circle(img2, tuple([10,10]),radius = 3, color = (255,0,0))

我得到了错误:

TypeError: Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)

查看 type(img2)img2.dtype 它似乎与 img1 相同。尺寸看起来也不错(前两个尺寸翻转,第三个保持“3”)

顺便说一句:这似乎有效。 (为什么?):

img2 = np.rot90(img1,3)
img3 = img2.copy()
cv2.circle(img3, tuple([10,10]),radius = 3, color = (255,0,0))

最佳答案

我遇到了同样的问题,但一直没有搞清楚。我使用的解决方法是使用 OpenCV 进行图像旋转/翻转,例如:

# flip image vertically
img = cv2.flip(img, 0)

# flip image horizontally
img = cv2.flip(img, 1)

# transpose image
img = cv2.transpose(img)

请注意,旋转等同于进行转置和翻转。

关于python - np.rot90() 损坏 opencv 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20843544/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com