计算轮廓上特定零件/边/线的斜率,长度和角度?

时间:2019-01-03 21:37:55

标签: python opencv image-processing angle

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

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

有几种方法可以仅获取斜率。为了知道斜率,我们可以使用cv2.HoughLines来检测底部的水平线,检测到该线的端点,然后从中获得斜率。作为说明,

lines = cv2.HoughLines(edges, rho=1, theta=np.pi/180, threshold=int(dist2*0.66) )
您的代码中edges上的

给出4行,如果我们强制将角度设为水平

for line in lines:
    rho, theta = line[0]

    # here we filter out non-horizontal lines
    if abs(theta - np.pi/2) > np.pi/180:
        continue

    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho
    y0 = b*rho
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a))

    cv2.line(img_lines,(x1,y1),(x2,y2),(0,0,255),1)

我们得到:

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]

很显然,它们不能完美地适合抛物线,因此在这里我们需要某种近似/拟合。为此,我们可以构建一个LinearRegression模型:

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)])     

然后,我们可以拟合给定的left_points, right_points以获得所需的抛物线:

# 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

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