0

我正在尝试运行用于检测齿轮缺陷的代码,无论齿轮中的螺栓是否存在,但面临同样的困难。

我正在运行的代码面临一个问题,即SSIM值对于好齿轮和坏齿轮都不正确。

此外,如果可能的话,请告诉我它如何检测到否。齿轮的齿数。

请找到代码

import cv2
import numpy as np
from skimage.metrics import structural_similarity as compare_ssim
import argparse
import imutils
original = cv2.imread("Pictures2/train/Corrected/60.jpg")
duplicate = cv2.imread("Pictures/train/Defected/3.jpg")
original=cv2.resize(original,(1000,640))
duplicate=cv2.resize(duplicate,(1000,640))

# convert the images to grayscale
grayA = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(duplicate, cv2.COLOR_BGR2GRAY)
_, threshold1= cv2.threshold( grayA,80,255,cv2.THRESH_BINARY_INV)
_, threshold2= cv2.threshold( grayB,80,255,cv2.THRESH_BINARY_INV)
# compute the Structural Similarity Index (SSIM) between the two
# images, ensuring that the difference image is returned
(score, diff) = compare_ssim(threshold1, threshold2, full=True)
diff = (diff * 255).astype("uint8")
print("SSIM: {}".format(score))
# threshold the difference image, followed by finding contours to
# obtain the regions of the two input images that differ
thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
# loop over the contours

#checking similarities using ORB Algorithm
orb= cv2.ORB_create()
kp_1, desc_1= orb.detectAndCompute(threshold1,None)
kp_2, desc_2= orb.detectAndCompute(threshold2,None)
matcher= cv2.BFMatcher(cv2.NORM_HAMMING)
matches=matcher.knnMatch(desc_1,desc_2,k=2)
good=[]
for m,n in matches:
    if m.distance<0.7*n.distance:
        good.append([m])
final_image=cv2.drawMatchesKnn(threshold1,kp_1,threshold2,kp_2,good,None)
final_image=cv2.resize(final_image,(1000,640))
print(len(matches))
cv2.imshow("matches",final_image)
if score>0.20:
    print("ok")
else:
    print("not ok")
#cv2.imshow("Original", original)
#cv2.imshow("Duplicate", duplicate)
#cv2.imshow("Difference", difference)
cv2.waitKey(0)
cv2.destroyAllWindows()

i/p 图片:

已更正 在此处输入图像描述

缺陷

在此处输入图像描述

输出的ss

在此处输入图像描述

在此处输入图像描述

我想检测齿轮的缺失螺栓(即圆孔)。并使用以下代码:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import cv2
import numpy as np


def find_circles(img, min=95, max=100):
    equ = cv2.equalizeHist(img)

    k_smooth = 9  # 9
    gauss = cv2.GaussianBlur(equ, (k_smooth, k_smooth), 0)  # equ

    ret, otsu = cv2.threshold(gauss, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

    blur = cv2.medianBlur(otsu, 21)

    circles = cv2.HoughCircles(blur, cv2.HOUGH_GRADIENT, 1, 100,
                               param1=200, param2=15, minRadius=min, maxRadius=max)
    return circles


def draw(img, circles, counter):
    res = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    if circles is not None:
        idx = 1
        for i in circles[0, :]:
            # draw the outer circle
            cv2.circle(res, (i[0], i[1]), i[2], (0, 0, 255), 3)
            # draw the center of the circle
            cv2.circle(res, (i[0], i[1]), 2, (255, 0, 0), 15)
            res = cv2.putText(res, str(idx), (i[0] + 100, i[1] + 100), cv2.FONT_HERSHEY_COMPLEX, 4, (255, 255, 255), 3,
                              cv2.LINE_AA)
            idx += 1
    cv2.namedWindow("There are {} holes without bolts.\n".format(counter), cv2.WINDOW_NORMAL)
    cv2.imshow("There are {} holes without bolts.\n".format(counter), res)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


empty = cv2.imread("Pictures/train/Defected/3.jpg", 0)
full = cv2.imread("Pictures2/train/Defected/60.jpg", 0)

img = full

circles = find_circles(img)

if circles is not None:
    circles = np.uint16(np.around(circles))
    counter = len(circles[0, :])
else:
    counter = 0

draw(img, circles, counter)

但对于不同的馈送图像,我没有得到所需的输出。

请找到相同的ss。

在此处输入图像描述

在此处输入图像描述

请找到不同方向的输入图像

1 在此处输入图像描述

2 在此处输入图像描述

3 在此处输入图像描述

4 在此处输入图像描述

5 在此处输入图像描述

4

2 回答 2

0

在二值化图像上,缺失的粗体显示为漂亮的圆形,易于按面积和圆形检测。

您还可以检测中心孔并检查距离。

在此处输入图像描述

另一种选择是模板匹配与相似性分数评估。

在此处输入图像描述

于 2022-02-25T08:01:18.303 回答
0

如果您使用与问题中的图像相同的设置:

  • 相机,
  • 图像分辨率,
  • 照明

考虑到合理的孔尺寸,经过适当的平滑处理后,霍夫圆形变换可能就足够了

资源1 资源2

代码如下:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import cv2
import numpy as np

def find_circles(img, min=85, max=100):
    
    equ = cv2.equalizeHist(img)

    k_smooth=9 # 9
    gauss = cv2.GaussianBlur(equ, (k_smooth, k_smooth), 0) # equ

    ret, otsu = cv2.threshold(gauss,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

    blur = cv2.medianBlur(otsu, 21)

    circles = cv2.HoughCircles(blur,cv2.HOUGH_GRADIENT,1,100,
                                param1=200,param2=15,minRadius=min,maxRadius=max)
    return circles

def draw(img, circles, counter):

    res = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    if circles is not None:
        idx = 1
        for i in circles[0,:]:
            # draw the outer circle
            cv2.circle(res,(i[0],i[1]),i[2],(0, 0, 255),3)
            # draw the center of the circle
            cv2.circle(res,(i[0],i[1]),2,(255, 0, 0), 15)
            res = cv2.putText(res, str(idx), (i[0]+100,i[1]+100), cv2.FONT_HERSHEY_COMPLEX, 4,  (255, 255, 255), 3, cv2.LINE_AA)
            idx+=1
    cv2.namedWindow("There are {} holes without bolts.\n".format(counter), cv2.WINDOW_NORMAL)
    cv2.imshow("There are {} holes without bolts.\n".format(counter),res)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

empty = cv2.imread("empty.jpg", 0)
full = cv2.imread("full.jpg", 0)

img = full

circles = find_circles(img)

if circles is not None:
    circles = np.uint16(np.around(circles))
    counter = len(circles[0,:])
else:
    counter = 0

draw(img, circles, counter)
于 2022-02-26T12:37:13.410 回答