pythonopencvimage-processingcomputer-visionscikit-image

Extracting the (size of the grid)features from an image


I am working with a image

Actualimage:

actualimage

I have used techniques like Canny, Hough transform to detect the line, and got this

Output:

transformed

Now I would like to extract the features like thickness size on each side in the grid cell indicated in the image. I need calculate area of specific

Part:

tobefind

Can you enlighten me on this?

# Dilate the edge image to make the edges thicker
   dialted_edge_image = cv2.dilate(edge_canny_image,kernel,iterations = 2)  

  # Perform a Hough Transform to detect lines
  lines = probabilistic_hough_line(edge_canny_image, threshold=40, line_length=1, line_gap=2)

  # Create a separate image for line detection
  line_detected_image = np.dstack([edge_canny_image] * 3)  # Convert to RGB for colored lines
   for line in lines:
    p0, p1 = line
    cv2.line(line_detected_image, p0, p1, (255, 255, 255), 2)  `

Solution

  • Okay, i am not sure with all the calculation you need but i can give you an approach to find the thicknesses of the sides.

    To be able to provide accuracy and stability for future calculations:

    1. You need to make sure the light levels of the image stays relatively constant with each different sample so that the inRange method gives proper filtering.
    2. I used spesific roi (region of interst) for thickness calculation for each edge, you need to make sure each brightedge stays inside corresponding roi for different images.

    I crop the image near the bright edges and since the thicknes changes accross the edge i measure it from different locations and calculate min max and average thickness. You may use the one suits your needs best.

    enter image description here

    and this is my code:

    import cv2
    
    def estimateThickness(img):
        #Determine if it is a vertical or a horizantal edege
        height,width = img.shape
        if height<=width:
            img = cv2.rotate(img,cv2.ROTATE_90_CLOCKWISE)
        height,width = img.shape
    
        #Estimate the thickness of sides from various locations
        #and extract min max average thickness
        thicknesess = []
        for nh in range(height//10):
            first,last = None,None
            for nw in range(width):
                # print(nl,ns)
                #Find the first white pixel on the direction 
                if img[10*nh][nw] == 255 and first is None:
                    first = nw
                #Find the last white pixel on the direction 
                if img[10*nh][width-nw-1] == 255 and last is None:
                    last = width-nw-1
                
                if first is not None and last is not None:
                    thicknesess.append(last-first)
    
        return max(thicknesess),min(thicknesess),sum(thicknesess)/len(thicknesess)
    
    
    #Read the image
    src_image = cv2.imread('img\grid.png')
    gray = cv2.cvtColor(src_image,cv2.COLOR_BGR2GRAY)
    
    
    #Extract the bright part in the image and filter the rest to measure thickness
    bright_part = cv2.inRange(gray,110,255)
    bright_part = cv2.morphologyEx(bright_part,cv2.MORPH_OPEN,cv2.getStructuringElement(cv2.MORPH_RECT,(3,3)))
    bright_part = cv2.morphologyEx(bright_part,cv2.MORPH_CLOSE,cv2.getStructuringElement(cv2.MORPH_RECT,(15,15)))
    
    #Crop top left bot and right edges from the filtered image
    left_edge = bright_part[200:500,180:280]
    right_edge = bright_part[200:500,750:850]
    top_edge = bright_part[20:120,400:700]
    bot_edge = bright_part[580:680,400:700]
    
    #Use the defined function with cropped image
    minL,maxL,avgL = estimateThickness(left_edge)
    minR,maxR,avgR = estimateThickness(right_edge)
    minT,maxT,avgT = estimateThickness(top_edge)
    minB,maxB,avgB = estimateThickness(bot_edge)
    
    print('L',minL,maxL,avgL)
    print('R',minR,maxR,avgR)
    print('T',minT,maxT,avgT)
    print('B',minB,maxB,avgB)
        
    cv2.imshow('L',left_edge)
    cv2.imshow('R',right_edge)
    cv2.imshow('T',top_edge)
    cv2.imshow('B',bot_edge)
    
    cv2.imshow('Bright Part',bright_part)
    cv2.imshow('Source',src_image)
    key = cv2.waitKey(0)
    

    For your other calculations if you can explain them further i can try to help you.