检测零件的缝隙中是否有异物

主要思想是两张图相减,相同的部分会变为0,即图像为黑色。

 PlanA


import matplotlib.image as pltimg
import matplotlib.pyplot as plt

image = pltimg.imread("./flower.jpg", 1)
image_01 = image.copy()
sub_image = image - image_01
plt.imshow(sub_image)
plt.show()
# plt.waitforbuttonpress(0)

PlanB

image = cv2.imread("./flower.jpg", 1)
img = image.copy()
result = cv2.subtract(image, img)
cv2.imshow("res", result)
cv2.waitKey()

PlanC


import cv2
import numpy as np
import os


# 图像放射变换,与原图对齐
def get_Homography(src_img,template):


    surf = cv2.SIFT_create()
    # surf=cv2.xfeatures2d.SIFT_create()#可以改为SIFT
    kp1, descrip1 = surf.detectAndCompute(src_img, None)  # 特征点与描述子
    kp2, descrip2 = surf.detectAndCompute(template, None)

    if descrip1 is None:
        print("提取特征点错误")

    FLANN_INDEX_KDTREE = 0
    indexParams = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    searchParams = dict(checks=50)

    flann = cv2.FlannBasedMatcher(indexParams, searchParams)
    match = flann.knnMatch(descrip1, descrip2, k=2)


    direct = src_img.copy()

    good = []
    for i, (m, n) in enumerate(match):
        if (m.distance < 0.5 * n.distance):
            good.append(m)

    if len(good) > 5:
        src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
        ano_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)

        M, mask = cv2.findHomography(src_pts, ano_pts, cv2.RANSAC, 5.0)
        T = np.linalg.inv(M)
        # T[2, 0:3] = [0, 0, 1]
        warpImg = cv2.warpPerspective(template, T, (src_img.shape[1], src_img.shape[0]))
        direct = warpImg.copy()
        return True, direct
    else:
        return False, direct

def find_contour(src_yiwu,warpImage):
    subtracted1 = cv2.subtract(warpImage, src_yiwu)  # 将图像image与M相减
    # cv2.imshow("subtract", subtracted1)

    subtracted1 = 255 - subtracted1
    # cv2.imshow("subtracted1", subtracted1)

    kernel1 = np.ones((5, 5), dtype=np.uint8)
    kernel2 = np.ones((3, 3), dtype=np.uint8)
    gray = cv2.dilate(subtracted1, kernel1, 3)  # 1:迭代次数,也就是执行几次膨胀操作
    gray = cv2.erode(gray, kernel2, 1)

    # cv2.imshow("gray", gray)
    # cv2.waitKey()

    img_median = cv2.medianBlur(gray, 5)
    gray = cv2.erode(img_median, kernel2, 1)

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

    ret, thresh = cv2.threshold(gray, mean, 255, cv2.THRESH_BINARY)
    thresh = 255 - thresh

    # cv2.imshow("thresh", thresh)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    if len(contours) == 1:
        print("两次检测所用图相同")
        return False

    mianJi = []
    for k in range(len(contours)):
        mianJi.append(cv2.contourArea(contours[k]))
        # cv2.drawContours(src_yiwu, contours, k, (0, 0, 255), 4)

    # cv2.imshow("src_yiwutest", src_yiwu)
    # cv2.waitKey()

    h, w = src_yiwu.shape[:2]

    # 筛选超过平均面积的轮廓
    yiwu_is = []
    for i in range(len(contours)):
        area = cv2.contourArea(contours[i], False)
        if area > max(mianJi)/3 and area < max(mianJi)*1.01:

            yiwu_is.append(1)
            # print("轮廓%d的面积是: %d" % (i, area))
            # cv2.drawContours(src_yiwu, contours, i, (0, 0, 255), 5)
            # 绘制椭圆
            ellipse = cv2.fitEllipse(contours[i])
            cv2.ellipse(src_yiwu, ellipse, (0, 0, 255), 2)

            # cv2.imshow("contours %d" % i, src_yiwu)
            # cv2.waitKey()
        else:
            yiwu_is.append(0)

    cv2.imshow("result", src_yiwu)
    cv2.waitKey()

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

    if len(set(yiwu_is)) == 1 and 0 in yiwu_is:
        return False
    else:
        return True

def start_dect_yiwu(src_yiwuPath,templatePath):

    src_yiwu = cv2.imread(src_yiwuPath)  #
    template = cv2.imread(templatePath)
    # cv2.imshow("src_yiwu", src_yiwu)
    # image = src_image.copy()

    res_hom = get_Homography(src_yiwu,template)
    # print(res_hom[0])

    if res_hom[0] == True:
        warpImage = res_hom[1]
        is_yiwu = find_contour(src_yiwu,warpImage)
        if is_yiwu:
            print("检测到异物,产品不合格!")
            return "检测到异物,不合格"
        else:
            print("无异物,产品合格!")
            return "产品合格"
    else:
        print("无异物,产品合格!")
        return "产品合格"



if __name__ == "__main__":


    start_dect_yiwu('Images/yiwu/yiwureal_1101test.jpg','Images/yiwu/src_1101test.jpg')
    start_dect_yiwu('Images/yiwu/yiwureal_1101test01.jpg','Images/yiwu/src_1101test01.jpg')
    start_dect_yiwu('Images/yiwu/yiwureal_1101test02.jpg','Images/yiwu/src_1101test01.jpg')
    start_dect_yiwu('Images/yiwu/yiwureal_1101test03.jpg','Images/yiwu/src_1101test01.jpg')
 
    # # 如果是相同的图像
    start_dect_yiwu('Images/yiwu/yiwureale11.jpg','Images/yiwu/yiwureale11.jpg')
    start_dect_yiwu('Images/yiwu/src_1101test01.jpg','Images/yiwu/src_1101test01.jpg')
    start_dect_yiwu('Images/yiwu/yiwureal01.jpg','Images/yiwu/yiwureal01.jpg')

    # # #  # yiwu roi
    # img = cv2.imread("Images/Final/E_0_2_is.jpg")
    # # img_yiwu = img[750:1550, 2500:3750]
    # img_yiwu = img[700:1580, 2300:3700]
    #
    # cv2.imshow("img_yiwu", img_yiwu)
    # cv2.imwrite("Images/yiwu/yiwureal_e02test07.jpg",img_yiwu)
    # cv2.waitKey()
    #
    #
    # img = cv2.imread("Images/Final/E_0_2_no.jpg")
    # img_src = img[700:1580, 2300:3700]
    # cv2.imshow("img", img_src)
    # cv2.imwrite("Images/yiwu/src_e02test07.jpg",img_src)
    # cv2.waitKey()




#     src_yiwu = cv2.imread('Images/yiwu/yiwu.jpg')  #
#     template = cv2.imread('Images/yiwu/template.jpg')
#     AREA_YIWU = 1500
#     # cv2.imshow("src_yiwu", src_yiwu)
#     # image = src_image.copy()
#
#     res_hom = get_Homography(src_yiwu,template)
#
#     if res_hom[0] == True:
#         warpImage = res_hom[1]
#         is_yiwu = find_contour(src_yiwu,warpImage, AREA_YIWU)
#         if is_yiwu:
#             print("检测到异物,产品不合格!")
#         else:
#             print("无异物,产品合格!")
#     else:
#         print("无异物,产品合格!")


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值