import cv2
import numpy as np
def merge_lines(lines,e0,e1):
merged_lines = []
for i,line in enumerate(lines):
if len(merged_lines) == 0:
merged_lines.append(line)
else:
merged = False
for i, merged_line in enumerate(merged_lines):
theta = line[1]
distan= line[0]
mtheta = merged_line[1]
mdistan= merged_line[0]
dtheta=abs(np.sin(theta))-abs(np.sin(mtheta))
ds=abs(abs(mdistan)-abs(distan))
if dtheta < e0 and ds < e1 :
merged_lines[i] = [(line[0] + merged_line[0]) / 2, (line[1] + merged_line[1]) / 2]
merged = True
break
if not merged:
merged_lines.append(line)
return merged_lines
def extend_lines(lines, img_shape):
extended_lines = []
for line in lines:
rho = line[0]
theta = line[1]
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))
extended_lines.append([(x1, y1), (x2, y2)])
return extended_lines
def find_intersections(lines):
intersections = []
for i in range(len(lines)):
for j in range(i+1, len(lines)):
line1 = lines[i]
line2 = lines[j]
x1, y1 = line1[0]
x2, y2 = line1[1]
x3, y3 = line2[0]
x4, y4 = line2[1]
denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
if denominator != 0:
x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / denominator
y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / denominator
intersections.append((int(x), int(y)))
return intersections
def draw_lines(img, lines):
for line in lines:
cv2.line(img, line[0], line[1], (0, 0, 255), 2)
def draw_intersections(img, intersections):
for intersection in intersections:
cv2.circle(img, intersection, 5, (0, 255, 0), -1)
def detect_lines(img, e0,e1,rho,threshold):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
lines= cv2.HoughLines(edges,rho, np.pi/180, threshold)
lines = lines[:, 0, :]
#print(lines)
merged_lines = merge_lines(lines, e0,e1)
extended_lines = extend_lines(merged_lines, img.shape)
intersections = find_intersections(extended_lines)
draw_lines(img, extended_lines)
draw_intersections(img, intersections)
return img
e0=10#角度
e1=10#距离
rho=2#像素偏差
threshold=260#投票数
cap = cv2.imread('jing.png')
while True:
img=cap.copy()
result=detect_lines(img, np.sin(e0/180*np.pi/2),e1,rho,threshold)
cv2.imshow('Result', result)
k=cv2.waitKey(1)
if k == ord("q") or k== 27:
break
cv2.destroyAllWindows()
本资源使用Python的机器视觉实用工具集来查找手绘棋盘直线交叉点的方法
48 浏览量
2024-01-05
11:23:25
上传
评论
收藏 143KB ZIP 举报
JAMES费
- 粉丝: 1w+
- 资源: 37
最新资源
- 详解protobuf-c之在C语言中如何使用repeated生成数组和字符串(包含配置pb-callback-t)
- Python 程序语言设计模式思路-并发模式:消费者模式:协调生产者和消费者之间的数据交换
- pythonA*算法(A-star algorithm),寻路算法
- guitest.zip
- udp_echo.v
- udp_echo_server.v
- python双向广度优先搜索算法(Bidirectional Breadth-First Search, BBFS),寻路算法
- python迭代加深算法(Iterative Deepening Depth-First Search, IDDFS),寻路算法
- 京东商品链接采集助手 2024版
- 基于Java的ApplicationPower快速项目生成脚手架设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈