Amo*_*kar 5 python opencv image-processing
我坚持在本教程中将ROI粘贴到同一图像的另一个区域.当我尝试类似的东西时,Python会出现一个值错误:
img = cv2.imread(path, -1)
eye = img[349:307, 410:383]
img[30:180, 91:256] = eye
Run Code Online (Sandbox Code Playgroud)
Exeption:
Traceback (most recent call last):
File "test.py", line 13, in <module>
img[30:180, 91:256] = eye
ValueError: could not broadcast input array from shape (0,0,3) into shape (150,165,3)
Run Code Online (Sandbox Code Playgroud)
这可能是一个非常新的问题,但我无法通过谷歌搜索得出答案.这样做有其他numpy方法吗?
编辑:同样在教程中它没有指定如何输入坐标.例如:我可以输入我想要的区域的坐标:eye = img[x1:y1, x2:y2]
或img[x1:x2, y1:y2]
.这让我很困惑.实际上我试图通过鼠标回调方法获取这些坐标,该方法打印了鼠标点击的位置.因此,坐标肯定来自图像内部.
切片[349:307, 410:383]
返回一个空数组eye
,该数组无法分配给不同形状的数组视图.
例如:
In [8]: import cv2
...: fn=r'D:\Documents\Desktop\1.jpg'
...: img=cv2.imread(fn, -1)
...: roi=img[200:400, 200:300]
In [9]: roi.shape
Out[9]: (200, 100, 3)
In [10]: img2=img.copy()
In [11]: img2[:roi.shape[0], :roi.shape[1]]=roi
In [12]: cv2.imshow('img', img)
...: cv2.imshow('roi', roi)
...: cv2.imshow('img2', img2)
...: cv2.waitKey(0)
...: cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
结果:
img&roi:
IMG2:
请注意,即使roi
不是空数组,具有不匹配形状的赋值也会引发错误:
In [13]: img2[:100, :100]=roi
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-13-85de95cf3ded> in <module>()
----> 1 img2[:100, :100]=roi
ValueError: could not broadcast input array from shape (200,100,3) into shape (100,100,3)
Run Code Online (Sandbox Code Playgroud)
我猜你的形象有问题。我们看一下返回的错误
ValueError: could not broadcast input array from shape (0,0,3) into shape (150,165,3)
Run Code Online (Sandbox Code Playgroud)
因此,eye 的尺寸似乎为 (0,0,3),img 的尺寸为 (150,165,3)。3 对应 RGB,即 3 个不同的颜色通道。所以你的原始图像是 150x165。但您尝试选择 img[349:307, 410:383] 处的区域。我怀疑由于您指定的区域位于图像之外,因此它没有选择任何尺寸(0,0,3)。
尝试导入pdb;pdb.set_trace() 在初始化 eye 的第二行之后。这将弹出一个交互式 python 终端,您可以在其中看到发生了什么。尝试看看 img 的尺寸是多少,以及它是否真的是您想要的。也许您下载的图像比导致错误的示例小。
查看类似问题的第一个答案。您获取 roi 的方法看起来是正确的,因此请尝试将坐标调整到适合的较小区域。
归档时间: |
|
查看次数: |
8987 次 |
最近记录: |