从图像中提取(网格大小)特征

问题描述 投票:0回答:1

我正在处理图像

实际图像:

我使用了

Canny
Hough
变换等技术来检测线条,并得到了这个

输出:

现在我想提取图像中指示的网格单元中每一侧的厚度尺寸等特征。我需要计算具体面积

部分:

你能告诉我这个吗?

# 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)  `
python opencv image-processing computer-vision scikit-image
1个回答
0
投票

好吧,我不确定您需要的所有计算,但我可以为您提供一种找到边的厚度的方法。

能够为未来的计算提供准确性和稳定性:

  1. 您需要确保每个不同样本的图像亮度水平保持相对恒定,以便 inRange 方法提供适当的过滤。
  2. 我使用特定的 roi(感兴趣区域)来计算每个边缘的厚度,您需要确保每个明亮边缘位于不同图像的相应 roi 内。

我在明亮边缘附近裁剪图像,由于边缘的厚度发生变化,我从不同的位置测量它并计算最小最大厚度和平均厚度。您可以使用最适合您需求的一种。

这是我的代码:

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)

对于您的其他计算,如果您可以进一步解释它们,我可以尽力帮助您。

© www.soinside.com 2019 - 2024. All rights reserved.