我如何改进我的 opencv 程序以仅检测人行横道?
How could i improve my opencv program to detect only the crosswalk?
我想检测下图中的人行横道,并用红色填充,但程序也检测其他东西。这是我的代码:
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)
这是 Python/OpenCV 中的一种方法。
- 读取输入
- white/gray 人行道条纹上的门槛
- 应用形态学打开和关闭
- 获取外部轮廓
- 过滤区域上的轮廓并保持良好的轮廓
- 在输入上绘制良好的轮廓
- 组合轮廓
- 计算组合轮廓的凸包
- 在输入上绘制凸包
- 保存结果
输入:
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)
阈值图像:
形态图像:
轮廓图:
结果:
我想检测下图中的人行横道,并用红色填充,但程序也检测其他东西。这是我的代码:
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)
这是 Python/OpenCV 中的一种方法。
- 读取输入
- white/gray 人行道条纹上的门槛
- 应用形态学打开和关闭
- 获取外部轮廓
- 过滤区域上的轮廓并保持良好的轮廓
- 在输入上绘制良好的轮廓
- 组合轮廓
- 计算组合轮廓的凸包
- 在输入上绘制凸包
- 保存结果
输入:
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)
阈值图像:
形态图像:
轮廓图:
结果: