python opencv 斑点和直线检测

检测结果

python opencv 斑点和直线检测_第1张图片

 找白色的线

python opencv 斑点和直线检测_第2张图片

线可以通过长度调节

# coding:utf-8
import math
import cv2
import numpy as np

def findNeedlePoints(img):
    gray_src= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    minThreshValue = 200
    _, gray = cv2.threshold(gray_src, minThreshValue, 255, cv2.THRESH_BINARY)
    erosion_size = 3
    # element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * erosion_size + 1, 2 * erosion_size + 1),
    #                                     (erosion_size, erosion_size))
    element = cv2.getStructuringElement(cv2.MORPH_ERODE, (2 * erosion_size + 1, 2 * erosion_size + 1),
                                        (erosion_size, erosion_size))
    #MORPH_ELLIPSE 不同的测试一下
    erosion_gray = cv2.erode(gray, element, 3)

    cv2.imshow("erosion_gray", erosion_gray)

    paramsIn = cv2.SimpleBlobDetector_Params()
    paramsIn.filterByArea = True

    # 不同图片应该调节的参数
    paramsIn.minArea = 200
    paramsIn.maxArea = 1000
    paramsIn.minDistBetweenBlobs = 90
    paramsIn.filterByColor = True
    paramsIn.filterByConvexity = False
    paramsIn.minThreshold = 100*2
    paramsIn.maxThreshold = 1000

    # 图像取反
    needleGray = 255 - erosion_gray.copy()
    # 中值滤波和腐蚀去噪
    needleGray = cv2.medianBlur(needleGray, 3)

    # cv2.imshow('needleGray', needleGray)


    erosion_size = 2
    element = cv2.getStructuringElement(cv2.MORPH_RECT, (2 * erosion_size + 1, 2 * erosion_size + 1),
                                        (erosion_size, erosion_size))
    needlePoints = cv2.erode(needleGray, element, 1)
    cv2.imshow('needle=Points', needlePoints)

    detector2 = cv2.SimpleBlobDetector_create(paramsIn)
    needleKeypoints = detector2.detect(needlePoints)
    # opencv
    needle_keypoints = cv2.drawKeypoints(needlePoints, needleKeypoints, np.array([]), (255, 0, 0),
                                         cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

    allNeedlePoints = []
    if needleKeypoints is not None:
        for i in range(len(needleKeypoints)):
            allNeedlePoints.append(needleKeypoints[i].pt)



    color_img = cv2.cvtColor(needle_keypoints, cv2.COLOR_BGR2RGB)
    # needle_img = cv2.cvtColor(im_with_keypoints, cv2.COLOR_BGR2RGB)

    cv2.imshow('holeShow', color_img)
    # cv2.imshow('needleShow', needle_img)

    cv2.waitKey()


def findWhiteLines(img):
    minThreshValue = 200
    gray_src= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    _, gray = cv2.threshold(gray_src, minThreshValue, 255, cv2.THRESH_BINARY)
    cv2.imshow("gray", gray)
    # erosion_size = 3
    # element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * erosion_size + 1, 2 * erosion_size + 1),
    #                                     (erosion_size, erosion_size))
    # erosion_dst = cv2.erode(gray, element, 3)
    # cv2.imshow("erosion", erosion_dst)
    lsd = cv2.createLineSegmentDetector(2, 0.1)
    dlines = lsd.detect(gray)



    if dlines is not None:
        for dline in dlines[0]:
            x0 = int(round(dline[0][0]))
            y0 = int(round(dline[0][1]))
            x1 = int(round(dline[0][2]))
            y1 = int(round(dline[0][3]))
            if np.linalg.norm(np.array([x0, y0]) - np.array([x1, y1])) > 300:
              cv2.line(img, (x0, y0), (x1, y1), (0, 0, 255), 2, cv2.LINE_AA)



    cv2.imshow("cvlines", img)
    cv2.waitKey()


if __name__ == "__main__":

    # # 单张图片测试
    img = cv2.imread("images/CircleLinker/CLinker10.jpg",1)
    # img = cv2.imread("images/Snap_0.jpg",1)
    # img = cv2.imread("images/Holes/Hole2.jpg",1)
    if img is not None:
        findNeedlePoints(img)
        # mainFigure(img)
        findWhiteLines(img)

    else:
        print("no image")

    # # # 所有图片测试
    # for i in range(5):
    #     fileName = "Zhen" + str(i+1) + ".jpg"
    # # img = cv2.imread("circles/Snap_007.jpg",1)
    #     img = cv2.imread("images/ZhenJiao/" + fileName,1)
    #     print(fileName)
    #     mainFigure(img)

你可能感兴趣的:(Opencv/Halcon,机器视觉与图形图像,Python,opencv,python,计算机视觉)