我正在尝试初始化两个数组图像和一个。
在图像中,我用颜色值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/