【问题标题】:Opencv fillPoly() not working for greyscale (1-channel) imagesOpencv fillPoly()不适用于灰度(1通道)图像
【发布时间】:2018-02-21 07:19:40
【问题描述】:

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

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?

【问题讨论】:

    标签: python python-2.7 opencv image-processing opencv3.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)
    

    【讨论】:

      【解决方案2】:

      也许您在代码中的某处重写或重新初始化图像(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 窗口:

      输出图像:

      【讨论】:

      • 如我所说,在窗口中显示图像;它看起来很好。但我希望能够查看文件并找到最大值。作为替代方案,我尝试将图像以 png 格式写入文件,然后使用 imread 打开保存的文件。现在我得到 [190, 190, 190] 作为图像中的最大值。这正是我对灰度图像的期望吗?我想因为图像形状是 (660, 512, 1),访问单个像素会给我一个奇异值。但是访问 img[x][y] 总是返回一个 3 元素数组。
      猜你喜欢
      • 1970-01-01
      • 2017-01-23
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 2019-04-02
      相关资源
      最近更新 更多