V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
woshichuanqilz
V2EX  ›  Python

opencv 识别表面 指针如何确定线段的长度就是每个指针的长度

  •  
  •   woshichuanqilz · 2021-04-20 16:03:33 +08:00 · 1104 次点击
    这是一个创建于 1101 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我用霍夫变换可以识别出来分针和时针, 但是我需要知道那条线更长, 这样才能知道那个是分针那个是时针。

    现在的问题是, 霍夫变换只是标注直线而不是线段, 所以这边没法知道那个指针更长, 这个应该怎么做?

    我用的测试图片 https://imgtu.com/i/c7ocLQ

    我现在用的代码

    import cv2
    import numpy as np
    
    img = cv2.imread('clock.png')
    h, w = img.shape[:2]
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray, 50, 150)
    
    lines = cv2.HoughLines(edges, 1, np.pi / 180, 100)
    
    for i in range(len(lines)):
        for rho, theta in lines[i]:
            a = np.cos(theta)
            b = np.sin(theta)
            x0 = a * rho
            y0 = b * rho
            x1 = int(x0 + w * (-b))
            y1 = int(y0 + w * (a))
            x2 = int(x0 - w * (-b))
            y2 = int(y0 - w * (a))
    
            cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
    cv2.imshow("edges", edges)
    cv2.imshow("lines", img)
    cv2.waitKey()
    cv2.destroyAllWindows() 
    
    1 条回复    2021-04-20 16:30:09 +08:00
    vincentV2
        1
    vincentV2  
       2021-04-20 16:30:09 +08:00
    HoughLinesP
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5366 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 07:21 · PVG 15:21 · LAX 00:21 · JFK 03:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.