c# - 在opencv中检测一个半完整的椭圆
解决方案
您可以通过找到区域的凸包然后在 Python/OpenCV 中为其拟合椭圆来做到这一点。
输入:
import cv2
import numpy as np
# read image
img = cv2.imread('half_ellipses.jpg')
hh, ww = img.shape[:2]
print(hh,ww)
# convert to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# threshold to binary and invert
thresh = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)[1]
thresh = 255 - thresh
# get convex hull of white pixels
points = np.column_stack(np.where(thresh.transpose() > 0))
hull = cv2.convexHull(points)
((centx,centy), (width,height), angle) = cv2.fitEllipse(hull)
print ((centx,centy), (width,height), angle)
# draw polygon
result = img.copy()
cv2.ellipse(result, (int(centx),int(centy)), (int(width/2),int(height/2)), angle, 0, 360, (0,0,255), 2)
cv2.imshow('image', img)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
# save results
cv2.imwrite('half_ellipses_hull_ellipse.png', result)
结果:
推荐阅读
- dart - 接受对象作为函数
- intellij-idea - Intellij IDEA - 多次打印事件日志
- javascript - Firebase - 在验证电子邮件之前冻结帐户
- javascript - 如何将用户凭据发布到外部域?
- html - SQLSTATE [42S22]:找不到列:1054 'where 子句'中的未知列'user_id'
- java - Spring Tomcat JDBC 连接池 - 如何在数据库脱机时恢复
- reactjs - create-react-app 占用过多磁盘空间和时间
- ruby - 收集和比较数组内的 ruby 哈希
- javascript - 三个(几乎?)相同的函数返回不同的结果
- powershell - 在 Windows Base Jenkins Pipeline 中注入环境并运行 powershell 脚本