【问题标题】:Filtering noise from very noisy binary thresholded image从非常嘈杂的二进制阈值图像中过滤噪声
【发布时间】:2020-03-01 22:42:50
【问题描述】:

我希望能够分析以下图像,获取线条并找到平均宽度。(我的副本要大得多 ~5K 乘 ~4K)由于阈值处理后的所有噪声,无法进行下一步。

使用我的代码,我能够做到这一点......

我的问题是它在线条之间有很多噪音,看起来像是浓缩的噪音。

这是我的代码...

image = np.copy(origImg)
newImage = np.empty_like(image)

scale = 64

height = image.shape[0]
width = image.shape[1]

dH = int(height / scale)
dW = int(width / scale)

xi = int(dH)
yi = int(dW)

fragments = []
image = cv2.bilateralFilter(image,9,75,75)
image = cv2.medianBlur(image, 21)

for i in range(0,height,dH):
    for j in range(0,width,dW):
        fragment = image[i:i + int(dH), j:j + int(dW)]

        fragment = cv2.adaptiveThreshold(fragment, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 0)

        fragments.append(fragment)

analyzed = com.stackArrayToImage(fragments)

nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(analyzed, None, None, None, 8, cv2.CV_32S)
sizes = stats[1:, -1] 
img2 = np.zeros((labels.shape), np.uint8)

for i in range(0, nlabels - 1):
    if sizes[i] >= 100:  
        img2[labels == i + 1] = 255

analyzed = cv2.bitwise_not(img2)

analyzed = cv2.erode(analyzed, np.ones((5, 5)), iterations=2)
analyzed = cv2.dilate(analyzed, np.ones((5, 5), np.uint8))

dis.plotImages([origImg], "Origional")
dis.plotImages([analyzed], "Analyzed")
dis.displayStart() 

有什么办法可以消除这种噪音吗?

非常感谢!

【问题讨论】:

标签: python image opencv image-processing computer-vision


【解决方案1】:

您可以使用带有cv2.contourArea 的轮廓区域过滤来去除一些噪声。这个想法是使用一些阈值区域进行过滤。如果一个轮廓通过这个过滤器,那么我们可以通过用cv2.drawContours 填充轮廓来去除噪声。使用您的二进制图像作为输入:

检测到的轮廓以绿色突出显示

结果

根据你想要去除多少噪音,你可以调整阈值区域值

代码

import numpy as np
import cv2

# Load image, grayscale, Otsu's threshold
image = cv2.imread("1.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Find contours and filter using contour area
cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    if area < 50:
        cv2.drawContours(thresh, [c], -1, 0, -1)
        cv2.drawContours(image, [c], -1, (36,255,12), -1)

result = 255 - thresh
cv2.imshow("image", image) 
cv2.imshow("thresh", thresh) 
cv2.imshow("result", result) 
cv2.waitKey()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多