简体   繁体   English

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

[英]Opencv fillPoly() not working for greyscale (1-channel) images

so I am using opencv to modify pixel values on a 1-channel image. 所以我正在使用opencv修改1通道图像上的像素值。 I create the blank image using 我使用创建空白图像

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

then execute the code: 然后执行代码:

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

where each region looks something like: 每个区域的外观如下:

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

I know that the code is at least somewhat working, because when I use imshow(), the regions are filled as desired. 我知道代码至少在某种程度上可以正常工作,因为当我使用imshow()时,区域将按需要填充。 However, I tried re-accessing the modified pixel values, and got [ 0.] I tried writing the whole img ti a temporary file as follows: 但是,我尝试重新访问修改后的像素值,并得到[0。]我尝试将整个img ti写入一个临时文件,如下所示:

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

However, the file just looks like 但是,文件看起来像

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

Where am I going wrong? 我要去哪里错了? Why can't I re-access the 190s I wrote to the image? 为什么我不能重新访问写在图像上的190年代?

Works just fine. 效果很好。

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)

maybe you are over-writing or re-initialising image(curr) somewhere in the code, here's the code using cv2.imwrite to save the file. 也许您正在代码中的某个位置覆盖或重新初始化图像(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 Window: OpenCV窗口: 在此处输入图片说明

Output image: 输出图像: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM