Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cv2客户端和屏幕中心之间的距离-python_Python_Opencv_Hough Transform - Fatal编程技术网

cv2客户端和屏幕中心之间的距离-python

cv2客户端和屏幕中心之间的距离-python,python,opencv,hough-transform,Python,Opencv,Hough Transform,我使用openCV来检测两条线之间的距离以及它们相对于图像中心点的位置。不需要精确的距离-只需要某种像素的上下文值就可以了 我用来检测这两行代码的代码是: import PIL import time import io import picamera import cv2 import numpy as np image_count = 0 with picamera.PiCamera() as camera: camera.start_preview() camera.r

我使用openCV来检测两条线之间的距离以及它们相对于图像中心点的位置。不需要精确的距离-只需要某种像素的上下文值就可以了

我用来检测这两行代码的代码是:

import PIL
import time
import io
import picamera
import cv2
import numpy as np

image_count = 0

with picamera.PiCamera() as camera:
    camera.start_preview()
    camera.resolution = (340, 240)
    time.sleep(2)

while(True):
    try:
        stream = io.BytesIO()
        image_counter+=1
        camera.capture(stream, format='png')
        data = np.fromstring(stream.getvalue(), dtype=np.uint8)
        image = cv2.imdecode(data, 1)
        grey_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        edge_image = cv2.Canny(grey_image, 50, 150, apertureSize = 3)
        lines = cv2.HoughLines(edge_image, 1, np.pi/180, 95)
        if(lines.any):
            for rho, theta in lines[0]
                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(image, (x1, y1), (x2, y2), (0,0,255), 2)

            cv2.imwrite('lined_image_' + str(image_counter) + '.png, image)

    except:
        print 'loop error'
它检测像这幅图像中这样的线条

我一直在试图解决如何在数字上做到这一点,但这是复杂的,可能是错误的-一定有一个更简单的方法,但我看不到它与我的经验不足,使用开放式简历

如何找到图像中心点与您看到的最里面的红线之间的距离?在直线与水平线相交的点处,水平线与图像中心点相交

谢谢

如果要使用HoughLinesP,可以直接获得直线的起点和终点。那么,什么时候? Dx是x2-x1,Dy是y2-y1,从中心点x0到y0所需的距离d是

如果你想坚持使用HoughLines,你可以很容易地变换rho和θ来得到一条线的方程,并使用所描述的许多公式中的一个,这也是上述公式的来源