首页 > 解决方案 > 是否对填充功能进行了修改以使其行为准确

问题描述

我正在尝试使用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)

在此处输入图像描述

在此处输入图像描述

标签: python

解决方案


推荐阅读