# 调整图像大小
size = (width, height)
img = cv2.resize(img_src, size, interpolation=cv2.INTER_CUBIC)
# 转化为HSV图像
HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# 提取红色区域gray = cv2.cvtColor(empty_img, cv2.COLOR_BGR2GRAY)
lines = cv2.HoughLinesP(gray, 1, np.pi / 180, int(width * 0.1), int(width * 0.5))
for line in lines:
[[x1, y1, x2, y2]] = line
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
# hough transform
lines = cv2.HoughLines(gray, 1, np.pi / 180, 160)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, (x1, y1), (x2, y2), (255, 0, 0), 1)
# 形态学操作
# kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
# 第二个参数:要执行的形态学操作类型,这里是bi操作
# filtered_area = cv2.morphologyEx(filtered_area, cv2.MORPH_CLOSE, kernel)
# filtered_area = cv2.morphologyEx(filtered_area, cv2.MORPH_CLOSE, kernel)
# cv2.imwrite("r_close.jpg", filtered_area)
# print("原始大小:%d - %d" % (width, height))
cv2.imwrite("r.jpg", img)
# 创建窗口并显示图像
cv2.namedWindow("Image")
cv2.imshow("Image", img)
cv2.waitKey(0)
# 释放窗口
cv2.destroyAllWindows()