利用opencv实现十字线中心点的识别 - img_gray = cv2
利用opencv实现十字线中心点的识别
一、效果展示
本文展示了一种利用opencv实现十字像的中心点识别的方法
十字线图像
十字线中心识别结果
二、 主要思路
1.图片灰度化与二值化
首先读取图片,然后对待处理的图片进行常规的灰度化与二值化处理
img = cv2.imread('E:\\test.png')
#在此处自行确定图片所在路径
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, im2 = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
plt.imshow(img_gray, cmap = 'gray')
2.对图片进行Canny边缘检测识别
利用Canny算子对图片进行边缘检测 边缘检测结果
#边缘检测
edges = cv2.Canny(gim2, 45, 135)
plt.imshow(edges, cmap='gray')
三、利用霍夫变换识别图像中的直线
直接利用Hough变换可以识别图像中的直线部分,通过阈值调节直线数量
lines = cv2.HoughLines(edges, 1, np.pi/180, 120)
#上面的120表示阈值像素个数,线段要长于这个值才会被识别
lines = [line[0] for line in lines.tolist()]
显示图像与直线
if lines is not None:
for line in lines:
rho, theta = line
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, (x1, y1), (x2, y2), (0, 255, 0), 1)
显示结果
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
四、直线交点识别
遍历霍夫变换识别到的有效直线,对任意两条倾角相差大于某阈值的直线求交点(倾角相差较小的直线往往是直线识别算法导致的伪线) 任意两条直线的交点计算可以避免倾斜十字对交点识别的影响,无需进行图像旋转
def IntersectionPoints(lines):
#求交点的函数
points = []
for l1 in lines:
for l2 in lines:
if((np.abs(l1[1] - l2[1]) > 10*np.pi/180)&(l1 != l2)):
a = np.array([
[np.cos(l1[1]), np.sin(l1[1])],
[np.cos(l2[1]), np.sin(l2[1])]
])
b = np.array(
[[l1[0]],
[l2[0]]]
)
points.append(np.linalg.solve(a, b))
return points
points = IntersectionPoints(lines)
五、多个交点平均
通过多个交点进行平均求最终的交点位置,并对原图片进行裁剪以清晰显示交点位置
for point in points:
cv2.circle(img, (int(point[0]), int(point[1])), 1, (0, 0, 255))
midx = np.mean([point[0] for point in points])
midy = np.mean([point[1] for point in points])
cv2.circle(img, (int(midx), int(midy)), 1, (0,0,255), -1)
print('(' + str(midx) + ', ' + str(midy) + ')')
#裁剪图像,只显示中间一百像素
height, width, _ = img.shape
# 定义裁剪区域(中心部分)
crop_size = 100 # 设定裁剪区域的尺寸(例如 100x100)
start_x = width // 2 - crop_size // 2
start_y = height // 2 - crop_size // 2
# 裁剪图像
cropped_img = img[start_y:start_y + crop_size, start_x:start_x + crop_size]
plt.figure(1)
plt.imshow(cropped_img)
plt.figure(2)
plt.imshow(img)
裁剪后的图片 裁剪前的图片
六、完整代码
import cv2
import numpy as np
import matplotlib.pyplot as plt
def IntersectionPoints(lines):
#求交点的函数
points = []
for l1 in lines:
for l2 in lines:
if((np.abs(l1[1] - l2[1]) > 10*np.pi/180)&(l1 != l2)):
a = np.array([
[np.cos(l1[1]), np.sin(l1[1])],
[np.cos(l2[1]), np.sin(l2[1])]
])
b = np.array(
[[l1[0]],
[l2[0]]]
)
points.append(np.linalg.solve(a, b))
return points
img = cv2.imread('E:\\0000pycode\\MTF\\CrosshairIntersectionIdentification\\heise.jfif')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, im2 = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
plt.imshow(img_gray, cmap = 'gray')
gimg = cv2.GaussianBlur(img_gray, (5, 5), 0)
gret, gim2 = cv2.threshold(gimg, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
plt.imshow(gim2, cmap = 'gray')
edges = cv2.Canny(gim2, 45, 135)
plt.imshow(edges, cmap='gray')
minLineLength = 10
maxLineGap = 5
lines = cv2.HoughLines(edges, 1, np.pi/180, 120)
#上面的120表示阈值,线段要长于这个值才会被识别
lines = [line[0] for line in lines.tolist()]
#lines = cleanlines(lines)
points = IntersectionPoints(lines)
if lines is not None:
for line in lines: 拼多多帮助力入口
rho, theta = line
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, (x1, y1), (x2, y2), (0, 255, 0), 1)
# 显示结果
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
for point in points:
cv2.circle(img, (int(point[0]), int(point[1])), 1, (0, 0, 255))
midx = np.mean([point[0] for point in points])
midy = np.mean([point[1] for point in points])
cv2.circle(img, (int(midx), int(midy)), 1, (0,0,255), -1)
print('(' + str(midx) + ', ' + str(midy) + ')')
#裁剪图像,只显示中间一百像素
height, width, _ = img.shape
# 定义裁剪区域(中心部分)
crop_size = 100 # 设定裁剪区域的尺寸(例如 100x100)
start_x = width // 2 - crop_size // 2
start_y = height // 2 - crop_size // 2
# 裁剪图像
cropped_img = img[start_y:start_y + crop_size, start_x:start_x + crop_size]
plt.figure(1)
plt.imshow(cropped_img)
plt.figure(2)
plt.imshow(img)
利用opencv实现十字线中心点的识别
参考文章