gpt4 book ai didi

python - 使用 cv2.connectedComponents 并剔除像素数少的元素

转载 作者:太空宇宙 更新时间:2023-11-03 21:32:31 27 4
gpt4 key购买 nike

我想使用函数 cv2.connectedComponents 来连接二进制图像上的组件,如下所示...

enter image description here

我已将该功能添加到 cv2。 connectedComponents 以消除具有少量像素的元素。

不幸的是,由于扩展,该算法对于大图像非常慢。有没有办法重写扩展以加速算法?

import cv2
import numpy as np

def zerolistmaker(n):
listofzeros = [0] * n
return listofzeros


img = cv2.imread('files/motorway/gabor/eGaIy.jpg', 0)

img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)[1] # ensure binary
retval, labels = cv2.connectedComponents(img)

##################################################
# ENLARGEMENT
##################################################
sorted_labels = labels.ravel()
sorted_labels = np.sort(sorted_labels)


maxPixel = 50 # eliminate elements with less than maxPixel

# detect how often an element occurs
i=0
counter=0
counterlist = zerolistmaker(retval)

while i < len(sorted_labels):
if sorted_labels[i] == counter:
counterlist[counter] = counterlist[counter] + 1
else:
counter = counter + 1
i = i - 1

i = i + 1


# delete small pixel values
i=0
while i < len(counterlist):
if counterlist[i] < maxPixel:
counterlist[i] = 0
i = i + 1

i=0
counterlisthelper = []
while i < len(counterlist):
if counterlist[i] == 0:
counterlisthelper.append(i)
i = i + 1

i=0
j=0
k=0
while k < len(counterlisthelper):
while i < labels.shape[0]:
while j < labels.shape[1]:
if labels[i,j] == counterlisthelper[k]:
labels[i,j] = 0
else:
labels[i,j] = labels[i,j]
j = j + 1
j = 0
i = i + 1
i = 0
j = 0
k = k + 1

##################################################
##################################################

# Map component labels to hue val
label_hue = np.uint8(179*labels/np.max(labels))
blank_ch = 255*np.ones_like(label_hue)
labeled_img = cv2.merge([label_hue, blank_ch, blank_ch])

# cvt to BGR for display
labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)

# set bg label to black
labeled_img[label_hue==0] = 0

cv2.imshow('labeled.png', labeled_img)
cv2.waitKey()

最佳答案

在 python 中,你应该避免深度循环。比 python-loop 更喜欢使用 numpy

改进:

##################################################
ts = time.time()
num = labels.max()

N = 50

## If the count of pixels less than a threshold, then set pixels to `0`.
for i in range(1, num+1):
pts = np.where(labels == i)
if len(pts[0]) < N:
labels[pts] = 0

print("Time passed: {:.3f} ms".format(1000*(time.time()-ts)))
# Time passed: 4.607 ms

##################################################

结果:

enter image description here enter image description here


完整代码:

#!/usr/bin/python3
# 2018.01.17 22:36:20 CST
import cv2
import numpy as np
import time

img = cv2.imread('test.jpg', 0)
img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)[1] # ensure binary
retval, labels = cv2.connectedComponents(img)

##################################################
ts = time.time()
num = labels.max()

N = 50
for i in range(1, num+1):
pts = np.where(labels == i)
if len(pts[0]) < N:
labels[pts] = 0

print("Time passed: {:.3f} ms".format(1000*(time.time()-ts)))
# Time passed: 4.607 ms

##################################################

# Map component labels to hue val
label_hue = np.uint8(179*labels/np.max(labels))
blank_ch = 255*np.ones_like(label_hue)
labeled_img = cv2.merge([label_hue, blank_ch, blank_ch])

# cvt to BGR for display
labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)

# set bg label to black
labeled_img[label_hue==0] = 0

cv2.imshow('labeled.png', labeled_img)
cv2.imwrite("labeled.png", labeled_img)
cv2.waitKey()

关于python - 使用 cv2.connectedComponents 并剔除像素数少的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48303309/

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