# 均值滤波
dst_jz = cv2.blur(dst2,(9,9))
cv2.imshow("dst_sb", dst_jz)
# 高斯滤波
dst_gs = cv2.GaussianBlur(dst2,(5,5),0,0)
cv2.imshow("dst_sb", dst_gs)
# 双边滤波
dst_sb = cv2.bilateralFilter(dst2, 15, 155, 100)
cv2.imshow("dst_sb", dst_sb)
# 腐蚀
k = np.ones((9,9),np.uint8)
det_fs = cv2.erode(dst2,k)
cv2.imshow("det_fs", det_fs)
已知两条直线四个点(两个点构成一条直线),求这两条直线的交点坐标。
# 求直线方程的参数
def equation_parameters(x0, y0, x1, y1):
a = y0 - y1
b = x1 - x0
c = x0*y1 - x1*y0
return a, b, c
# 构造直线方程并求交点
def linear_equation_point(line1, line2):
a0, b0, c0 = equation_parameters(*line1)
a1, b1, c1 = equation_parameters(*line2)
D = a0 * b1 - a1 * b0
if D == 0:
return None
x = (b0 * c1 - b1 * c0) / D
y = (a1 * c0 - a0 * c1) / D
return x, y
if __name__ == '__main__':
line1 = [5, 5, 10, 10]
line2 = [9, 9, 10, 0]
cross_pt = linear_equation_point(line1, line2)
print(cross_pt)
"""
功能:导入图片,求两直线交点,再求直线距离
"""
import cv2
import numpy as np
import math
class MainWindow():
def __init__(self):
super().__init__()
img = cv2.imread("D:/Users/xuyf2/Desktop/project_C/cap_2/2cp1638667470.jpg")
img = img[357:517, 613:779]
# cv2.imwrite("D:/Users/xuyf2/Desktop/project_C/cap_2/" + '2' + ".jpg", img)
edges = self.image_processing(img) # 图像处理
line_zuobiao = self.line_detection(edges) # 寻找直线坐标
# 求交点
crossover_point1 = self.linear_equation_point(line_zuobiao[0], line_zuobiao[1])
crossover_point2 = self.linear_equation_point(line_zuobiao[1], line_zuobiao[2])
crossover_point3 = self.linear_equation_point(line_zuobiao[2], line_zuobiao[3])
crossover_point4 = self.linear_equation_point(line_zuobiao[3], line_zuobiao[4])
# print(list(crossover_point1),list(crossover_point2),list(crossover_point3),list(crossover_point4))
# 求长度
long1 = self.width_calculation(list(crossover_point1),list(crossover_point2))
long2 = self.width_calculation(list(crossover_point3), list(crossover_point4))
act_long1 = self.actual_width_jier(long1)
act_long2 = self.actual_width_jier(long2)
print('极耳像素点长度:',long1,long2)
print('极耳实际长度:', act_long1, act_long2)
# 画线
self.LineAtext1(img, crossover_point1, crossover_point2, crossover_point3, crossover_point4)
cv2.imshow("LineAtext1", img)
# 图像处理
def image_processing(self, img):
# 灰度处理
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Otsu方法
t2, dst2 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# 中值滤波
scr1 = cv2.medianBlur(dst2, 9)
cv2.imshow("3", scr1)
# 边缘检测
edges = cv2.Canny(scr1, 150, 250, L2gradient=True)
cv2.imshow("bianyuanjiance", edges)
return edges
def line_detection(self,img):
lines = cv2.HoughLinesP(img, 1, np.pi / 180, 30, minLineLength=30, maxLineGap=30)
zuobiaos = []
for line in lines:
x1, y1, x2, y2 = line[0]
zuobiao = [x1, y1, x2, y2]
zuobiaos.append(zuobiao)
print('直线检测出线段数:',len(zuobiaos))
# cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
# cv2.imshow("img1", img)
zuobiaos_new = sorted({tuple(x): x for x in zuobiaos[:]}.values())
# print(zuobiaos_new)
return zuobiaos_new
# 计算两点之间长度
def width_calculation(self,list_point1,list_point2):
p1 = np.array(list_point1)
p2 = np.array(list_point2)
p3 = p2 - p1
long = math.hypot(p3[0], p3[1])
long = ('%.3f' % long)
return long
# 求直线方程的参数
def equation_parameters(self, x0, y0, x1, y1):
a = y0 - y1
b = x1 - x0
c = x0*y1 - x1*y0
return a, b, c
# 构造直线方程并求交点
def linear_equation_point(self, line1, line2):
a0, b0, c0 = self.equation_parameters(*line1)
a1, b1, c1 = self.equation_parameters(*line2)
D = a0 * b1 - a1 * b0
if D == 0:
return None
x = (b0 * c1 - b1 * c0) / D
y = (a1 * c0 - a0 * c1) / D
return x, y
# 画线
def LineAtext1(self, image, a, b, c, d):
line_color = (0, 0, 255)
line_thin = 2
cv2.line(image, (int(a[0]),int(a[1])), (int(b[0]),int(b[1])), line_color, line_thin)
cv2.line(image, (int(c[0]),int(c[1])), (int(d[0]),int(d[1])), line_color, line_thin)
return image
def actual_width_jier(self, long):
width = (16.0 * float(long)) / 89.0
width = float('%.2f' % width)
return width
if __name__ == '__main__':
mainWindow = MainWindow()
cv2.waitKey(0)
cv2.destroyAllWindows()