Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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
Python 如何在OpenCV中找到T形的角度_Python_Opencv - Fatal编程技术网

Python 如何在OpenCV中找到T形的角度

Python 如何在OpenCV中找到T形的角度,python,opencv,Python,Opencv,我正在使用OpenCV 2.4进行一些跟踪,我可以得到我想要的形状的轮廓,这是一个T 输入图像: 我可以使用cv2.minareact(我的t\u轮廓)获得该矩形的角度,但这只能给我0-180度。但这是一个T形,所以我想知道0-360。我在想: 将轮廓拆分为两个矩形 通过矩形获取一条直线(使用skeletonize>HoughLinesP) 确定哪条线是哪条线,确定它们的梯度(使用我从HoughLinesP获得的坐标),然后确定T的方向 但是我被困在了第一位,我怎么能把一个轮廓分成两个形状呢

我正在使用OpenCV 2.4进行一些跟踪,我可以得到我想要的形状的轮廓,这是一个T

输入图像:

我可以使用
cv2.minareact(我的t\u轮廓)
获得该矩形的角度,但这只能给我0-180度。但这是一个T形,所以我想知道0-360。我在想:

  • 将轮廓拆分为两个矩形
  • 通过矩形获取一条直线(使用skeletonize>HoughLinesP)
  • 确定哪条线是哪条线,确定它们的梯度(使用我从HoughLinesP获得的坐标),然后确定T的方向
  • 但是我被困在了第一位,我怎么能把一个轮廓分成两个形状呢


    方法1:绘制轮廓中心和轮廓背面中心

    dst = cv2.cvtColor(r_target, cv2.COLOR_BGR2GRAY)
    dst = cv2.GaussianBlur(dst, (11, 11), 0)
    ret,dst = cv2.threshold(dst,110,255,cv2.THRESH_BINARY_INV)
    cnts = cv2.findContours(dst, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    for c in cnts:
        # get minAreaRect around contour and draw its center in red
        rect = cv2.minAreaRect(c)
        cv2.circle(r_target, (int(rect[0][0]), int(rect[0][1])), 7, (0, 0, 255), -1)
    
        # get moments of contour to get center and draw it in white
        M = cv2.moments(c)
        cX = int(M["m10"] / M["m00"])
        cY = int(M["m01"] / M["m00"])
        cv2.circle(r_target, (cX, cY), 7, (255, 255, 255), -1)
    

    下一步可能会计算中心之间的简单梯度,以确定角度


    方法2:使用HoughLinesP对图像进行骨架化并获取线条

    dst = cv2.cvtColor(r_target, cv2.COLOR_BGR2GRAY)
    dst = cv2.GaussianBlur(dst, (11, 11), 0)
    ret,dst = cv2.threshold(dst,110,255,cv2.THRESH_BINARY)
    dst = 1 - dst / 255
    dst = skimage.morphology.skeletonize(dst).astype(np.uint8)
    rho = 1
    theta = np.pi / 180
    threshold = 1
    minLineLength = 30
    maxLineGap = 15
    lines = cv2.HoughLinesP(dst, rho, theta, threshold, minLineLength=minLineLength, maxLineGap=maxLineGap)
    for line in lines[0]:
        cv2.line(r_target, (line[0], line[1]), (line[2], line[3]), (0, 255, 0), 1, 8)
    
    但是台词不太好。这是骨架的外观:


    我仍在试验变量,但使用HoughLinesP是否有特定的思维过程?

    作为变量,您可以使用PCA,找到第一个组件方向,并将其用作搜索角度。您可以在这里查看一个示例:

    您可能把这件事复杂化了。我将开始用质心/重心(检查“cv::矩”)检查Minareact中心的位置,找到使用基于hough变换的算法的矩形。相反,您可以运行形态学操作,只获取图像中字母T的骨架,然后检测它们之间的线条和角度。@Miki所以我画了轮廓的Minareact中心和轮廓本身的中心(请参见编辑),它们之间的关系看起来相当一致(在某些帧中,轮廓的中心除外),所以我所需要做的就是在中心之间获得一个梯度来表示角度。我不明白,为什么这样做?是因为minarealect只是一个近似值,但它和轮廓之间的关系总是一样的吗?@v.coder所以我使用了scikit图像对字母进行骨架化,但结果很奇怪(请参见编辑)。我希望它会吐出两行正确的=)表示T。我正在对阈值图像运行skeletonize函数,对吗?