Python实现多个圆和圆中圆的检测

主要思想是先检测外边圆和圆心

然后再外圆内检测小圆,计算小圆圆心与外圆圆心的距离判断是不是有问题

或者可以计算两圆圆心的距离

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

def findNeedlePoints(img):
    gray_src= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    minThreshValue = 50
    _, 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 = 80
    paramsIn.maxArea = 1000
    paramsIn.minDistBetweenBlobs = 80
    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 innerHoughCicle(hsv_image, src_image, rect):
    # 霍夫变换圆检测

    gray_src = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2RGB)
    gray_src = cv2.cvtColor(gray_src, cv2.COLOR_RGB2GRAY)
    minThreshValue = 100
    _, gray = cv2.threshold(gray_src, minThreshValue, 255, cv2.THRESH_BINARY)

    kernel1 = np.ones((3, 3), dtype=np.uint8)
    kernel2 = np.ones((3, 3), dtype=np.uint8)

    gray = cv2.erode(gray, kernel2, 2)
    gray = cv2.dilate(gray, kernel1, 2)  # 1:迭代次数,也就是执行几次膨胀操作

    # cv2.namedWindow("gray", 2)
    # cv2.imshow("gray", gray)
    # cv2.waitKey()
    circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 2, 100, param1=100, param2=60, minRadius=10, maxRadius=100)
    # 如果没检测到会报错
    # 这种判断方式过于简单
    if circles is None:
        print("没有检测到连接器外圆")

    else:
        for circle in circles[0]:
            # 圆的基本信息
            # print(circle[2])
            # 坐标行列-圆心坐标
            out_x = int(circle[0])
            out_y = int(circle[1])
            # 半径
            r = int(circle[2])
            # # 在原图用指定颜色标记出圆的边界
            cv2.circle(hsv_image, (out_x, out_y), r, (0, 0, 255), 2)
            # # 画出圆的圆心
            cv2.circle(hsv_image, (out_x, out_y), 3, (0, 0, 255), -1)

        cv2.namedWindow("hsv_circle", 2)
        cv2.imshow("hsv_circle",hsv_image)
        cv2.waitKey()

def outHoughCicle(hsv_image, src_image, rect):
    # 霍夫变换圆检测

    gray_src = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2RGB)
    gray_src = cv2.cvtColor(gray_src, cv2.COLOR_RGB2GRAY)
    minThreshValue = 50
    _, gray = cv2.threshold(gray_src, minThreshValue, 255, cv2.THRESH_BINARY)

    kernel1 = np.ones((3, 3), dtype=np.uint8)
    kernel2 = np.ones((3, 3), dtype=np.uint8)

    gray = cv2.erode(gray, kernel2, 2)
    gray = cv2.dilate(gray, kernel1, 2)  # 1:迭代次数,也就是执行几次膨胀操作

    cv2.namedWindow("gray", 2)
    cv2.imshow("gray", gray)
    cv2.waitKey()
    circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 10e10, param1=100, param2=60, minRadius=500, maxRadius=10000)
    # 如果没检测到会报错
    # 这种判断方式过于简单
    if circles is None:
        print("没有检测到连接器外圆")

    else:
        for circle in circles[0]:
            # 圆的基本信息
            # print(circle[2])
            # 坐标行列-圆心坐标
            out_x = int(circle[0])
            out_y = int(circle[1])
            # 半径
            r = int(circle[2])
            # # 在原图用指定颜色标记出圆的边界
            cv2.circle(hsv_image, (out_x, out_y), r, (0, 0, 255), 2)
            # # 画出圆的圆心
            cv2.circle(hsv_image, (out_x, out_y), 3, (0, 0, 255), -1)

            # 画在原图上
            cv2.circle(src_image, (out_x + rect[0], out_y + rect[1]), r, (0, 0, 255), 2)
            # # 画出圆的圆心
            cv2.circle(src_image, (out_x + rect[0], out_y+ rect[1]), 3, (0, 0, 255), -1)

        cv2.namedWindow("hsv_circle", 2)
        cv2.imshow("hsv_circle",hsv_image)

        cv2.namedWindow("src_image", 2)
        cv2.imshow("src_image",src_image)
        cv2.waitKey()

# 检测针脚位置
def needelCenter_detect(img):
    params = cv2.SimpleBlobDetector_Params()
    # Setup SimpleBlobDetector parameters.
    # print('params')
    # print(params)
    # print(type(params))

    # Filter by Area.
    params.filterByArea = True
    params.minArea = 100
    params.maxArea = 10e3
    params.minDistBetweenBlobs = 50
    # params.filterByColor = True
    params.filterByConvexity = False
    # tweak these as you see fit
    # Filter by Circularity
    params.filterByCircularity = False
    params.minCircularity = 0.2
    # params.blobColor = 0
    # # # Filter by Convexity
    # params.filterByConvexity = True
    # params.minConvexity = 0.87
    # Filter by Inertia
    # params.filterByInertia = True
    # params.filterByInertia = False
    # params.minInertiaRatio = 0.01

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

    # Detect blobs.
    minThreshValue = 100
    _, gray = cv2.threshold(gray, minThreshValue, 255, cv2.THRESH_BINARY)

    erosion_size = 1
    # 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))
    dilate_gray = cv2.dilate(gray, element, 1)

    # cv2.namedWindow("gray", 2)
    # cv2.imshow("gray",dilate_gray)
    # cv2.waitKey()

    detector = cv2.SimpleBlobDetector_create(params)
    keypoints = detector.detect(dilate_gray)
    # print(len(keypoints))
    # print(keypoints[0].pt[0])
    # 如果这儿没检测到可能会出错
    if len(keypoints) == 0:
        print("没有检测到针角坐标,可能需要调整针角斑点检测参数")
        print(keypoints)
        return keypoints

    else:
        print("检测到孔的数量", len(keypoints))
        # im_with_keypoints = cv2.drawKeypoints(img, keypoints, np.array([]), (255, 0, 0),
        #                                       cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
        #
        # color_img = cv2.cvtColor(im_with_keypoints, cv2.COLOR_BGR2RGB)
        # 画出圆的圆心
        # for kp in keypoints:
        #     cv2.circle(img, (int(kp.pt[0]), int(kp.pt[1])), 3, (0, 0, 255), -1)
        #
        # cv2.namedWindow("color_img", 2)
        # cv2.imshow("color_img",img)
        # # cv2.waitKey()

        return keypoints

# 检测外部区域针或孔的位置
def out_circle_detect(rect_hole_info, src):
    # 灰度化
    circle_img = rect_hole_info

    gray = cv2.cvtColor(circle_img, cv2.COLOR_HSV2RGB)
    gray = cv2.cvtColor(gray, cv2.COLOR_RGB2GRAY)

    # 输出图像大小,方便根据图像大小调节minRadius和maxRadius
    # print(image.shape)
    # 进行中值滤波
    img = cv2.medianBlur(gray, 3)

    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))
    dilate_gray = cv2.dilate(img, element, 1)
    # cv2.namedWindow("dilate_gray", 2)
    # cv2.imshow("dilate_gray", dilate_gray)
    # cv2.waitKey()

    # 针角圆心坐标
    out_x, out_y, r = 0, 0, 0

    # 霍夫变换检测最大圆
    circles = cv2.HoughCircles(dilate_gray, cv2.HOUGH_GRADIENT, 1, 1000, param1=100, param2=30, minRadius=500, maxRadius=1000)
    # 如果没检测到会报错
    # 这种判断方式过于简单
    if circles is None:
        print("没有检测到连接器外圆")
        return 0, 0, 0
    else:
        for circle in circles[0]:
            # 圆的基本信息
            # print(circle[2])
            # 坐标行列-圆心坐标
            out_x = int(circle[0])
            out_y = int(circle[1])
            # 将检测到的坐标保存
            # 半径
            r = int(circle[2])
            # print(r)
            # # # 在原图用指定颜色标记出圆的边界
            cv2.circle(circle_img, (out_x, out_y), r, (0, 0, 255), 2)
            # # 画出圆的圆心
            cv2.circle(circle_img, (out_x, out_y), 5, (0, 0, 255), -1)

        cv2.namedWindow("circle_imgs", 2)
        cv2.imshow("circle_imgs", circle_img)
        cv2.waitKey()

        return out_x, out_y, r

# 检测内部区域针或孔的位置
def inner_circle_detect(rect_hole_info, src):
    # 灰度化
    circle_img = rect_hole_info

    gray = cv2.cvtColor(circle_img, cv2.COLOR_HSV2RGB)
    gray = cv2.cvtColor(gray, cv2.COLOR_RGB2GRAY)

    # 输出图像大小,方便根据图像大小调节minRadius和maxRadius
    # print(image.shape)
    # 进行中值滤波
    img = cv2.medianBlur(gray, 3)

    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))
    dilate_gray = cv2.dilate(img, element, 1)
    # cv2.namedWindow("dilate_gray", 2)
    # cv2.imshow("dilate_gray", dilate_gray)
    # cv2.waitKey()

    # 针角圆心坐标
    out_x_p = []
    out_y_p = []
    rudis = []

    # 霍夫变换检测最大圆
    circles = cv2.HoughCircles(dilate_gray, cv2.HOUGH_GRADIENT, 1, 100, param1=100, param2=30, minRadius=20, maxRadius=100)
    # 如果没检测到会报错
    # 这种判断方式过于简单
    if circles is None:
        print("没有检测到连接器外圆")
        return out_x_p, out_y_p

    else:
        for circle in circles[0]:
            # 圆的基本信息
            # print(circle[2])
            # 坐标行列-圆心坐标
            out_x = int(circle[0])
            out_y = int(circle[1])
            # 将检测到的坐标保存
            out_x_p.append(out_x)
            out_y_p.append(out_y)
            # 半径
            r = int(circle[2])
            rudis.append(r)
            # print(r)
            # # # 在原图用指定颜色标记出圆的边界
            cv2.circle(circle_img, (out_x, out_y), r, (0, 0, 255), 2)
            # # 画出圆的圆心
            cv2.circle(circle_img, (out_x, out_y), 5, (0, 0, 255), -1)

        cv2.namedWindow("circle_img", 2)
        cv2.imshow("circle_img",circle_img)
        cv2.waitKey()

        # 记录外圆坐标

        out_xpoints = out_x_p.copy()
        out_ypoints = out_y_p.copy()
        out_rudis = rudis.copy()
        # print("out_xpoints",out_xpoints)
        # print("out_ypoints",out_ypoints)

        # 只框出单个针角的位置区域
        step_center = 25
        step_rect = 50

        # 遍历所有的孔的位置
        # 记录孔的位置
        in_x_p = []
        in_y_p = []
        for i in range(0, len(out_xpoints)):
            out_x_begin = out_xpoints[i] - step_center
            out_y_begin = out_ypoints[i] - step_center
            needleRect = circle_img[out_y_begin: out_y_begin + step_rect, out_x_begin: out_x_begin + step_rect]
            # cv2.namedWindow("needleRect", 2)
            # cv2.imshow("needleRect", needleRect)
            # cv2.waitKey()

            # 根据检测到的圆形连接器中心找针角位置
            centerPoint = needelCenter_detect(needleRect)
            # print(len(centerPoint))

            if len(centerPoint) == 0:
                out_x_p.remove(out_xpoints[i])
                out_y_p.remove(out_ypoints[i])
                rudis.remove(out_rudis[i])
                print("调整位置")
            else:
                for cp in centerPoint:
                        # 将针角的坐标原还至原图
                    in_x = int(cp.pt[0])
                    in_y = int(cp.pt[1])
                    in_x += out_x_begin
                    in_y += out_y_begin

                    in_x_p.append(in_x)
                    in_y_p.append(in_y)

                # # # 画出中心孔的圆心
                #     cv2.circle(circle_img, (in_x, in_y), 4, (0, 255, 0), -1)
                #     # 画出外孔的圆心
                #     cv2.circle(circle_img, (out_xpoints[i], out_ypoints[i]), 4, (0, 0, 255), -1)

                # # 计算两者的距离
                #     # 假设通过标定其一个像素代表0.0056mm
                #     DPI = 0.0198
                #     dis = math.sqrt(math.pow(out_xpoints[i] - in_x,2) + math.pow(out_ypoints[i] - in_y,2))
                #     print("两者相互之间的距离为(mm):", dis*DPI)
        return in_x_p,in_y_p

                # cv2.namedWindow("image", 2)
                # cv2.imshow("image",circle_img)
                # cv2.waitKey()
        # if len(out_x_p) == 0:
        #     print("没检测到,需要调整位置")
        # else:
        #     for j in range(0,len(out_x_p)):
        #         # 画出外孔的圆心
        #         cv2.circle(circle_img, (out_x_p[j], out_y_p[j]), rudis[j], (0, 0, 255), 3)
        #         cv2.circle(circle_img, (out_x_p[j], out_y_p[j]), 3, (0, 0, 255), -1)
        #
        #         # cv2.circle(circle_img, (in_x_p[j], in_y_p[j]), 3, (0, 255, 0), -1)
        #
        #     cv2.namedWindow("image", 2)
        #     cv2.imshow("image",circle_img)
        #     cv2.waitKey()

def j599_4_holes_dectWX(imagePath, templatePath):
    # templatePath需要用户手动框获取ROI
    img = cv2.imread(imagePath)
    img_roi = cv2.imread(templatePath)

    if img_roi is  None:
        print("no image")

    # HSV二值化
    img_roi = cv2.medianBlur(img_roi, 5)  # 中值滤波
    outx, outy, outR = out_circle_detect(img_roi, img)
    print(outx, outy, outR )
    inx, iny = inner_circle_detect(img_roi, img)
    if len(inx) == 0 or outx == 0:
        print("没检测到位置")
        return "没检测到对象", -1
    else:
        cv2.circle(img_roi, (outx, outy), outR, (0, 0, 255), 3)

        is_ok = []

        for k in range(0, len(inx)):
            # 计算两者的距离
            # 假设通过标定其一个像素代表0.0056mm
            # 两者相互之间的距离为(mm): 9.311053946788194
            # 两者相互之间的距离为(mm): 9.163550379629067
            # 两者相互之间的距离为(mm): 8.95984457900917
            # 两者相互之间的距离为(mm): 8.977940966613671
            # 平均值为 9.103 所以其阈值为9.103 + 0.5
            DPI = 0.0198
            dis = math.sqrt(math.pow(outx - inx[k], 2) + math.pow(outy - iny[k], 2))
            dis *= DPI
            # print("两者相互之间的距离为(mm):", dis)
            if dis < 9.603:
                cv2.circle(img_roi, (inx[k], iny[k]), 8, (0, 255, 0), -1)
                # print("没有插针歪斜,产品合格")
                is_ok.append(1)
            else:
                cv2.circle(img_roi, (inx[k], iny[k]), 20, (0, 0, 255), 3)
                # print("有插针歪斜,不合格")
                is_ok.append(0)

        # cv2.namedWindow("image", 2)
        # cv2.imshow("image",img_roi)
        # cv2.waitKey()

        isExists = os.path.exists("./runs/J599/")
        if not isExists:
            os.makedirs("./runs/J599/")
        cv2.imwrite("./runs/J599/result.jpg", img_roi)

        if 0 in is_ok:
            print("有插针歪斜,不合格")
            return "有插针歪斜,不合格"
        else:
            print("没有插针歪斜,产品合格")
            return "没有插针歪斜,产品合格"

if __name__ == "__main__":
    reslut = j599_4_holes_dectWX("images/Final/E_0_8.jpg","J599-4holes_template.jpg")
    print(reslut)
#
#     # # # 4holes
#     img = cv2.imread("images/Final/E_0_8.jpg", 1)
#     # img_roi = img[973:2027, 1713:2751]
#     # img_roi = img[852:2224, 1515:2940]
#     img_roi = img[842:2234, 1480:2950]
#     cv2.imwrite("J599-4holes_template.jpg",img_roi)
#
#     # cv2.namedWindow("img_roi",2)
#     # cv2.imshow("img_roi", img_roi)
#     # cv2.waitKey()

#     if img_roi is  None:
#         print("no image")
#     else:
#         # HSV二值化
#         img_roi = cv2.medianBlur(img_roi, 5)  # 中值滤波
#         outx, outy, outR = out_circle_detect(img_roi, img)
#         print(outx, outy, outR )
#         inx, iny = inner_circle_detect(img_roi, img)
#         if len(inx) == 0 or outx == 0:
#             print("没检测到位置")
#         else:
#             cv2.circle(img_roi, (outx, outy), outR, (0, 0, 255), 3)
#
#             for k in range(0, len(inx)):
#                 # 计算两者的距离
#                 # 假设通过标定其一个像素代表0.0056mm
#                 # 两者相互之间的距离为(mm): 9.311053946788194
#                 # 两者相互之间的距离为(mm): 9.163550379629067
#                 # 两者相互之间的距离为(mm): 8.95984457900917
#                 # 两者相互之间的距离为(mm): 8.977940966613671
#                 # 平均值为 9.103 所以其阈值为9.103 + 0.5
#                 DPI = 0.0198
#                 dis = math.sqrt(math.pow(outx - inx[k], 2) + math.pow(outy - iny[k], 2))
#                 dis *= DPI
#                 # print("两者相互之间的距离为(mm):", dis)
#                 if dis > 9.603:
#                     cv2.circle(img_roi, (inx[k], iny[k]), 20, (0, 0, 255), 3)
#                     print("有插针歪斜,不合格")
#                 else:
#                     cv2.circle(img_roi, (inx[k], iny[k]), 8, (0, 255, 0), -1)
#                     print("没有插针歪斜,产品合格")
#
#             cv2.namedWindow("image", 2)
#             cv2.imshow("image",img_roi)
#             cv2.waitKey()

到此这篇关于Python实现多个圆和圆中圆的检测的文章就介绍到这了,更多相关Python检测圆内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Python OpenCV基于霍夫圈变换算法检测图像中的圆形

    目录 第一章:霍夫变换检测圆 ① 实例演示1 ② 实例演示2 ③ 霍夫变换函数解析 第二章:Python + opencv 完整检测代码 ① 源代码 ② 运行效果图 第一章:霍夫变换检测圆 ① 实例演示1 这个是设定半径范围 0-50 后的效果. ② 实例演示2 这个是设定半径范围 50-70 后的效果,因为原图稍微大一点,半径也大了一些. ③ 霍夫变换函数解析 cv.HoughCircles() 方法 参数分别为:image.method.dp.minDist.param1.param2.mi

  • 如何基于OpenCV&Python实现霍夫变换圆形检测

    简述 基于python使用opencv实现在一张图片中检测出圆形,并且根据坐标和半径标记出圆.不涉及理论,只讲应用. 霍夫变换检测圆形的原理 其实检测圆形和检测直线的原理差别不大,只不过直线是在二维空间,因为y=kx+b,只有k和b两个自由度.而圆形的一般性方程表示为(x-a)²+(y-b)²=r².那么就有三个自由度圆心坐标a,b,和半径r.这就意味着需要更多的计算量,而OpenCV中提供的cvHoughCircle()函数里面可以设定半径r的取值范围,相当于有一个先验设定,在每一个r来说,在

  • Python+OpenCV实现单个圆形孔和针检测

    如果中间红色区域是针则可以用下面的代码检测,其阈值和斑点检测的参数根据图像像素值做相应修改 检测的主要思路是先通过找到外面的大圆,再通过圆心定位出一个ROI区域,在ROI区域中检测中心的检测对象 import os import cv2 import numpy as np import math # 检测针脚位置 def needelCenter_detect(img): params = cv2.SimpleBlobDetector_Params() # Setup SimpleBlobDe

  • Python下opencv使用hough变换检测直线与圆

    在数字图像中,往往存在着一些特殊形状的几何图形,像检测马路边一条直线,检测人眼的圆形等等,有时我们需要把这些特定图形检测出来,hough变换就是这样一种检测的工具. Hough变换的原理是将特定图形上的点变换到一组参数空间上,根据参数空间点的累计结果找到一个极大值对应的解,那么这个解就对应着要寻找的几何形状的参数(比如说直线,那么就会得到直线的斜率k与常熟b,圆就会得到圆心与半径等等). 关于hough变换,核心以及难点就是关于就是有原始空间到参数空间的变换上.以直线检测为例,假设有一条直线L,

  • Python+OpenCV实现图片中的圆形检测

    效果展示 中心的三个没检测到 import cv2 import numpy as np import matplotlib.pyplot as plt w = 20 h = 5 params = cv2.SimpleBlobDetector_Params() # Setup SimpleBlobDetector parameters. print('params') print(params) print(type(params)) # Filter by Area. params.filte

  • Python实现多个圆和圆中圆的检测

    主要思想是先检测外边圆和圆心 然后再外圆内检测小圆,计算小圆圆心与外圆圆心的距离判断是不是有问题 或者可以计算两圆圆心的距离 # coding:utf-8 import math import cv2 import numpy as np import os def findNeedlePoints(img): gray_src= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) minThreshValue = 50 _, gray = cv2.threshold(gr

  • JavaScript+html5 canvas制作的圆中圆效果实例

    本文实例讲述了JavaScript+html5 canvas制作的圆中圆效果.分享给大家供大家参考,具体如下: 运行效果截图如下: 具体代码如下: <!DOCTYPE html> <html> <head> <title>demo</title> <style type="text/css"> #canvas { background:#F2F2F2; height:500px; height:500px; mar

  • python编程之requests在网络请求中添加cookies参数方法详解

    哎,好久没有学习爬虫了,现在想要重新拾起来.发现之前学习爬虫有些粗糙,竟然连requests中添加cookies都没有掌握,惭愧.废话不宜多,直接上内容. 我们平时使用requests获取网络内容很简单,几行代码搞定了,例如: import requests res=requests.get("https://cloud.flyme.cn/browser/index.jsp") print res.content 你没有看错,真的只有三行代码.但是简单归简单,问题还是不少的. 首先,这

  • Python实现比较两个文件夹中代码变化的方法

    本文实例讲述了Python实现比较两个文件夹中代码变化的方法.分享给大家供大家参考.具体如下: 这里将修改代码后的目录与原始目录做对比,罗列出新增的代码文件,以及修改过的代码文件 # -*- coding: utf-8 -*- import os; folderA = "F:\\Projects\\FreeImageV3_14_1\\".lower(); folderB = u"E:\\Software\\图像解码库\\FreeImage3141\\FreeImage\\&q

  • Python输出PowerPoint(ppt)文件中全部文字信息的方法

    本文实例讲述了Python输出PowerPoint(ppt)文件中全部文字信息的方法.分享给大家供大家参考.具体分析如下: 下面的代码依赖于windows com,所以必须在机器上安装PowerPoint才能用,可以将PPT文件中的所有纯文本信息进行输出到指定的文件,非常实用. import win32com from win32com.client import Dispatch, constants ppt = win32com.client.Dispatch('PowerPoint.App

  • Python实现树的先序、中序、后序排序算法示例

    本文实例讲述了Python实现树的先序.中序.后序排序算法.分享给大家供大家参考,具体如下: #encoding=utf-8 class Tree(): def __init__(self,leftjd=0,rightjd=0,data=0): self.leftjd = leftjd self.rightjd = rightjd self.data = data class Btree(): def __init__(self,base=0): self.base = base #前序遍历 根

  • Python结巴中文分词工具使用过程中遇到的问题及解决方法

    本文实例讲述了Python结巴中文分词工具使用过程中遇到的问题及解决方法.分享给大家供大家参考,具体如下: 结巴分词是Python语言中效果最好的分词工具,其功能包括:分词.词性标注.关键词抽取.支持用户词表等.这几天一直在研究这个工具,在安装与使用过程中遇到一些问题,现在把自己的一些方法帖出来分享一下. 官网地址:https://github.com/fxsjy/jieba 1.安装. 按照官网上的说法,有三种安装方式, 第一种是全自动安装:easy_install jieba 或者 pip

  • python清除指定目录内所有文件中script的方法

    本文实例讲述了python清除指定目录内所有文件中script的方法.分享给大家供大家参考.具体如下: 将脚本存储为stripscripts.py 调用语法 : python stripscripts.py <directory> 使用范例 : python stripscripts.py d:\myfiles # Hello, this is a script written in Python. See http://www.pyhon.org import os,sys,string,r

  • python实现应用程序在右键菜单中添加打开方式功能

    最近项目组开发的一个小工具想要在右键菜单中添加打开方式,以有道云笔记为例进行了需求拆解和代码编写 1.需求拆解: 如何实现手动添加右键菜单的打开方式: Step1:打开注册表编辑器,Win+R->输入 "regedit" Step2:在HKEY_CLASSES_ROOT/*/shell (或者HKEY_LOCAL_MACHINE/SOFTWARE/Classes/*/shell ,两个目录是一样的) 添加一个key:YNote,然后在该项中新建项command,然后再编辑字符串,

  • python使用xlrd模块读取xlsx文件中的ip方法

    程序中经常需要使用excel文件,批量读取文件中的数据 python读取excel文件可以使用xlrd模块 pip install xlrd安装模块 示例: #coding=utf8 import xlrd from os import path import sys filename='ip.xlsx' if not path.isfile(filename): print "err: not exists or not file ip.xlsx " sys.exit() getfi

随机推荐