python - Opencv fillPoly()不适用于灰度(1 channel )图像

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

所以我使用 opencv 来修改 1 channel 图像上的像素值。
我使用创建空白图像

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.]

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

最佳答案

也许您在代码中的某处重写或重新初始化图像(curr),这是使用 cv2.imwrite 的代码保存文件。

curr = np.zeros((660,512, 1))
regions = np.random.uniform(1, 200, size=(1, 5, 2))
regions = regions.astype(np.int32, copy=False)
for r in regions:
    cv2.fillPoly(curr, [r], [190])

while(1):
    cv2.imshow('Terry Martin', curr)
    k= cv2.waitKey(1) & 0xFF
    if k == 27:
        cv2.imwrite('FillPloy.jpg', curr)
        break

cv2.destroyAllWindows()

OpenCV 窗口:
enter image description here

输出图像:
enter image description here

关于python - Opencv fillPoly()不适用于灰度(1 channel )图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46182975/

相关文章:

python - 如何读取具有多个 header block 的数据

java - 在 OpenCV Approxpoly 中检测三角形

python - 如何在不知道路径的情况下从另一个相对文件夹导入?

python - 我如何将 SMOTE 应用于多类文本数据

python - os.system 提示圆括号

python - 如何获取字符串中重复出现的字符的位置?

python - 如何在 Python 中检查另一个字典中是否存在值的键(如一个字典中所定义)?

c++ - 在 opencv 中显示文件中的视频

c++ - OpenCV 3 中未在此范围内声明消息 "‘CV_SHAPE_ELLIPSE’

python - 如何在Python中单独更新 turtle