gpt4 book ai didi

python - 无法在不创建副本的情况下在图像中画线。(问题仅存在于 .png 图像中)

转载 作者:行者123 更新时间:2023-12-02 16:34:19 25 4
gpt4 key购买 nike

下面的代码不会在 png 图像上画任何线

imgPath = "./images/dummy.png"

img = cv2.imread(imgPath,cv2.IMREAD_UNCHANGED)
imgBGR = img[:,:,:3]
imgMask = img[:,:,3]

cv2.line(imgBGR, (200,100), (250,100), (0,100,255), thickness=9, lineType=cv2.LINE_AA)
plt.imshow(imgBGR[:,:,::-1])

关于创建 BGR channel 的副本并使用它来绘制线条作品。
imgPath = "./images/dummy.png"

img = cv2.imread(imgPath,cv2.IMREAD_UNCHANGED)
imgBGR = img[:,:,:3]
imgMask = img[:,:,3]

imgBGRCopy = imgBGR.copy()

cv2.line(imgBGRCopy , (200,100), (250,100), (0,100,255), thickness=9, lineType=cv2.LINE_AA)
plt.imshow(imgBGRCopy [:,:,::-1])

请解释为什么?

最佳答案

由于神秘的原因,OpenCV 无法在 NumPy 切片上绘图。

例如,请参阅以下帖子:Why cv2.line can't draw on 1 channel numpy array slice inplace?

该错误与 PNG 与 JPEG 或 BGR 与 BGRA 无关。
该错误是因为您试图在切片上绘制。

例如,以下代码也失败:

imgBGR = cv2.imread('test.jpg', cv2.IMREAD_UNCHANGED)
imgB = imgBGR[:,:,0] # Get a slice of imgBGR
cv2.circle(imgB, (100, 100), 10, 5)

作为替代方案,您可以在 BGRA 图像上绘图。

根据 Drawing Functions文档:

Note The functions do not support alpha-transparency when the target image is 4-channel. In this case, the color[3] is simply copied to the repainted pixels. Thus, if you want to paint semi-transparent shapes, you can paint them in a separate buffer and then blend it with the main image.



以下代码正在工作:
img = cv2.imread(imgPath,cv2.IMREAD_UNCHANGED) 
cv2.line(img, (200,100), (250,100), (0,100,255), thickness=9, lineType=cv2.LINE_AA)

imgBGR = img[:,:,:3]
plt.imshow(imgBGR[:,:,::-1])

笔记:
Python 和 OpenCV 是“开源”的,所以理论上我们可以按照源代码来揭开问题的神秘面纱。

关于python - 无法在不创建副本的情况下在图像中画线。(问题仅存在于 .png 图像中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61215724/

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