我想检测下图中的人行横道,并用红色填充它,但程序也会检测其他东西。这是我的代码:
import cv2
import numpy as np
img = cv2.imread("zebra_lane.jpg")
cv2.imshow("kep" ,img)
imgContour=img.copy()
def getContours(img, imgContour):
contours, hierarchy = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for cnt in contours:
area=cv2.contourArea(cnt)
if area>500:
cv2.drawContours(imgContour, contours, -1, (0, 0, 255), 2)
# peri=cv2.arcLength(cnt,True)
# approx = cv2.approxPolyDP(cnt,0.02*peri,True)
# # print(len(approx))
# if len(approx)==4:
# x,y,w,h =cv2.boundingRect(approx)
# cv2.rectangle(imgContour,(x,y),(x+w,y+h), (0,0,255),1)
imgblur=cv2.GaussianBlur(img,(7,7),1)
imggray=cv2.cvtColor(imgblur,cv2.COLOR_BGR2GRAY)
imgcanny=cv2.Canny(imggray,150,90)
cv2.imshow("kep" ,imgcanny)
kernel=np.ones((1,1))
imgDil = cv2.dilate(imgcanny,kernel,iterations=1)
cv2.imshow("kep" ,imgDil)
getContours(imgDil,imgContour)
cv2.imshow("contour",imgContour)