opencv 裂隙检测用红色激光线识别裂隙+opencv骨架化

本文介绍了使用OpenCV进行裂隙检测的方法,通过红色激光线并结合HSV颜色空间和霍夫直线检测来识别裂隙。过程中需要注意摄像头与激光笔的角度调整、光照控制以及图像噪声处理。虽然尝试了骨架化方法,但效果不理想,作者提出了利用目标与上一帧的IoU计算来改进检测精度,欢迎大家提出更好的方案。
摘要由CSDN通过智能技术生成

思路:

1、摄像头和激光笔要产生夹角,人为让激光线产生落差

2、降低摄像头亮度,不然会在提取激光线的产生各种光斑干扰

3、BGR转HSV提取红色区域,如果效果不好再加OTSU二值分割

4、霍夫直线检测

5、检测直线对掩码图做差

6、形态学操作处理噪点

import os
from PIL import Image
import cv2
import numpy as np
import math
index = 0
def morphology(img):
    ret,thresh2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
    op_close = cv2.morphologyEx(thresh2, cv2.MORPH_CLOSE, kernel)
    op_open = cv2.morphologyEx(op_close, cv2.MORPH_OPEN, kernel)
    return op_open
def mask(img_mask,raw):
    img_mask_1,img_mask_2,img_mask_3 = cv2.split(img_mask)
    x = np.zeros(img_mask_2.shape).astype('uint8')
    img_mask_x = cv2.merge([x,img_mask_1,x])
    masked = cv2.add(raw, img_mask_x)
    return masked
def extract_red(img):
    img_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    rows, cols, channels = img.shape
    lower_red = np.array([0, 43, 46])
    upper_red = np.array([10, 255, 255])
    mask0 = cv2.inRange(img_hsv,lower_red,upper_red)
    lower_red = np.array([156, 43, 46])
    upper_red = np.array([180, 255, 255])
    mask1 = cv2.inRange(img_hsv,lower_red,upper_red)
    maskpp = mask0 + mask1
    return maskpp
def operation(img):
    img = cv2.resize(img, (1152,648))
    raw = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
    raw_gray = cv2.cvtColor(raw,cv2.COLOR_RGB2GRAY)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5, 5))
    eroded = cv2.erode(raw_gray,kernel)
    lines = cv2.HoughLinesP(eroded,1,np.pi/180,60,minLineLength=100,maxLineGap=20)     
    if lines is not None:
        line = lines[:,0,:]
        sums = np.zeros(raw_gray.shape).astype('uint8')
        for x1,y1,x2,y2 in line[:]:
            mask_line = np.zeros(raw_gray.shape).astype('uint8')
            cv2.line(mask_line,(x1,y1),(x2,y2),(255,255,255),1)        
            b,g,r  = cv2.split(raw)
            img2subtraction = cv2.subtract(mask_line,b)
            sums = sums + img2subtraction
            sums[ sums > 255 ] = 255
        return sums
    else:
        sums =  np.zeros(raw_gray.shape).astype('uint8')
        return sums
def get_length(x1,y1,x2,y2):
    point1=np.array([x1,y1])
    point2=np.array([x2,y2])
    x=point2-point1
    res=math.hypot(x[0],x[1])
    return res
def get_gradient(x1,y1,x2,y2):
    if x1 != x2 :
        k = (y2-y1) / (x2-x1)
        return k
    else:
        k = 0
        return k
def start(img):
    masks = extract_red(img)
    sums = operation(masks)
    return sums


cap = cv2.VideoCapture("0.mp4")
while(1):
    ret, frame = cap.read()
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值