python-3.x - np.where的不同类型的结果。x,y在两个条件下互换。我想念什么?

标签 python-3.x opencv opencv-python

我正在尝试初始化两个数组图像和一个。
在图像中,我用颜色值255填充轮廓
在轮廓cnt中,其顺序为x,y。
在数组中,我为a [2] [0]分配值1,其中2是x轴点,0是y轴点。
但是结果是我在y数组,x数组中得到图像的顺序,但是在a中它是x数组,y数组。
我在哪里犯错?

import cv2
import numpy as np
image = np.zeros((700,700),dtype=np.int32)
cnt = np.array([[340, 157], [695, 157], [695, 309], [340, 309], [340, 157]])

image = cv2.fillPoly(image,[cnt],color=255)

a = np.zeros((10,10),dtype=np.int32)
a [2][0] = 1

print(np.where(a>0)) #output_1
print(np.where(image>0)) #output_2
output_1 :
(数组([2]),数组([0]))
output_2 :
(array([157,157,157,...,309,309,309]),array([340,341,342,...,693,694,695]))

最佳答案

代码没有错误。我已经用numpy slice重写了它,以填充数组的某些部分。

import cv2
import numpy as np
image = np.zeros((700,700),dtype=np.int32)
print(image.shape)
# cnt = np.array([[340, 157], [695, 157], [695, 309], [340, 309], [340, 157]])
image[157:309, 340:695] = 255

# image = cv2.fillPoly(image,[cnt],color=255)
print(image.shape)

a = np.zeros((10,10),dtype=np.int32)
a [2, 0:5] = 1

print(np.where(a>0)) #output_1
print(np.where(image==255)) #output_2
(array([2], dtype=int64), array([0], dtype=int64))
(array([157, 157, 157, ..., 308, 308, 308], dtype=int64), array([340, 341, 342, ..., 692, 693, 694], dtype=int64))
如果您看这个,image[157:309, 340:695] = 255沿x轴的 slice 由列描述,而y轴的 slice 由行描述。
您的代码的cnt部分描述了157-309之间的行范围和340-695之间的列范围,这就是输出的原因。

关于python-3.x - np.where的不同类型的结果。x,y在两个条件下互换。我想念什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63285166/

相关文章:

Python Multiprocessing Pool.map 导致 __new__ 错误

python - 来自图像 : OCR-Python 的验证码中断或文本阅读器

android - 使用 OpenCV 创建人脸锁屏

computer-vision - 使用像素距离计算现实世界的距离

python - 频域上的同态滤波(Python 和 OpenCV)

python-3.x - cv2 imshow 有时显示黑色图像

python - 我可以在创建字典时引用字典值吗?

Python 3 绝对导入不起作用

Python 在列表中检查字典列表

opencv - 了解霍夫变换