python opencv 多划痕的金属面板中光滑正方形磁块的检测

项目场景:

多划痕的金属面板中光滑正方形磁块的检测
python opencv 多划痕的金属面板中光滑正方形磁块的检测_第1张图片

问题描述

1.定位多粗糙金属面板中光滑正方形磁块的中心点并将其框出;
2.若正方形偏转角度大于标准值则给与提示;

解决方案:

'''
 * @Author: visual_eagle 
 * @Date: 2022-08-30 13:45:28 
 * @Last Modified by:   visual_eagle
 * @Last Modified time: 2022-08-30 13:45:28 
'''

import cv2
import numpy as np
def detection(inputimage):
    # cv2.imshow("inputimage",inputimage)
    
    #copy image
    copyimage = inputimage.copy()

    # BRG->GRAY
    grayimage = cv2.cvtColor(inputimage,cv2.COLOR_BGR2GRAY)
    # cv2.imshow("grayimage",grayimage)

    # GaussianBlur
    blurred = cv2.GaussianBlur(grayimage,(3,3),0)

    # canny
    canny = cv2.Canny(blurred, 20 ,30)
    # cv2.imshow("canny",canny)

    # thresh
    ret,thresh = cv2.threshold(canny,0,255,cv2.THRESH_BINARY)
    # cv2.imshow("thresh",thresh)

    # dilate
    kernel = np.ones(shape=[3,3],dtype=np.uint8)
    dilateimage = cv2.dilate(thresh, kernel,iterations=3)
    # cv2.imshow("dilateimage",dilateimage)

	# find contours
    contours = cv2.findContours(dilateimage,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    contours = contours[0] if len(contours) == 2 else contours[1]

	#save data list contour,x,y,w,h
    contour_list = []
    box_x_list=[]
    box_y_list=[]
    box_w_list=[]
    box_h_list=[]
    
    for contour in contours:    
        (x, y, w, h) = cv2.boundingRect(contour)
        if ((w<=(h+70) or w>=(h-70)) or (h<=(w+70) or h>=(w-70))) and w>=60 and w<=200 and h>=60 and h<=200:    
            # about the rect size
            # Not good rect
            if w*h>10000:
                cv2.rectangle(copyimage, (x, y), (x + w, y + h), (0, 0, 255), 2)

                rect = cv2.minAreaRect(contour)
                box = cv2.boxPoints(rect)
                box = np.int0(box)
                cv2.drawContours(copyimage,[box],0,(255, 0, 0),2)
            #Very good rect
            else:
                rect = cv2.minAreaRect(contour)
                box = cv2.boxPoints(rect)
                box = np.int0(box)
                cv2.drawContours(copyimage,[box],0,(0, 255, 0),2)
            

            #centrum point
            cv2.circle(copyimage,(int(x+w/2),int(y+h/2)),3,(0,0,255),-1)
            cv2.putText(copyimage,str(x)+","+str(y),(int(x+w/4),int(y+h/3)), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,255,0), 1, cv2.LINE_AA)

            contour_list.append(contour)

            box_x_list.append(x)
            box_y_list.append(y)
            box_w_list.append(w)
            box_h_list.append(h)

    print("box_x_list:",box_x_list)
    print("box_y_list:",box_y_list)
    print("box_w_list:",box_w_list)
    print("box_h_list:",box_h_list)

    # cv2.imshow('copyimage', copyimage)
    return copyimage

    
if __name__ == "__main__":
    imagepath = "202208311001.png"
    src = cv2.imread(imagepath)
    resultimage = detection(src)
    # cv2.imshow('resultimage', resultimage)
    images = np.hstack((src, resultimage))
    # Show images
    cv2.namedWindow('images', cv2.WINDOW_AUTOSIZE)
    cv2.imshow('images', images)
    #waitKey
    cv2.waitKey(0)
    cv2.destroyAllWindows()

输出图像



绿色框标出的则为合格方块,红色框标出的为不合格方块。

你可能感兴趣的:(opencv,python,opencv,python,人工智能)