OpenCV小案例——Canny检测车道线、寻找轮廓

import cv2
import numpy as np


def canny_test_01(image):
    image = cv2.imread(image)
    image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    edges = cv2.Canny(image, 125, 350)
    cv2.imshow('image', edges)
    cv2.waitKey(0)


def find_contours_test(image):
    image = cv2.imread(image)
    bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray, 100, 200, apertureSize=3)
    contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    cv2.drawContours(image, contours, -1, (255, 255, 0), 2)
    cv2.imshow('image', image)
    cv2.waitKey(0)


# 检测直线
def find_line(image):
    image_old = cv2.imread(image)
    image = cv2.cvtColor(image_old, cv2.COLOR_RGB2BGR)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    canny = cv2.Canny(gray, 100, 300)
    print(canny)
    lines = cv2.HoughLinesP(canny, 1, np.pi / 180, 60, minLineLength=100, maxLineGap=20)
    print(lines)
    line = lines[:, 0, :]
    for x1, y1, x2, y2 in line[:]:
        cv2.line(image_old, (x1, y1), (x2, y2), (0, 0, 255), 1)
    cv2.imshow('image', image_old)
    cv2.waitKey(0)
    # 在黑色图像上画一条白线
    # 并且穿过用于检查之间的canny轮廓图,结果是包含了与制定直线相关点的图像
    one_line = np.zeros(image_old.shape, dtype=np.uint8)
    cv2.line(one_line, (line[0][0], line[0][1]), (line[0][2], line[0][3]), (255, 255, 255), 3)
    B, G, R = cv2.split(one_line)
    cv2.imshow('one_line', one_line)
    cv2.waitKey(0)
    bitwise_and = cv2.bitwise_and(canny, B)
    cv2.imshow('bitwise_and', bitwise_and)
    cv2.waitKey(0)
    points = []
    for i in range(bitwise_and.shape[0]):
        for j in range(bitwise_and.shape[1]):
            if bitwise_and[i][j] != 0:
                points.append((i, j))
    # 拟合直线
    points = np.asarray(points)
    output = cv2.fitLine(points,  # 矩阵形式的点集
                         cv2.DIST_L2,  # 欧式距离,此时与最小二乘法相同
                         0,  # 距离参数
                         0.01,  # 径向精度
                         0.01  # 角度精度
                         )
    cv2.imshow('output', output)
    cv2.waitKey(0)


def find_lines(image):
    image = cv2.imread(image)
    image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    image_gray = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2GRAY)
    threshold = cv2.adaptiveThreshold(image_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, -2)
    cv2.imshow('threshold', threshold)
    cv2.waitKey(0)
    horzion_kernel = cv2.getStructuringElement(cv2.MORPH_RECT,
                                               (image_gray.shape[1] // 16, 1))
    vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (image_gray.shape[0] // 16, 1))
    h_opening = cv2.morphologyEx(threshold, cv2.MORPH_OPEN, horzion_kernel)
    v_opening = cv2.morphologyEx(threshold, cv2.MORPH_OPEN, vertical_kernel)
    cv2.imshow('h_opening', h_opening)
    cv2.waitKey(0)
    cv2.imshow('v_opening', v_opening)
    cv2.waitKey(0)


def find_keep_contours(image):
    image = cv2.imread(image)
    image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    image_gray = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2GRAY)
    threshold = cv2.adaptiveThreshold(image_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, -2)
    contours, hierarchy = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    cv2.drawContours(image, contours, -1, (0, 0, 255), 2)
    cv2.imshow('image', image)
    cv2.waitKey(0)
    list_index = []
    for index in range(len(contours)):
        length = cv2.arcLength(contours[index], True)
        if length >= 500 or length <= 50:
            list_index.append(index)
    img = np.zeros(image.shape)
    cv2.drawContours(img, contours, -1, (255, 255, 255), 2)
    cv2.imshow('img', img)
    cv2.waitKey(0)


if __name__ == '__main__':
    # find_contours_test('/Users/guojun/Desktop/20200402185501291.png')
    # dict3 = {[1, 2, 3]: '666'}
    # dict4 = {(1, 2, 3): '666'}
    # print(dict3,dict4)
    # find_line('/Users/guojun/Desktop/daolu.png')
    # find_lines('/Users/guojun/Desktop/lines.jpeg')
    find_keep_contours('/Users/guojun/Desktop/qian.jpeg')

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值