Ask Your Question
0

way to find Zebra crossing lines

asked 2020-02-14 00:14:54 -0600

updated 2020-02-14 03:17:10 -0600

supra56 gravatar image

Code:

import cv2
import numpy as np
image = cv2.imread('abbey_road.jpg',-1)
paper = cv2.resize(image,(500,500))
ret, thresh_gray = cv2.threshold(cv2.cvtColor(paper, cv2.COLOR_BGR2GRAY),
                        200, 255, cv2.THRESH_BINARY)
contours, hier = cv2.findContours(thresh_gray,  cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for c in contours:
    print(c)
    rect = cv2.minAreaRect(c)
    box = cv2.boxPoints(rect)
    # convert all coordinates floating point values to int
    box = np.int0(box)
    # draw a green 'nghien' rectangle
    cv2.drawContours(paper, [box], 0, (0, 255, 0),1)

cv2.imshow('paper', paper)

cv2.imwrite('paper.jpg',paper)
cv2.waitKey(0)

image description

I tried the above code for zebra detection,but it end up with detecting all thewhite boxes but not the whole are.Is there any way to solve this?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-03-04 12:57:33 -0600

supra56 gravatar image

I solved problem. There are no noises.

#!/usr/bin/python37
#Raspberry pi 3/3B+/4B, OpenCV 4.2.0, Thonny 3.7.3
#Date: 4h March, 2020

import cv2
import numpy as np

img = cv2.imread("zebra_lane.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray,
                           (15, 15), 6)

ret, thresh = cv2.threshold(blurred,
                            180, 255,
                            cv2.THRESH_BINARY)

contours, hier = cv2.findContours(thresh.copy(),
                                  cv2.RETR_TREE,
                                  cv2.CHAIN_APPROX_SIMPLE)


for c in contours:
    # if the contour is not sufficiently large, ignore it
    if  cv2.contourArea(c) < 2000:
        continue

    # get the min area rect
    rect = cv2.minAreaRect(c)
    box = cv2.boxPoints(rect)
    # convert all coordinates floating point values to int
    box = np.int0(box)
    # draw a red 'nghien' rectangle
    cv2.drawContours(img, [box], 0, (0, 0, 255), 2)
    cv2.imwrite('zebra_lane1.jpg', img)
    cv2.imshow("contours", img)

while True:
    key = cv2.waitKey(1)
    if key == 27:  
        break

cv2.destroyAllWindows()

Output:

image description

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-02-14 00:14:54 -0600

Seen: 1,750 times

Last updated: Mar 04 '20