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

Time:04-30

I only want to fill the middle crosswalk, not all the edges of the image. How can I do it?

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))

Result: enter image description here

Note: In order to fill the contour with a color of your choice, the thickness parameter within cv2.drawContours() must be -1.

  • Related