python recognizes the component contour and obtains the maximum contour

Posted by DonelleJenae on Fri, 03 Dec 2021 17:57:35 +0100

          When working in the adhesive film company in the photovoltaic industry, the work of data sorting is monotonous and boring. How can a lazy manufacturing industry who advocates automatic office stand it?!

        The screenshot of the EL test results of photovoltaic modules mentioned in this paper is one of them. The specific content is to save the screenshot of the luminous part in the picture. A single picture is very simple, but it will take a lot of time to increase the number. We must find a way to solve it. Even if we save an hour a day to touch the fish with the mobile phone, it is good~~~

         

         I happen to order python. I found that it can realize contour recognition in my CSDN forum. I can change it myself. There is still a little bonus for reporting to the company~~~

        Here is a brief introduction to the core functions:

        1. Due to the relationship of EL instrument, the test result will be a large luminous area and many fuzzy light spots. Through the CV2 contour recognition method, a list corresponding to a series of luminous contours will be obtained, and the test result must be the one with the largest luminous contour. The following idea is also based on this.

import cv2    
ret, thresh = cv2.threshold(gray.copy(), f, 255, cv2.THRESH_BINARY)
    contours,hierarchy= cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        2. Traverse the luminous contour list contours obtained above to obtain the size of each luminous area newimage.size, and form a one-to-one correspondence list piclist for subsequent identification.

    #piclist outline list
    piclist=[]
    for i in range(0,len(contours)):  
        x, y, w, h = cv2.boundingRect(contours[i])   
        newimage=gray[y+2:y+h-2,x+2:x+w-2] # First determine the height with y, and then determine the width with x
        #Contour slice the original image, obtain the image size, and add it to the list to eliminate unreasonable contours
        piclist.append(newimage.size)

        3. Identify the largest item in the piclist and obtain the corresponding index, then extract the area from the contents and save the screenshot. Because there will be a short circuit of the battery in the EL test, that is, there will be no luminous area, so there will be if newimage. Size= 0 this judgment.

The following is all the code of the core function.

def singleCut(inpath,fileName,outpath):
    #Start screenshot timing
    start = time.process_time()
    #Read picture
    img = cv_imread(inpath)
    
    #Convert to grayscale
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    #Setting the threshold is open to discussion
    f=(2*max(gray[2])+3*min(gray[2]))/4#Get component outline
    ret, thresh = cv2.threshold(gray.copy(), f, 255, cv2.THRESH_BINARY)
    contours,hierarchy= cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    #piclist outline list
    piclist=[]
    for i in range(0,len(contours)):  
        x, y, w, h = cv2.boundingRect(contours[i])   
        newimage=gray[y+2:y+h-2,x+2:x+w-2] # First determine the height with y, and then determine the width with x
        #Contour slice the original image, obtain the image size, and add it to the list to eliminate unreasonable contours
        piclist.append(newimage.size)
    
    #Key step: obtain the slice with the largest picture size in the outline list, that is, the component luminous area in EL
    flag=True
    m=0  #test
    for i in range(0,len(contours)):  
        m=m+1
        if i==piclist.index(max(piclist)):
            x, y, w, h = cv2.boundingRect(contours[i])    
            #Slice and save results
            newimage=img[y+2:y+h-2,x+2:x+w-2] # First determine the height with y, and then determine the width with x   
            if newimage.size!=0:
                cv_imwrite( outpath+fileName+".jpg",newimage)
                #cv_imwrite( outpath+"\{}-{}-{}".format(m,max(gray[3]),min(gray[3]))+".jpg",newimage)
            else:
                flag=False
    #End timing
    end = time.process_time()
    print("Screenshot time consuming{}second".format("%.2f"%(end-start)))
    return flag

I also finished after searching many articles of the great God of CSDN forum, so I hope I can help later people. After all, my Python development environment is so friendly~~~~

Topics: Python OpenCV Computer Vision