blob只能检测里面是黑色外面是白色的斑点

如果要检测里面是白色外面是黑色的斑点,则图像要取反

img_inv = 255 - img

原图

检测结果图

plt

opencv

# coding:utf-8
import math
import cv2
import numpy as np
import xml.etree.ElementTree as ET
import random
import matplotlib.pyplot as pltdef get_distance_point2line(point, line_ab):    # 求点到直线的距离"""Args:point: [x0, y0]line_ab: [k, b]"""k, b = line_abdistance = abs(k * point[0] - point[1] + b) / math.sqrt(k ** 2 + 1)return distance# 没有考虑垂直和水平线的情况
def drawLines(img, allCirclesCenter):# 计算两排点的中心线nptest = np.array(allCirclesCenter)line = cv2.fitLine(nptest, cv2.DIST_L2, 0, 0.001, 0.0)# print(line)k = line[1] / line[0]b = line[3] - k * line[2]# 如果是水平线if k <= 10e-5:pass# 如果是垂直if k > 10e5:passprint('y = {:0.8f}x + {:0.8f}'.format(k[0], b[0]))ptStart, ptEnd = (0, int(k * 0 + b)), (img.shape[1], int(k * img.shape[1] + b))# 坐标点取得整数需要修改***************  画一线占满整个图片的线cv2.line(img, ptStart, ptEnd, (0, 0, 255), thickness=2, lineType=3)# cv2.imshow("line", img)# cv2.waitKey()# 区分上下各点,拟合两条直线line1_yx, line2_yx = [], []for i in allCirclesCenter:if i[1] < float(k * i[0] + b):line1_yx.append(i)else:line2_yx.append(i)# line1 求第一条直线nptest1 = np.array(line1_yx)line1 = cv2.fitLine(nptest1, cv2.DIST_L2, 0, 0.01, 0.0)k1 = line1[1] / line1[0]b1 = line1[3] - k1 * line1[2]# print(line1)print('line1')print('y = {:0.8f}x + {:0.8f}'.format(k1[0], b1[0]))for i in line1_yx:  # 显示点到直线的距离point = iline_ = k1, b1dis = get_distance_point2line(point, line_)# print('距离: ' + str(dis))ptStart, ptEnd = (0, int(k1 * 0 + b1)), (img.shape[1], int(k1 * img.shape[1] + b1))# 坐标点取得整数需要修改***************cv2.line(img, ptStart, ptEnd, ( 255, 0, 0), 2)# cv2.imshow("line1", img)# lin2 求第二条直线nptest2 = np.array(line2_yx)line2 = cv2.fitLine(nptest2, cv2.DIST_L2, 0, 0.01, 0.0)k2 = line2[1] / line2[0]b2 = line2[3] - k2 * line2[2]# print(line2)print('line2')print('y = {:0.8f}x + {:0.8f}'.format(k2[0], b2[0]))ptStart, ptEnd = (0, int(k2 * 0 + b2)), (img.shape[1], int(k2 * img.shape[1] + b2)) # 坐标点取得整数需要修改***************cv2.line(img, ptStart, ptEnd, ( 255, 0, 0), 2)  # 像素值必须是整数****************# cv2.imshow("line2", img)# cv2.waitKey()imgNormal = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)plt.imshow(imgNormal)plt.show()def mainFigure(img):paramsOut = cv2.SimpleBlobDetector_Params()# Setup SimpleBlobDetector parameters.# print('params')# print(params)# print(type(params))# Filter by Area.paramsOut.filterByArea = TrueparamsOut.minArea = 5000paramsOut.maxArea = 10e3paramsOut.minDistBetweenBlobs = 25paramsOut.filterByColor = TrueparamsOut.filterByConvexity = FalseparamsOut.minThreshold = 10paramsOut.maxThreshold = 30*2.5# tweak these as you see fit# Filter by Circularity# params.filterByCircularity = False# params.minCircularity = 1# params.blobColor = 0# # # Filter by Convexity# params.filterByConvexity = True# params.minConvexity = 1# Filter by Inertia# params.filterByInertia = True# params.filterByInertia = False# params.minInertiaRatio = 0.1gray_src= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)# Detect blobs.# image = cv2.resize(gray_img, (int(img.shape[1]/4),int(img.shape[0]/4)), 1, 1, cv2.INTER_LINEAR)# image = cv2.resize(gray_img, dsize=None, fx=0.25, fy=0.25, interpolation=cv2.INTER_LINEAR)minThreshValue = 35_, gray = cv2.threshold(gray_src, minThreshValue, 255, cv2.THRESH_BINARY)gray = cv2.resize(gray, dsize=None, fx=1, fy=1, interpolation=cv2.INTER_LINEAR)# cv2.imshow("gray1",gray)kernel = np.ones((3, 3), dtype=np.uint8)gray = cv2.dilate(gray, kernel, 1)  # 1:迭代次数,也就是执行几次膨胀操作gray = cv2.erode(gray, kernel, 1)# plt.imshow(gray)# cv2.imshow("gray",gray)# 找到距离原点(0,0)最近和最远的点# 检测外侧圆detector = cv2.SimpleBlobDetector_create(paramsOut)keypoints = detector.detect(gray)# opencvim_with_keypoints = cv2.drawKeypoints(gray, keypoints, np.array([]), (255, 0, 0),  cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)# im_with_keypoints = cv2.drawKeypoints(img, keypoints, np.array([]), (0, 255, 0), cv2.DRAW_MATCHES_FLAGS_DEFAULT)# print(keypoints)# cv2.imshow("zero_gray", gray)# 检测内侧圆 图像取反paramsIn = cv2.SimpleBlobDetector_Params()paramsIn.filterByArea = TrueparamsIn.minArea = 100paramsIn.maxArea = 500paramsIn.minDistBetweenBlobs = 10paramsIn.filterByColor = TrueparamsIn.filterByConvexity = FalseparamsIn.minThreshold = 30paramsIn.maxThreshold = 30*2.5# 图像取反needleGray = 255 - gray.copy()cv2.imshow('needleGray', needleGray)detector2 = cv2.SimpleBlobDetector_create(paramsIn)needleKeypoints = detector2.detect(needleGray)# opencvneedle_keypoints = cv2.drawKeypoints(needleGray, needleKeypoints, np.array([]), (255, 0, 0),cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)allNeedlePoints = []if keypoints is not None:for i in range(len(needleKeypoints)):allNeedlePoints.append(needleKeypoints[i].pt)# 不可能大多数是斜的,直接按照大多数的点集拟合即可# print(allCirclesCenter)# if allCirclesCenter is not None:if len(allNeedlePoints) > 10:drawLines(needle_keypoints, allNeedlePoints)color_img = cv2.cvtColor(im_with_keypoints, cv2.COLOR_BGR2RGB)needle_img = cv2.cvtColor(needle_keypoints, cv2.COLOR_BGR2RGB)cv2.imshow('holeShow', color_img)cv2.imshow('needleShow', needle_img)cv2.waitKey()if __name__ == "__main__":# # # 单张图片测试# img = cv2.imread("images/HenFeng/HF01.jpg",1)# img = cv2.imread("images/Snap_0.jpg",1)# img = cv2.imread("images/Holes/Hole2.jpg",1)# mainFigure(img)# # 所有图片测试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)

python Blob检测圆点相关推荐

  1. CS131专题-6:图像特征(Blob检测、LoG算子、Harris-Laplacian)

    本专题介绍的技术应用已不广,但是这些有利于理解SIFT算法的原理,也有助于感悟CV传统技术的发展变迁,以及解决问题的思路. 速记要点: blob是什么:blob是描述图像中局部区域的平均像素强度的特征 ...

  2. python代码检测链表中的环并删除环

    python代码检测链表中的环并删除环 在计算机科学中,链表是数据元素的线性集合,其顺序不是由它们在内存中的物理位置决定的.相反,每个元素指向下一个元素.它是一种数据结构,由一组节点组成,这些节点共同 ...

  3. python 人脸检测

    python 人脸检测 pip install opencv-python # 导入cv模块 import cv2 def face_detector():# 人脸识别cap = cv2.VideoC ...

  4. python opencv 检测特定颜色

    python opencv 检测特定颜色 import cv2 import numpy as npcap = cv2.VideoCapture(0)# set blue thresh 设置HSV中蓝 ...

  5. 用 Opencv 和 Python 模糊检测

    用 Opencv 和 Python 模糊检测 在刚刚过去的这个周末,我坐下来想在 iphoto 中整理这些海量的照片.这不仅仅意味着巨大的工作量,因为我很快注意到一个现象--其中充斥着大量模糊的照片. ...

  6. python udp_如何用python方法检测UDP端口

    如何用python方法检测UDP端口,首先要了解什么是UDP端口及作用.网上搜索了一圈后,我得到的个人理解是:UDP端口是含有网络服务必须的源端口和目的端口信息,用以建立和实现网络传输服务. 那么如何 ...

  7. python语言可以在哪系统操作-python能检测到它运行的是哪个操作系统?

    python可以检测操作系统,然后为文件系统构造一个if / else语句. 我需要用FileSys字符串替换Fn字符串中的C: CobaltRCX .import os.path, csv from ...

  8. python opencv检测人脸

    python opencv检测人脸 文章目录: 一.opencv检测一张图片 二.opencv摄像头实时检测人脸 一.opencv检测一张图片 opencv检测人脸分成三部分: 1.图片转换成灰色(降 ...

  9. OpenCV BLOB检测和过滤区域的实例(附完整代码)

    OpenCV BLOB检测和过滤区域的实例 OpenCV BLOB检测和过滤区域的实例 OpenCV BLOB检测和过滤区域的实例 #include <opencv2/core.hpp> ...

最新文章

  1. 基于聚类的图像分割-Python
  2. oracle symonym_oracle vs. SQL 同义词synonym 别名 alias | 学步园
  3. 软件需求阅读笔记之三
  4. boost::range模块sliced相关的测试程序
  5. python webdriver api-操作日期元素的方法
  6. 恒生证券期货行业用户维稳工作指引(一)
  7. java junit autowired_写Junit测试时用Autowired注入的类实例始终为空怎么解?
  8. 软件项目周报_软件产品研发流程
  9. 文件 服务器 pandas,疑难杂症-使用pandas_profiling查看EDA数据文档遇到的一些坑
  10. python中execute函数_在excel中调用python函数
  11. linux返回值含义,linux命令返回值的含义解析
  12. onlyoffice添加中文字体及字号
  13. BLP读书摘录和笔记——make
  14. 手把手教你在树莓派上搭建ghost个人博客呦
  15. 轮询小案例-扫码登录
  16. 北工大计算机学院大赛,做北工大的竞赛咖!这些信息你一定不能错过!
  17. RTMP 摄像头推流至七牛云直播
  18. java定时器每天执行一次_定时器-每天23:00执行一次
  19. 通过爬虫在GEO数据库上获取对应SRR号
  20. SDKMAN!使用指南

热门文章

  1. WEB入门之十八 动画特效
  2. 【前端】HTML基础(学习笔记)
  3. 苹果操作系统入门知识
  4. QQ中MM常用的双关语(搞笑)
  5. 转:NetBackup 7.5:放下磁带,才能走更远?
  6. rust 贪图碎片_如果智慧结晶碎片可兑换暗魂水晶,你能否做到一秒毕业
  7. 苹果2018春季发布会_苹果春季发布会 这几大猜想能实现吗?
  8. visual studio 2015 “正在加载..........的符号”问题的 解决方法
  9. poi设置word表格单元格宽度_xwpftable设置宽度;POI操作Word设置表格宽度
  10. 中国自行车十强企业FRW辐轮王国内外十大最著名自行车品牌排行榜