OpenCV:识别图像的一部分

问题描述

我正在尝试识别下图的主要部分。我正在寻找突出显示的6个部分。 我尝试使用模糊/扩张/腐蚀的组合,并应用了findCountures(),但无法将这些主要部件作为一个单元来获得。

谁能建议最好的方法来做到这一点。我包括了可以生成图像的代码

import cv2
import numpy as  np

def createImage():
    points = [
        [(0,8),(5,8)],[(5,(10,12)],[(10,12),(15,26)],[(15,26),(20,56)],[(20,56),(25,82)],[(25,82),(30,102)],[(30,102),(35,129)],[(35,129),(40,100)],[(40,100),(45,81)],[(45,81),(50,80)],[(50,80),(55,[(55,(60,84)],[(60,84),(65,104)],[(65,104),(70,151)],[(70,151),(75,[(75,(80,159)],[(80,159),(85,191)],[(85,191),(90,193)],[(90,193),(95,230)],[(95,230),(100,[(100,(105,248)],[(105,248),(110,224)],[(110,224),(115,199)],[(115,199),(120,170)],[(120,170),(125,130)],[(125,130),(130,101)],[(130,101),(135,69)],[(135,69),(140,61)],[(140,61),(145,59)],[(145,59),(150,62)],[(150,62),(155,85)],[(155,85),(160,[(160,(165,117)],[(165,117),(170,89)],[(170,89),(175,71)],[(175,71),(180,43)],[(180,43),(185,21)]
    ]
    img = np.zeros([256,256],dtype=np.uint8)

    for p in points:
        cv2.line(img,p[0],p[1],255,1)

    cv2.imwrite("sample.png",img)
    return img

img  =createImage()
cv2.imshow("sample",img)
cv2.waitKey(0)

picture_with_highligts

original_picture

解决方法

一种方法是使用FastLineDetector

如果将FastLineDetector应用于输入图像,结果将是:

enter image description here

让我们看看结果如何:

    1. 实施FastLineDetector

import cv2

image = cv2.imread("KZd2a.png")
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
d = cv2.ximgproc.createFastLineDetector()
lines = d.detect(gray)
    1. 画线

r = d.drawSegments(i,lines)

但是,如果我们进行分析,则会在图像上绘制20条线。

for line in lines:
    (x1,y1,x2,y2) = [i for i in line[0]]
    cv2.line(i,pt1=(x1,y1),pt2=(x2,y2),color=(255,255,0),thickness=4)

步骤:


  • enter image description here