Opencv如何查找轮廓(箭头)方向

2024-09-27 09:34:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样的形象 sample

带着一串箭。我想知道图像上大多数箭头的方向是什么。在

我读了一些opencv并想出了下面的代码

import numpy as np
import cv2
import math

img = cv2.imread('sample7.png')
height, width, channels = img.shape 
img = cv2.resize(img, (width*8, height*8))                    
img = cv2.medianBlur(img,5)
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)


ret,th1 = cv2.threshold(imgray,100,255,cv2.THRESH_BINARY)
edged=cv2.Canny(th1,127,200)


im2,contours,h = cv2.findContours(edged.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
(img2,cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
screenCnt=None
flag_t=False
flag_s=False

kot=[]

for c in cnts:
    approx = cv2.approxPolyDP( c, 0.01*cv2.arcLength(c,True), True )

    area = cv2.contourArea(c)
    #print area
    if int(len(approx)) > 8 and area > 600 and area < 1100: 
        anglelist=[]
        cv2.drawContours(img, [approx], -1, (0, 255, 255), 1)
        (x,y),(MA,ma),angle = cv2.fitEllipse(c)
        print round(angle,-1)
        kot.append(round(angle,-1))


d = {}
for elm in kot:
    d[elm] = d.get(elm, 0) + 1
counts = [(j,i) for i,j in d.items()]
count, max_elm = max(counts)


print 'most common:'
print max_elm
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

用我的脚本,我设法得到一些准确的结果。问题是我得到相同的结果,如果箭头是向上或向下,向左或向右。如何知道箭头是指向左边还是右边?在下面的两张图片中,我得到的结果都是一样的,大约90度。 enter image description hereenter image description here


Tags: inimportimgforarea箭头cv2max
1条回答
网友
1楼 · 发布于 2024-09-27 09:34:16

如果没有一些计算机视觉基础知识,这将是一项艰巨的任务。我的方法是用opencv读取图像,这会给您留下一个numpy数组。将其加载为灰度图像,并使用sobel过滤器之类的遮罩来检测边缘。更妙的是,精明的接线员。最好的办法是你先查一下它们是怎么工作的。有了这些,你可以检测出图像的边缘及其方向。你可以用它工作。另一个更高级(可能更好)的选择是使用hough变换,它能够检测图像中的线条(或任何其他几何对象)。这些都打包在opencv中,但是您可能需要了解它们是如何工作的。希望这有帮助。在

相关问题 更多 >

    热门问题