opencv学习—简单方法用于斑马线检测(Python)

opencv学习—简单方法用于斑马线检测(Python)

目录

opencv学习—简单方法用于斑马线检测(Python)

1、读取原图像并将图像灰度化

 2、通过高斯滤波去除噪声信息

3、阈值分割

4、腐蚀操作

5、膨胀操作

6、轮廓检测

完整代码


方法流程如下:

1、读取原图像并将图像灰度化

原图像

 
读取并复制图像

img = cv2.imread("E:\\car\\6.JPG")
copy_img = img.copy()

修改图像尺寸

copy_img = cv2.resize(copy_img,(1600,800))

图像灰度化

gray=cv2.cvtColor(copy_img,cv2.COLOR_BGR2GRAY)

灰度化图像

 

 2、通过高斯滤波去除噪声信息

imgblur=cv2.GaussianBlur(gray,(5,5),10)

(5,5)模板,高斯核为10

 高斯滤波后图像

3、阈值分割

ret,thresh=cv2.threshold(imgblur,200,255,cv2.THRESH_BINARY)

阈值分割后图像

 

4、腐蚀操作

img_Ero=cv2.erode(thresh,kernel_Ero,iterations=3)

 腐蚀操作后图像

 

5、膨胀操作

img_Dia=cv2.dilate(img_Ero,kernel_Dia,iterations=1)

 

6、轮廓检测

contouts,h = cv2.findContours(img_Dia,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = contouts
for i in cnt:
    #坐标赋值
    x,y,w,h = cv2.boundingRect(i)
    print(x,y,w,h)
    if w>50 and h>10:
        out=cv2.drawContours(copy_img,i,-1,(0,255,0),3)
cv2.imshow('out',out)
cv2.waitKey(0)

检测结果

 

可以通过调整轮廓高和宽以及检测的x、y位置来进行检测。

完整代码

import cv2
import numpy as np
kernel_Ero = np.ones((3,1),np.uint8)
kernel_Dia = np.ones((5,1),np.uint8)
img = cv2.imread("E:\\car\\6.JPG")
copy_img = img.copy()
copy_img = cv2.resize(copy_img,(1600,800))
cv2.imshow('copy_img',copy_img)
cv2.waitKey(0)
# 图像灰度化
gray=cv2.cvtColor(copy_img,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray',gray)
cv2.waitKey(0)
# 高斯滤波
imgblur=cv2.GaussianBlur(gray,(5,5),10)
cv2.imshow('imgblur',imgblur)
cv2.waitKey(0)
#阈值处理
ret,thresh=cv2.threshold(imgblur,200,255,cv2.THRESH_BINARY)
cv2.imshow('thresh',thresh)
cv2.waitKey(0)
#腐蚀
img_Ero=cv2.erode(thresh,kernel_Ero,iterations=3)
cv2.imshow('img_Ero',img_Ero)
cv2.waitKey(0)
#膨胀
img_Dia=cv2.dilate(img_Ero,kernel_Dia,iterations=1)
cv2.imshow('img_Dia',img_Dia)
cv2.waitKey(0)
#轮廓检测
contouts,h = cv2.findContours(img_Dia,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = contouts
# img=cv2.drawContours(copy_img, cnt, -1, (0, 255, 0), 2)
# cv2.imshow('img',img)
# cv2.waitKey(0)
for i in cnt:
    #坐标赋值
    x,y,w,h = cv2.boundingRect(i)
    print(x,y,w,h)
    if x>1000 and w>50 and h>10:
        out=cv2.drawContours(copy_img,i,-1,(0,255,0),3)
cv2.imshow('out',out)
cv2.waitKey(0)

测试图像

 结果图像

鸣谢

基于python的opencv图像处理实现对斑马线的检测

 

 

  • 14
    点赞
  • 76
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岁月蹉跎的一杯酒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值