Home > Software design >  How could i improve my opencv program to detect only the crosswalk?
How could i improve my opencv program to detect only the crosswalk?

Time:04-26

I would like to detect the pedestrian crossing in the image below, and fill it with red color, but the program detect other things too. Here is my code:

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)

CodePudding user response:

Here is one way to do that in Python/OpenCV.

  • Read the input
  • Threshold on the white/gray sidewalk stripes
  • Apply morphology open and close
  • Get external contours
  • Filter contours on area and keep good contours
  • Draw good contours on input
  • Combine contours
  • Compute convex hull of combined contours
  • Draw convex hull on input
  • Save results

Input:

enter image description here

import cv2
import numpy as np

# read image
img = cv2.imread('walkway.jpg')

# threshold on white/gray sidewalk stripes
lower = (100,130,130)
upper = (180,200,200)
thresh = cv2.inRange(img, lower, upper)


# apply morphology close to fill interior regions in mask
kernel = np.ones((3,3), np.uint8)
morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
kernel = np.ones((5,5), np.uint8)
morph = cv2.morphologyEx(morph, cv2.MORPH_CLOSE, kernel)

# get contours
cntrs = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]

# filter on area
contours = img.copy()
good_contours = []
for c in cntrs:
    area = cv2.contourArea(c)
    if area > 200:
        cv2.drawContours(contours, [c], -1, (0,0,255), 1)
        good_contours.append(c)

# combine good contours
contours_combined = np.vstack(good_contours)

# get convex hull
result = img.copy()
hull = cv2.convexHull(contours_combined)
cv2.polylines(result, [hull], True, (0,0,255), 2)

# write result to disk
cv2.imwrite("walkway_thresh.jpg", thresh)
cv2.imwrite("walkway_morph.jpg", morph)
cv2.imwrite("walkway_contours.jpg", contours)
cv2.imwrite("walkway_result.jpg", result)

# display it
cv2.imshow("THRESH", thresh)
cv2.imshow("MORPH", morph)
cv2.imshow("CONTOURS", contours)
cv2.imshow("RESULT", result)
cv2.waitKey(0)

Threshold image:

enter image description here

Morphology image:

enter image description here

Contour image:

enter image description here

Result:

enter image description here

  • Related