计算轮廓上特定零件/边/线的坡度、长度和角度?

2024-09-27 04:25:37 发布

您现在位置:Python中文网/ 问答频道 /正文

Original Picture which is loaded

我在一个图像中得到了两个检测到的轮廓,需要顶部轮廓的两个垂直边缘之间的直径和下部轮廓的垂直边缘之间的直径。我用这个代码实现了这一点。在

import cv2
import numpy as np
import math, os
import imutils

img = cv2.imread("1.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)
edges = cv2.Canny(gray, 200, 100)
edges = cv2.dilate(edges, None, iterations=1)
edges = cv2.erode(edges, None, iterations=1)

cnts = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

# sorting the contours to find the largest and smallest one
c1 = max(cnts, key=cv2.contourArea)
c2 = min(cnts, key=cv2.contourArea)

# determine the most extreme points along the contours
extLeft1 = tuple(c1[c1[:, :, 0].argmin()][0])
extRight1 = tuple(c1[c1[:, :, 0].argmax()][0])
extLeft2 = tuple(c2[c2[:, :, 0].argmin()][0])
extRight2 = tuple(c2[c2[:, :, 0].argmax()][0])

# show contour
cimg = cv2.drawContours(img, cnts, -1, (0,200,0), 2)

# set y of left point to y of right point
lst1 = list(extLeft1)
lst1[1] = extRight1[1]
extLeft1 = tuple(lst1)

lst2 = list(extLeft2)
lst2[1] = extRight2[1]
extLeft2= tuple(lst2)

# compute the distance between the points (x1, y1) and (x2, y2)
dist1 = math.sqrt( ((extLeft1[0]-extRight1[0])**2)+((extLeft1[1]-extRight1[1])**2) )
dist2 = math.sqrt( ((extLeft2[0]-extRight2[0])**2)+((extLeft2[1]-extRight2[1])**2) )

# draw lines
cv2.line(cimg, extLeft1, extRight1, (255,0,0), 1)
cv2.line(cimg, extLeft2, extRight2, (255,0,0), 1)

# draw the distance text
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 0.5
fontColor = (255,0,0)
lineType = 1
cv2.putText(cimg,str(dist1),(155,100),font, fontScale, fontColor, lineType)
cv2.putText(cimg,str(dist2),(155,280),font, fontScale, fontColor, lineType)

# show image
cv2.imshow("Image", img)
cv2.waitKey(0)

Result Image

现在我还需要上轮廓底侧的斜率线的角度。在

Target Image Demo 1

你知道我怎么弄到这个吗?有可能使用轮廓吗?在

或者有必要使用HoughLinesP对相关行进行排序吗?在

接下来的问题是:也许也可以得到描述这一边的抛物线斜率的函数吗?在

Target Image Demo 2

非常感谢你的帮助!在


Tags: theimportimgcv2轮廓c2c1edges
1条回答
网友
1楼 · 发布于 2024-09-27 04:25:37

有几种方法可以得到坡度。为了知道斜率,我们可以使用cv2.HoughLines来检测底部水平线,检测到该线的端点,并从中得到斜率。作为一个例子

lines = cv2.HoughLines(edges, rho=1, theta=np.pi/180, threshold=int(dist2*0.66) )

在代码中的edges上给出4行,如果我们强制角度是水平的

^{pr2}$

我们得到:

enter image description here

对于与抛物线有关的扩展问题,我们首先构造一个返回左点和右点的函数:

def horizontal_scan(gray_img, thresh=50, start=50):
    '''
    scan horizontally for left and right points until we met an all-background line
    @param thresh: threshold for background pixel
    @param start: y coordinate to start scanning
    '''

    ret = []
    thickness = 0
    for i in range(start,len(gray_img)):
        row = gray_img[i]

        # scan for left:
        left = 0
        while left < len(row) and row[left]<thresh:
            left += 1

        if left==len(row):
            break;
        # scan for right:
        right = left
        while right < len(row) and row[right] >= thresh:
            right+=1

        if thickness == 0:
            thickness = right - left

        # prevent sudden drop, error/noise
        if (right-left) < thickness//5:
            continue
        else:
            thickness = right - left

        ret.append((i,left,right))
    return ret

# we start scanning from extLeft1 down until we see a blank line
# with some tweaks, we can make horizontal_scan run on edges, 
# which would be simpler and faster
horizontal_lines = horizontal_scan(gray, start = extLeft1[1])

# check if horizontal_line[0] are closed to extLeft1 and extRight1
print(horizontal_lines[0], extLeft1, extRight1[0])

注意,我们可以使用这个函数来查找HoughLines返回的水平线的端点。在

# last line of horizontal_lines would be the points we need:
upper_lowest_y, upper_lowest_left, upper_lowest_right = horizontal_lines[-1]

img_lines = img.copy()
cv2.line(img_lines, (upper_lowest_left, upper_lowest_y), extLeft1, (0,0,255), 1)
cv2.line(img_lines, (upper_lowest_right, upper_lowest_y), extRight1, (0,0,255),1)

因此:

enter image description here

让我们回到扩展的问题上,这里有左右两个点:

left_points = [(x,y) for y,x,_ in horizontal_lines]
right_points = [(x,y) for y,_,x in horizontal_lines]

显然,它们不能完美地拟合抛物线,所以我们需要某种近似/拟合。为此,我们可以建立一个线性回归模型:

from sklearn.linear_model import LinearRegression
class BestParabola:
    def __init__(self, points):
        x_x2 = np.array([(x**2,x) for x,_ in points])
        ys = np.array([y for _,y in points])

        self.lr = LinearRegression()
        self.lr.fit(x_x2,ys)
        self.a, self.b = self.lr.coef_
        self.c = self.lr.intercept_
        self.coef_ = (self.c,self.b,self.a)

    def transform(self,points):
        x_x2 = np.array([(x**2,x) for x,_ in points])
        ys = self.lr.predict(x_x2)

        return np.array([(x,y) for (_,x),y in zip(x_x2,ys)])     

{cd3>我们可以得到想要的抛物线:

# construct the approximate parabola
# the parabollas' coefficients are accessible by BestParabola.coef_
left_parabola = BestParabola(left_points)
right_parabola = BestParabola(right_points)


# get points for rendering
left_parabola_points = left_parabola.transform(left_points)
right_parabola_points = right_parabola.transform(right_points)

# render with matplotlib, cv2.drawContours would work
plt.figure(figsize=(8,8))
plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
plt.plot(left_parabola_points[:,0], left_parabola_points[:,1], linewidth=3)
plt.plot(right_parabola_points[:,0], right_parabola_points[:,1], linewidth=3, color='r')
plt.show()

它给出了:

enter image description here

左抛物线不是完美的,但是如果需要的话,你应该算出:-)

相关问题 更多 >

    热门问题