python - 是否对填充功能进行了修改以使其行为准确
问题描述
我正在尝试使用python开发一个函数,该函数在位于灰度图像中的边界内完成填充,我使用了递归解决方案,以便该函数在每个像素周围的4个方向上移动并填充黑色像素,但是我发现它只填充将点插入函数的中心列。我希望任何人都可以帮助我找出我的函数定义中的错误在哪里,以填充所有多边形。请找到我的代码和结果。提前谢谢了。
import cv2
import matplotlib.pyplot as plt
import numpy as np
im = cv2.imread('D:\My work\\try2.jpg')
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
pt = [int(h/2),int(w/2)]
print(gray[pt[0],pt[1]])
points = []
def filling(image,point): # Image must be grayscale image using 4 connectivity
p1 =[point[0]+1,point[1]]
p2 =[point[0]-1, point[1]]
p3 =[point[0],point[1]+1]
p4 =[point[0], point[1]-1]
if (image[p1[0],p1[1]] < 200):
image[p1[0],p1[1]] = 255
filling(image,p1)
elif (image[p2[0],p2[1]]<200):
image[p2[0],p2[1]]=255
filling(image,p2)
elif (image[p3[0],p3[1]]<200):
image[p3[0],p3[1]]=255
filling(image,p3)
elif (image[p4[0],p4[1]]<200):
image[p4[0],p4[1]] =255
filling(image,p4)
return image
gray2 = filling(gray,[int(h/2),int(w/2)])
cv2.imshow('Image1',gray2)
cv2.waitKey(0)
解决方案
推荐阅读
- c++ - 使用 SFML 绘制曲线会给出错误的形状
- c# - Bot Framework - 防止 GatewayTimeout 长时间操作
- python - 使用 Python 合并具有相同 KEY 的 JSON 对象
- java - 递归列出包中定义的所有枚举
- flutter - TabBar 未将另一个屏幕显示为全屏 - 颤动
- laravel - 如何将唯一属性设置为laravel中的字段
- sql-server - SQL Server 代理 - BCryptDecrypt 失败
- sql - 如何创建一个从 postgreSQL 中的表返回一组行的过程
- java - 如何仅在 . 如果它前面有双斜杠而不是?
- html - 我想使用 express js 将数据插入 mysql 数据库,但我无法将其插入以及将数据获取到终端