python - 计算图像中对象的中心
问题描述
我正在阅读这篇文章来计算使用OpenCV
which uses的图像的中心Moments
。但我正在尝试计算使用 HoughLinesP 检测到的对象的中心。OpenCV有没有办法我可以做到这一点?
这是我试图计算中心的图像。
找到了线段,输出图像如下所示:
import cv2
import numpy as np
import math
img = cv2.imread("./images/octa.jpg")
b,g,r = cv2.split(img)
smoothed = cv2.GaussianBlur(g, (3,3), 0)
edges = cv2.Canny(smoothed, 15, 60, apertureSize = 3)
lines = cv2.HoughLinesP(edges,1,np.pi/180,35, 30, 20)
print("length of lines detected ", lines.shape)
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(img,(x1,y1),(x2,y2),(255,0,0),2)
print("x1,y1", x1,",",y1, " --- ", "x2,y2", x2,",",y2)
cv2.imshow('detected',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
使用坐标如何计算该图像的中心?我怎么能Moments
在这里使用?
我的一个限制是我不能Contour
使用OpenCV
.
解决方案
以下代码cv2
与3.3.1
.
我密切关注opencv 文档,它运行良好。
import cv2
img = cv2.imread("octa.jpg", 0)
ret,thresh = cv2.threshold(img,100,255,0)
im2, contours, hierachy = cv2.findContours(thresh, 1, 2)
cnt = contours[0]
M = cv2.moments(cnt)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
im2 = cv2.cvtColor(im2, cv2.COLOR_GRAY2RGB)
cv2.polylines(im2, cnt, True, (0, 0, 255), 2)
cv2.circle(im2, (cx, cy), 5, (0, 0, 255), 1)
cv2.imshow("res", im2)
两个注意事项:
- 您需要添加参数
0
,imread
否则轮廓查找将不起作用 - 我将阈值设置得稍微低一点,所以只找到了八边形的轮廓
结果:
如果您使用不同版本的cv2
,您只需将文档更改为您的版本即可;文档真的很好。
您可能还想稍微模糊您的图像或进行一些其他预处理,但在这种情况下,没有必要这样做。
编辑没有轮廓:
我从这篇文章中获得了有用的评论,并进行了一些修改。这不使用轮廓。它找到线并使用它们来找到中心
import cv2
import numpy as np
mg = cv2.imread('octa.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
kernel_size = 5
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
ret,thresh = cv2.threshold(blur_gray,100,255,0)
low_threshold = 50
high_threshold = 150
edges = cv2.Canny(thresh, low_threshold, high_threshold)
rho = 1 # distance resolution in pixels of the Hough grid
theta = np.pi / 180 # angular resolution in radians of the Hough grid
threshold = 15 # minimum number of votes (intersections in Hough grid cell)
min_line_length = 50 # minimum number of pixels making up a line
max_line_gap = 50 # maximum gap in pixels between connectable line segments
line_image = np.copy(img) * 0 # creating a blank to draw lines on
# Run Hough on edge detected image
# Output "lines" is an array containing endpoints of detected line segments
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
min_line_length, max_line_gap)
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),2)
lines_edges = cv2.addWeighted(img, 0.5, line_image, 1, 0)
line_image_gray = cv2.cvtColor(line_image, cv2.COLOR_RGB2GRAY)
M = cv2.moments(line_image_gray)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
cv2.circle(lines_edges, (cx, cy), 5, (0, 0, 255), 1)
cv2.imshow("res", lines_edges)
推荐阅读
- android-workmanager - 当 onStopped 是 final 时,如何在我的 CoroutineWorker 中执行清理代码?
- java - 使用 Hamcrest 检查某些元素是否在集合中
- python - 游戏;Surface 上的 Alpha gestion 问题
- php - 除日期外,将 PHP 生成的 DataPeriod 列表拆分为月和年
- ios - 从另一个 ViewController 中删除子视图
- arrays - Delphi 数组与对象
- javascript - 正确测试 Vue 组件中的事件处理程序
- javascript - Vue JS:从输入中重置/清除值
- java - 如何在多个微服务中管理安全上下文
- java - 如何使 IntellijIDEA 与 Java 12 一起工作