检测结果,红色表示拟合的外圆,绿色表示里面针的圆心
import os
import cv2
import numpy as np
import math
import xml.etree.ElementTree as ET
# import imutils
# 检测针脚位置
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 = 10
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
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect blobs.
minThreshValue = 110
_, gray = cv2.threshold(gray, minThreshValue, 255, cv2.THRESH_BINARY)
detector = cv2.SimpleBlobDetector_create(params)
gray = 255 - gray
keypoints = detector.detect(gray)
# print(len(keypoints))
# print(keypoints[0].pt[0])
# 如果这儿没检测到可能会出错
if len(keypoints) == 0:
print("没有检测到针角坐标,可能需要调整针角斑点检测参数")
return keypoints
else:
# print(len(keypoints))
# im_with_keypoints = cv2.drawKeypoints(gray, keypoints, np.array([]), (255, 0, 0),
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# if keypoints is not None:
# color_img = cv2.cvtColor(im_with_keypoints, cv2.COLOR_BGR2RGB)
# 画出圆的圆心
# cv2.circle(color_img, (int(keypoints[0].pt[0]), int(keypoints[0].pt[1])), 5, (0, 255, 0), -1)
# cv2.imshow("color_img",color_img)
# cv2.waitKey()
return keypoints
# 检测连接器圆形位置
def circle_detect(image):
# 灰度化
circle_img = image.copy()
gray = cv2.cvtColor(circle_img, cv2.COLOR_BGR2GRAY)
img = cv2.medianBlur(gray, 3)
# 针角圆心坐标
out_x = 0
out_y = 0
# 霍夫变换圆检测
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 10e10, param1=100, param2=30, minRadius=10, maxRadius=200)
# 如果没检测到会报错
# 这种判断方式过于简单
if circles is None:
print("没有检测到连接器外圆")
return False, 0
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(circle_img, (out_x, out_y), r, (0, 0, 255), 2)
# # # 画出圆的圆心
# cv2.circle(circle_img, (out_x, out_y), 3, (0, 0, 255), -1)
cv2.circle(circle_img, (out_x, out_y), r, (0, 0, 255), 2)
# # 画出圆的圆心
cv2.circle(circle_img, (out_x, out_y), 3, (0, 0, 255), -1)
# cv2.namedWindow("circle_img", 2)
# cv2.imshow("circle_img", circle_img)
# cv2.waitKey()
# 记录外圆坐标
out_xpoint = out_x
out_ypoint = out_y
# 只框出单个针角的位置区域
step_center = 15
step_rect = 30
out_x -= step_center
out_y -= step_center
needleRect = image[out_y: out_y + step_rect, out_x: out_x + step_rect]
# cv2.namedWindow("needleRect", 2)
# cv2.imshow("needleRect", needleRect)
# cv2.waitKey()
# 根据检测到的圆形连接器中心找针角位置
centerPoint = needelCenter_detect(needleRect)
if len(centerPoint) == 0:
print("调整位置")
return False, 0
else:
# 将针角的坐标原还至原图
in_x = int(centerPoint[0].pt[0])
in_y = int(centerPoint[0].pt[1])
in_x += out_x
in_y += out_y
# 画出针角的圆心
cv2.circle(circle_img, (in_x, in_y), 3, (0, 255, 0), -1)
# 计算两者的距离
# 假设通过标定其一个像素代表0.0056mm
DPI = 0.019696448792778324
dis = math.sqrt(math.pow(out_xpoint - in_x,2) + math.pow(out_ypoint - in_y,2))*DPI
# print("两者相互之间的距离为(mm):", dis)
#
# # 将计算结果保存在xml文件
# configFile_xml = "wellConfig.xml"
# tree = ET.parse(configFile_xml)
# root = tree.getroot()
# secondRoot = root.find("distance")
# precisionValue = root.find("precision")
# result = root.find("result")
#
# secondRoot.text = str(dis)
# tree.write("wellConfig.xml")
# if dis > float(precisionValue.text):
# result.text = str("发现插针歪斜")
# tree.write("wellConfig.xml", encoding="utf_8")
# print("发现插针歪斜")
# else:
# result.text = str("没有插针歪斜,产品合格!")
# tree.write("wellConfig.xml", encoding="utf_8")
# print("没有插针歪斜,产品合格!")
# cv2.namedWindow("image", 2)
# cv2.imshow("image",circle_img)
# cv2.waitKey()
isExists = os.path.exists("./runs/needle/")
if not isExists:
os.makedirs("./runs/needle/")
cv2.imwrite("./runs/needle/OneNeedleSmall.jpg",circle_img)
return True, dis
def dect_danZhen_start(needleImagePath, roi_TemplatePath):
# # 这个需要用户在图片中手动选取ROI
template = cv2.imread(roi_TemplatePath)
image = cv2.imread(needleImagePath)
if template is None or image is None:
return "图片读取失败", -1
theight, twidth = template.shape[:2]
# 执行模板匹配,采用的匹配方式cv2.TM_SQDIFF_NORMED
result = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)
# 归一化处理
cv2.normalize(result, result, 0, 1, cv2.NORM_MINMAX, -1)
# 寻找矩阵(一维数组当做向量,用Mat定义)中的最大值和最小值的匹配结果及其位置
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# 匹配值转换为字符串
# 对于cv2.TM_SQDIFF及cv2.TM_SQDIFF_NORMED方法min_val越趋近与0匹配度越好,匹配位置取min_loc
# 对于其他方法max_val越趋近于1匹配度越好,匹配位置取max_loc
strmin_val = str(max_val)
# 绘制矩形边框,将匹配区域标注出来
# min_loc:矩形定点
# (min_loc[0]+twidth,min_loc[1]+theight):矩形的宽高
# (0,0,225):矩形的边框颜色;2:矩形边框宽度
# cv2.rectangle(image, max_loc, (max_loc[0] + twidth, max_loc[1] + theight), (0, 0, 225), 2)
# 显示结果,并将匹配值显示在标题栏上
# cv2.namedWindow("MatchResult----MatchingValue=" + strmin_val, 2)
# cv2.imshow("MatchResult----MatchingValue=" + strmin_val, image)
# cv2.waitKey()
roi = image[max_loc[1]:max_loc[1] + theight, max_loc[0]:max_loc[0] + twidth]
dect_result = circle_detect( roi)
if dect_result[0] == True:
print("检测成功")
if dect_result[1] > 0.05:
print("不合格,距离为",dect_result[1])
return "不合格",dect_result[1]
else:
print("合格,距离为",dect_result[1],"mm")
return "合格",dect_result[1]
else:
print("检测失败")
return "检测失败", -1
def dect_danZhen_ceshi(image, template):
# # 这个需要用户在图片中手动选取ROI
# template = cv2.imread(roi_TemplatePath)
# image = cv2.imread(needleImagePath)
if template is None or image is None:
return "图片读取失败", -1
theight, twidth = template.shape[:2]
# 执行模板匹配,采用的匹配方式cv2.TM_SQDIFF_NORMED
result = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)
# 归一化处理
cv2.normalize(result, result, 0, 1, cv2.NORM_MINMAX, -1)
# 寻找矩阵(一维数组当做向量,用Mat定义)中的最大值和最小值的匹配结果及其位置
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# 匹配值转换为字符串
# 对于cv2.TM_SQDIFF及cv2.TM_SQDIFF_NORMED方法min_val越趋近与0匹配度越好,匹配位置取min_loc
# 对于其他方法max_val越趋近于1匹配度越好,匹配位置取max_loc
strmin_val = str(max_val)
# 绘制矩形边框,将匹配区域标注出来
# min_loc:矩形定点
# (min_loc[0]+twidth,min_loc[1]+theight):矩形的宽高
# (0,0,225):矩形的边框颜色;2:矩形边框宽度
# cv2.rectangle(image, max_loc, (max_loc[0] + twidth, max_loc[1] + theight), (0, 0, 225), 2)
# 显示结果,并将匹配值显示在标题栏上
# cv2.namedWindow("MatchResult----MatchingValue=" + strmin_val, 2)
# cv2.imshow("MatchResult----MatchingValue=" + strmin_val, image)
# cv2.waitKey()
roi = image[max_loc[1]:max_loc[1] + theight, max_loc[0]:max_loc[0] + twidth]
dect_result = circle_detect( roi)
if dect_result[0] == True:
print("检测成功")
if dect_result[1] > 0.05:
print("不合格,距离为",dect_result[1])
return "不合格",dect_result[1]
else:
print("合格,距离为",dect_result[1],"mm")
return "合格",dect_result[1]
else:
print("检测失败")
return "检测失败", -1
if __name__ == "__main__":
# # 从xml文件中读取要检测的图片位置
# configFile_xml = "wellConfig.xml"
# tree = ET.parse(configFile_xml)
# root = tree.getroot()
# needleImagePath = root.find("needleFilePath")
# needleTemplate = root.find("needleMatchFilePath")
#
# result = dect_danZhen_start(needleImagePath.text,needleTemplate.text)
# print(result)
# 测试
image = cv2.imread("1031/E_0_6.jpg")
roi = image[1263:1368, 1954:2050]
# roi = cv2.imread("Images/oneNeedle/ROI.jpg")
cv2.imshow("roi",roi)
cv2.waitKey()
reslut = dect_danZhen_ceshi(image, roi)
print(reslut)
# needleImagePath = "1031/E_0_6.jpg"
# roi_TemplatePath = "Images/oneNeedle/ROI.jpg"
# result = dect_danZhen_start(needleImagePath,roi_TemplatePath)
# print(result)
# # 1 路径写入xml文件
# # 2 检测结果也写入xml
# if __name__ == "__main__":
# # # 从xml文件中读取要检测的图片位置
# configFile_xml = "wellConfig.xml"
# tree = ET.parse(configFile_xml)
# root = tree.getroot()
# needleImagePath = root.find("needleFilePath")
# needleTemplate = root.find("needleMatchFilePath")
#
# # # 读文件
# # needleImagePath = "Images/Final/E_0_6.jpg"
# # needleTemplate = "Images/Final/template.jpg"
# # 检测有针的
# image = cv2.imread(needleImagePath.text)
# # # 这个需要用户在图片中手动选取ROI
# template = cv2.imread(needleTemplate.text)
# # 检测有针的
# # image = cv2.imread("Images/Final/E_0_6.jpg")
# # # 这个需要用户在图片中手动选取ROI
# # template = cv2.imread("Images/Final/template.jpg")
#
# theight, twidth = template.shape[:2]
# # 执行模板匹配,采用的匹配方式cv2.TM_SQDIFF_NORMED
# result = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)
# # 归一化处理
# cv2.normalize(result, result, 0, 1, cv2.NORM_MINMAX, -1)
# # 寻找矩阵(一维数组当做向量,用Mat定义)中的最大值和最小值的匹配结果及其位置
# min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# # 匹配值转换为字符串
# # 对于cv2.TM_SQDIFF及cv2.TM_SQDIFF_NORMED方法min_val越趋近与0匹配度越好,匹配位置取min_loc
# # 对于其他方法max_val越趋近于1匹配度越好,匹配位置取max_loc
# strmin_val = str(max_val)
# # 绘制矩形边框,将匹配区域标注出来
# # min_loc:矩形定点
# # (min_loc[0]+twidth,min_loc[1]+theight):矩形的宽高
# # (0,0,225):矩形的边框颜色;2:矩形边框宽度
# # cv2.rectangle(image, max_loc, (max_loc[0] + twidth, max_loc[1] + theight), (0, 0, 225), 2)
# # 显示结果,并将匹配值显示在标题栏上
# # cv2.namedWindow("MatchResult----MatchingValue=" + strmin_val, 2)
# # cv2.imshow("MatchResult----MatchingValue=" + strmin_val, image)
# # cv2.waitKey()
#
# roi = image[max_loc[1]:max_loc[1] + theight, max_loc[0]:max_loc[0] + twidth]
# dect_result = circle_detect( roi)
#
# if dect_result[0] == True:
# print("检测成功")
# if dect_result[1] > 0.05:
# print("不合格,距离为",dect_result[1])
# else:
# print("合格,距离为",dect_result[1],"mm")
# else:
# print("检测失败")
#
#