如何使用 Canny 边缘检测 Opencv 填充图像中的特定部分

How do I fill specific parts in the image with Canny Edge Detection Open CV

我只想填充中间的人行横道,而不是图像的所有边缘。我该怎么做?

import numpy as np
import cv2 as cv2

%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rc('axes', labelsize=14)
mpl.rc('xtick', labelsize=12)
mpl.rc('ytick', labelsize=12)

#Routine to fix 
def fixColor(img):
return(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
img = cv2.imread("walk.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
plt.imshow(fixColor(blurred))
canny = cv2.Canny(blurred, 30, 300)
plt.imshow(fixColor(canny))

(cnts, _) = cv2.findContours(canny.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
coins = image.copy()
cv2.drawContours(coins, cnts, -1, (255, 0, 0), 2)
plt.imshow(fixColor(coins))

结果:

我想要的:

原图:

我使用了您分享的代码并执行了额外的膨胀操作来加厚 Canny 边缘输出

附加代码:

kernel_ellipse = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
dilate = cv2.dilate(canny, kernel_ellipse, iterations=1)

(cnts, _) = cv2.findContours(dilate.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

coins = image.copy()
for c in cnts:
    area = cv2.contourArea(c)
    if area > 1000:
         cv2.drawContours(coins , [c], -1, (0, 0, 255), -1)

结果:

注意: 为了用您选择的颜色填充轮廓,thickness 中的参数 cv2.drawContours() 必须是 -1.