gpt4 book ai didi

OpenCV - 检测特定宽度的组件

转载 作者:行者123 更新时间:2023-12-02 07:15:49 29 4
gpt4 key购买 nike

我有一张包含检测到的组件的图像。由此,我需要检测形成一定宽度的“折线”的组件(下图中的白色和红色)。

在 OpenCV 中什么算法最适合这个?我尝试过将所有组件一一分离并使用形态学操作,但速度相当慢且不完全准确。

注意:下面的图像经过下采样。原始图像分辨率为8K,边框厚度约为。 30-40 像素。

enter image description here

最佳答案

我喜欢你的问题 - 它有点像线条的粒度测量而不是颗粒。

我的方法是找到图像中独特的颜色,然后针对每种颜色:

  • 将该颜色隔离为黑底白字
  • 反复侵 eclipse 3个像素,直到什么都没有

请注意,下面 20-30% 的代码仅用于调试和解释,并且还可以通过多重处理和一些调整来加速。


#!/usr/bin/env python3

import cv2
import numpy as np
from skimage.morphology import medial_axis, erosion, disk

def getColoursAndCounts(im):
"""Returns list of unique colours in an image and their counts."""

# Make a single 24-bit number for each pixel - it's faster
f = np.dot(im.astype(np.uint32), [1,256,65536])
# Count unique colours in image and how often they occur
colours, counts = np.unique(f, return_counts=1)
# Convert found colours back from 24-bit number to BGR
return np.dstack((colours&255,(colours>>8)&255,colours>>16)).reshape((-1,3)), counts

if __name__ == "__main__":

# Load image and get colours present and their counts
im = cv2.imread('classes_fs.png',cv2.IMREAD_COLOR)
colours, counts = getColoursAndCounts(im)

# Iterate over unique colours/classes - this could easily be multi-processed
for index, colour in enumerate(colours):
b, g, r = colour
count = counts[index]
print(f'DEBUG: Processing class {index}, colour ({b},{g},{r}), area {count}')

# Generate this class in white on a black background for processing
m = np.where(np.all(im==[colour], axis=-1), 255, 0).astype(np.uint8)
# Create debug image - can be omitted
cv2.imwrite(f'class-{index}.png', m)

# DEBUG only - show progression of erosion
out = m.copy()

# You could trim the excess black around the shape here to speed up morphology

# Erode, repeatedly with disk of radius 3 to determine line width
radius = 3
selem = disk(radius)
for j in range(1,7):
# Erode again, see what's left
m = erosion(m,selem)
c = cv2.countNonZero(m)
percRem = int(c*100/count)
print(f' Iteration: {j}, nonZero: {c}, %remaining: {percRem}')
# DEBUG only
out = np.hstack((out, m))

if c==0:
break
# DEBUG only
cv2.imwrite(f'erosion-{index}.png', out)

因此,图像中的 35 种独特颜色一旦分离就会产生以下类别:

enter image description here

这是输出:

DEBUG: Processing class 0, colour (0,0,0), area 629800
Iteration: 1, nonZero: 390312, %remaining: 61
Iteration: 2, nonZero: 206418, %remaining: 32
Iteration: 3, nonZero: 123643, %remaining: 19
Iteration: 4, nonZero: 73434, %remaining: 11
Iteration: 5, nonZero: 40059, %remaining: 6
Iteration: 6, nonZero: 21975, %remaining: 3
DEBUG: Processing class 1, colour (10,14,0), area 5700
Iteration: 1, nonZero: 2024, %remaining: 35
Iteration: 2, nonZero: 38, %remaining: 0
Iteration: 3, nonZero: 3, %remaining: 0
Iteration: 4, nonZero: 0, %remaining: 0
...
...
DEBUG: Processing class 22, colour (174,41,180), area 3600
Iteration: 1, nonZero: 1501, %remaining: 41
Iteration: 2, nonZero: 222, %remaining: 6
Iteration: 3, nonZero: 17, %remaining: 0
Iteration: 4, nonZero: 0, %remaining: 0
DEBUG: Processing class 23, colour (241,11,185), area 200
Iteration: 1, nonZero: 56, %remaining: 28
Iteration: 2, nonZero: 0, %remaining: 0
DEBUG: Processing class 24, colour (247,23,185), area 44800
Iteration: 1, nonZero: 38666, %remaining: 86
Iteration: 2, nonZero: 32982, %remaining: 73
Iteration: 3, nonZero: 27904, %remaining: 62
Iteration: 4, nonZero: 23364, %remaining: 52
Iteration: 5, nonZero: 19267, %remaining: 43
Iteration: 6, nonZero: 15718, %remaining: 35
DEBUG: Processing class 25, colour (165,142,185), area 33800
Iteration: 1, nonZero: 30506, %remaining: 90
Iteration: 2, nonZero: 27554, %remaining: 81
Iteration: 3, nonZero: 24970, %remaining: 73
Iteration: 4, nonZero: 22603, %remaining: 66
Iteration: 5, nonZero: 20351, %remaining: 60
Iteration: 6, nonZero: 18206, %remaining: 53
DEBUG: Processing class 26, colour (26,147,198), area 2100
Iteration: 1, nonZero: 913, %remaining: 43
Iteration: 2, nonZero: 152, %remaining: 7
Iteration: 3, nonZero: 12, %remaining: 0
Iteration: 4, nonZero: 0, %remaining: 0
DEBUG: Processing class 27, colour (190,39,199), area 18500
Iteration: 1, nonZero: 6265, %remaining: 33
Iteration: 2, nonZero: 0, %remaining: 0
DEBUG: Processing class 28, colour (149,210,201), area 2200
Iteration: 1, nonZero: 598, %remaining: 27
Iteration: 2, nonZero: 0, %remaining: 0
DEBUG: Processing class 29, colour (188,169,216), area 10700
Iteration: 1, nonZero: 9643, %remaining: 90
Iteration: 2, nonZero: 8664, %remaining: 80
Iteration: 3, nonZero: 7763, %remaining: 72
Iteration: 4, nonZero: 6932, %remaining: 64
Iteration: 5, nonZero: 6169, %remaining: 57
Iteration: 6, nonZero: 5460, %remaining: 51
DEBUG: Processing class 30, colour (100,126,217), area 5624300
Iteration: 1, nonZero: 5565713, %remaining: 98
Iteration: 2, nonZero: 5511150, %remaining: 97
Iteration: 3, nonZero: 5464286, %remaining: 97
Iteration: 4, nonZero: 5420125, %remaining: 96
Iteration: 5, nonZero: 5377851, %remaining: 95
Iteration: 6, nonZero: 5337091, %remaining: 94
DEBUG: Processing class 31, colour (68,238,237), area 2100
Iteration: 1, nonZero: 1446, %remaining: 68
Iteration: 2, nonZero: 922, %remaining: 43
Iteration: 3, nonZero: 589, %remaining: 28
Iteration: 4, nonZero: 336, %remaining: 16
Iteration: 5, nonZero: 151, %remaining: 7
Iteration: 6, nonZero: 38, %remaining: 1
DEBUG: Processing class 32, colour (131,228,240), area 4000
Iteration: 1, nonZero: 3358, %remaining: 83
Iteration: 2, nonZero: 2788, %remaining: 69
Iteration: 3, nonZero: 2290, %remaining: 57
Iteration: 4, nonZero: 1866, %remaining: 46
Iteration: 5, nonZero: 1490, %remaining: 37
Iteration: 6, nonZero: 1154, %remaining: 28
DEBUG: Processing class 33, colour (0,0,255), area 8500
Iteration: 1, nonZero: 6046, %remaining: 71
Iteration: 2, nonZero: 3906, %remaining: 45
Iteration: 3, nonZero: 2350, %remaining: 27
Iteration: 4, nonZero: 1119, %remaining: 13
Iteration: 5, nonZero: 194, %remaining: 2
Iteration: 6, nonZero: 18, %remaining: 0
DEBUG: Processing class 34, colour (255,255,255), area 154300
Iteration: 1, nonZero: 117393, %remaining: 76
Iteration: 2, nonZero: 82930, %remaining: 53
Iteration: 3, nonZero: 51625, %remaining: 33
Iteration: 4, nonZero: 24842, %remaining: 16
Iteration: 5, nonZero: 6967, %remaining: 4
Iteration: 6, nonZero: 2020, %remaining: 1

如果我们看一下第 34 类 - 您感兴趣的类别。连续的侵 eclipse 看起来像这样 - 您可以看到形状完全消失,半径约为 15 像素,这对应于左侧丢失 15 像素,左侧丢失 15 像素30 像素宽形状右侧的像素:

enter image description here

如果您绘制每次连续腐 eclipse 后剩余像素的百分比,您可以很容易地看到 34 类和 25 类之间的差异,其中 34 类在每次 3 像素(即 15-18 像素)的 5-6 次腐 eclipse 后变为零。事实并非如此:

enter image description here

注释:

对于任何希望运行我的代码的人,请注意,我使用 ImageMagick 将输入图像(最近邻重采样)放大到当前大小的 10 倍:

magick classes.png -scale 1000%x classes_fs.png

关于OpenCV - 检测特定宽度的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62048079/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com