OpenCV:识别图像的各个部分

2024-10-02 04:30:22 发布

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

我试图确定下图的主要部分。我希望找到突出显示的6个部分。 我尝试使用模糊/扩张/侵蚀的组合并应用findCountures(),但无法将这些主要部分作为单个单元

有人能建议最好的方法吗。我包括代码,可以产生图像以及

import cv2
import numpy as  np

def createImage():
    points = [
        [(0, 8), (5, 8)],        [(5, 8), (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, 81)],        [(55, 81), (60, 84)],
        [(60, 84), (65, 104)],        [(65, 104), (70, 151)],        [(70, 151), (75, 151)],
        [(75, 151), (80, 159)],        [(80, 159), (85, 191)],        [(85, 191), (90, 193)],
        [(90, 193), (95, 230)],        [(95, 230), (100, 230)],        [(100, 230), (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, 104)],        [(160, 104), (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


Tags: sample方法代码图像importnumpyimgas
1条回答
网友
1楼 · 发布于 2024-10-02 04:30:22

一种方法是使用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

相关问题 更多 >

    热门问题