高噪声二值阈值图像的噪声滤波

2024-05-21 02:14:42 发布

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

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

enter image description here

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

enter image description here

我的问题是,它在两行之间有很多噪音,看起来像是被压缩了的噪音

这是我的密码

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() 

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

多谢各位


Tags: imagefornpwidthcv2intdhheight
1条回答
网友
1楼 · 发布于 2024-05-21 02:14:42

您可以使用^{}的轮廓区域过滤来去除一些噪声。其思想是使用一些阈值区域进行过滤。如果一个轮廓通过这个滤波器,那么我们可以通过用^{}填充轮廓来去除噪声。使用二进制图像作为输入:

enter image description here

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

enter image description here

结果

enter image description here

根据要删除的噪波量,可以调整“阈值区域”值

代码

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()

相关问题 更多 >