所以我使用 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 窗口:
输出图像:
关于python - Opencv fillPoly()不适用于灰度(1 channel )图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46182975/