Opencv fillPoly()不适用于灰度(1通道)图像

时间:2017-09-12 18:11:27

标签: python python-2.7 opencv image-processing opencv3.0

所以我使用opencv来修改单通道图像上的像素值。 我使用

创建空白图像
curr = np.zeros((660,512, 1))

然后执行代码:

for r in regions:
    cv2.fillPoly(curr, r, [190])

每个区域看起来像:

[[[363 588]
  [304 593]
  [323 652]
  [377 654]]]

我知道代码至少在某种程度上有效,因为当我使用imshow()时,区域会根据需要填充。但是,我尝试重新访问修改后的像素值,得到[0.]我尝试将整个img ti写成一个临时文件,如下所示:

for elt in curr:
    f2.write(str(elt) + '\n')

但是,该文件看起来像

 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]
 [ 0.]

我哪里错了?为什么我不能重新访问我写给图像的190年代?

2 个答案:

答案 0 :(得分:1)

工作得很好。

curr = np.zeros((660,512, 1),dtype = np.uint8)

regions = np.array([[[363,588],[304,593],[323,652],[377,654]]])

for r in regions:
    cv2.fillPoly(curr, [regions[0]], (190))

# find minimum value, maximum value and their location index in the image
minVal,maxVal,minLoc,maxLoc = cv2.minMaxLoc(curr)

print(maxVal, maxLoc)

答案 1 :(得分:1)

也许你在代码中的某处覆盖或重新初始化图像(curr),这里是使用oHbox1保存文件的代码。

oHbox2

OpenCV窗口: enter image description here

输出图片: enter image description here