如需安装运行环境或远程调试,可加QQ905733049, 或QQ2945218359由专业技术人员远程协助!
运行结果如下:
代码如下:
import cv2 as cv
import numpy as np
def do_canny(frame):
gray = cv.cvtColor(frame, cv.COLOR_RGB2GRAY)
blur = cv.GaussianBlur(gray, (5, 5), 0)
canny = cv.Canny(blur, 50, 150)
return canny
def do_segment(frame):
height = frame.shape[0]
polygons = np.array([
[(0, height), (800, height), (380, 290)]
])
mask = np.zeros_like(frame)
cv.fillPoly(mask, polygons, 255)
segment = cv.bitwise_and(frame, mask)
return segment
def calculate_lines(frame, lines):
left = []
right = []
for line in lines:
x1, y1, x2, y2 = line.reshape(4)
slope = parameters[0]
y_intercept = parameters[1]
if slope < 0:
left.append((slope, y_intercept))
else:
right.append((slope, y_intercept))
left_avg = np.average(left, axis = 0)
right_avg = np.average(right, axis = 0)
left_line = calculate_coordinates(frame, left_avg)
right_line = calculate_coordinates(frame, right_avg)
return np.array([left_line, right_line])
def calculate_coordinates(frame, parameters):
slope, intercept = parameters
y1 = frame.shape[0]
y2 = int(y1 - 150)
return np.array([x1, y1, x2, y2])
cap = cv.VideoCapture("input.mp4")
while (cap.isOpened()):
ret, frame = cap.read()
canny = do_canny(frame)
cv.imshow("canny", canny)
segment = do_segment(canny)
hough = cv.HoughLinesP(segment, 2, np.pi / 180, 100, np.array([]), minLineLength = 100, maxLineGap = 50)
lines = calculate_lines(frame, hough)
lines_visualize = visualize_lines(frame, lines)
cv.imshow("hough", lines_visualize)
output = cv.addWeighted(frame, 0.9, lines_visualize, 1, 1)
cv.imshow("output", output)
if cv.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows()
运行结果如下:
Python, C++, PHP语言学习参考实例连接:
C++学习参考实例:
C++实现图形界面五子棋游戏源码:
https://blog.csdn.net/alicema1111/article/details/90035420
C++实现图形界面五子棋游戏源码2:
https://blog.csdn.net/alicema1111/article/details/106479579
C++ OpenCV相片视频人脸识别统计人数:
https://blog.csdn.net/alicema1111/article/details/105833928
VS2017+PCL开发环境配置:
https://blog.csdn.net/alicema1111/article/details/106877145
VS2017+Qt+PCL点云开发环境配置:
https://blog.csdn.net/alicema1111/article/details/105433636
C++ OpenCV汽车检测障碍物与测距:
https://blog.csdn.net/alicema1111/article/details/105833449
Windows VS2017安装配置PCL点云库:
https://blog.csdn.net/alicema1111/article/details/105111110
VS+VTK+Dicom(dcm)+CT影像切片窗体界面显示源码
https://blog.csdn.net/alicema1111/article/details/106994839
Python学习参考实例:
Python相片更换背景颜色qt窗体程序:
https://blog.csdn.net/alicema1111/article/details/106919140
OpenCV汽车识别检测数量统计:
https://blog.csdn.net/alicema1111/article/details/106597260
OpenCV视频识别检测人数跟踪统计:
https://blog.csdn.net/alicema1111/article/details/106113042
OpenCV米粒检测数量统计:
https://blog.csdn.net/alicema1111/article/details/106089697
opencv人脸识别与变形哈哈镜:
https://blog.csdn.net/alicema1111/article/details/105833123
OpenCV人脸检测打卡系统:
https://blog.csdn.net/alicema1111/article/details/105315066
Python+OpenCV摄像头人脸识别:
https://blog.csdn.net/alicema1111/article/details/105107286
Python+Opencv识别视频统计人数:
https://blog.csdn.net/alicema1111/article/details/103804032
Python+OpenCV图像人脸识别人数统计:
https://blog.csdn.net/alicema1111/article/details/105378639
python人脸头发身体部位识别人数统计:
https://blog.csdn.net/alicema1111/article/details/116424942
PHP网页框架:
PHP Laravel框架安装与配置后台管理前台页面显示:
https://blog.csdn.net/alicema1111/article/details/106686523