python-3.x - 如何使用 OpenCV 查找图像中三角形的方向

标签 python-3.x opencv image-processing contour triangle

我正在尝试找到图像中三角形的方向。下面是图片:

enter image description here

这些三角形指向上/下/左/右。这不是真实的图像。我已经使用精明的边缘检测来查找边缘,然后找到轮廓,然后放大的图像如下所示。

我寻找方向的逻辑:

我想使用的逻辑是,在三个角坐标中,如果我可以识别三角形的基坐标(具有相同的横坐标或纵坐标值坐标),我可以制作一个基向量。然后可以使用单位向量和基向量之间的角度来识别方向。但该方法只能判断上下左右,无法区分上下左右。我尝试使用 cv2.goodFeaturesToTrack 找到角点,但据我所知,它只给出了整个图像中 3 个最有效的点。所以我想知道是否有其他方法可以找到三角形的方向。

这是我用 python 编写的代码,用于区分三角形/正方形和圆形:

#blue_masking
mask_blue=np.copy(img1)
row,columns=mask_blue.shape
for i in range(0,row):
    for j in range(0,columns):
        if (mask_blue[i][j]==25):
            mask_blue[i][j]=255
        else: 
            mask_blue[i][j]=0
blue_edges = cv2.Canny(mask_blue,10,10)
kernel_blue = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(2,2))
dilated_blue = cv2.dilate(blue_edges, kernel)
blue_contours,hierarchy = 
cv2.findContours(dilated_blue,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in blue_contours:
    area = cv2.contourArea(cnt)
    perimeter = cv2.arcLength(cnt,True)
    M = cv2.moments(cnt)
    cx = int(M['m10']/M['m00'])
    cy = int(M['m01']/M['m00'])
    if(12<(perimeter*perimeter)/area<14.8):
        shape="circle"
    elif(14.8<(perimeter*perimeter)/area<18):
        shape="squarer"
    elif(18<(perimeter*perimeter)/area and area>200):
        shape="triangle"
    print(shape) 
    print(area)
    print((perimeter*perimeter)/area,"\n")
    
cv2.imshow('mask_blue',dilated_blue)
cv2.waitKey(0)
cv2.destroyAllWindows()

  

源图像可以在这里找到:img1

请帮忙,我怎样才能找到三角形的方向?
谢谢。

最佳答案

假设您只有四种情况:[上、下、左、右],此代码应该适合您。

这个想法很简单:

  1. 获取轮廓的边界矩形。使用:box = cv2.boundingRect(contour_pnts)
  2. 使用边界矩形裁剪图像。
  3. 使用 Sum 垂直和水平缩小图像选项。现在您已获得沿每个轴的像素总和。和最大的轴决定三角形底边是垂直还是水平。
  4. 要识别三角形是指向左/右还是指向上/下:您需要检查边界矩形中心是在最大列/行之前还是之后:

代码(假设您从裁剪后的图像开始):

ver_reduce = cv2.reduce(img,  0, cv2.REDUCE_SUM, None, cv2.CV_32F)
hor_reduce = cv2.reduce(img,  1, cv2.REDUCE_SUM, None, cv2.CV_32F)

#For smoothing the reduced vector, could be removed
ver_reduce = cv2.GaussianBlur(ver_reduce, (3, 1), 0)
hor_reduce = cv2.GaussianBlur(hor_reduce, (1, 3), 0)

_,ver_max, _, ver_col = cv2.minMaxLoc(ver_reduce)
_,hor_max, _, hor_row = cv2.minMaxLoc(hor_reduce)

ver_col = ver_col[0]
hor_row = hor_row[1]

contour_pnts = cv2.findNonZero(img) #in my code I do not have the original contour points

rect_center, size,  angle = cv2.minAreaRect(contour_pnts )

print(rect_center)

if ver_max > hor_max:
    if rect_center[0] > ver_col:
        print ('right')
    else:
        print ('left')
else:
    if rect_center[1] > hor_row:
        print ('down')
    else:
        print ('up')

照片:

Up case

Right case

Down case

Left case

关于python-3.x - 如何使用 OpenCV 查找图像中三角形的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66953166/

相关文章:

python - 改进 Canny 边缘检测

python-3.x - 如何裁剪矩形或正方形内的图像区域?

c++ - Mat::at 的 C 包装器 - 错误:类型的非常量引用的初始化无效

python - 如何使用OPENCV有效地定位和保存具有特定值的像素?

python - 如何从彩色图像创建 CMYK 半色调图像?

python - 将文本换行与更改文本中的一个单词颜色相结合

c++ - 如何使openCV的solvePNP头部姿势估计输出更准确

Python 3.5 async for block ioloop

python 3.x 将字节数组写入sqlite3 blob字段

python-3.x - 有没有办法在每个特定的时间间隔运行 python Flask 函数并在本地服务器上显示输出?