作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 OpenCV 和 Python 处理图像。我需要去除图像中的点/噪声。
我尝试了使点变小的膨胀,但是文本被损坏了。我还尝试了两次循环扩张和一次腐 eclipse 。但这并没有给出令人满意的结果。
有没有其他方法可以实现此目的?
谢谢:)
编辑:
我是图像处理的新手。我现在的代码如下
image = cv2.imread(file)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
kernel = np.ones((2, 2), np.uint8)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
gray = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
gray = cv2.erode(gray, kernel, iterations=1)
gray = cv2.dilate(gray, kernel, iterations=1)
cv2.imwrite(file.split('.'[0]+"_process.TIF", gray))
编辑 2:
我尝试了中值模糊。它已经解决了 90% 的问题。我一直在使用 gaussianBlurring。
谢谢
最佳答案
如何使用 connectedComponentsWithStats
移除小的连通分量
import cv2
import numpy as np
img = cv2.imread('path_to_your_image', 0)
_, blackAndWhite = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(blackAndWhite, None, None, None, 8, cv2.CV_32S)
sizes = stats[1:, -1] #get CC_STAT_AREA component
img2 = np.zeros((labels.shape), np.uint8)
for i in range(0, nlabels - 1):
if sizes[i] >= 50: #filter small dotted regions
img2[labels == i + 1] = 255
res = cv2.bitwise_not(img2)
cv2.imwrite('res.png', res)
这里是 C++ 示例:
Mat invBinarized;
threshold(inputImage, invBinarized, 127, 255, THRESH_BINARY_INV);
Mat labels, stats, centroids;
auto nlabels = connectedComponentsWithStats(invBinarized, labels, stats, centroids, 8, CV_32S, CCL_WU);
Mat imageWithoutDots(inputImage.rows, inputImage.cols, CV_8UC1, Scalar(0));
for (int i = 1; i < nlabels; i++) {
if (stats.at<int>(i, 4) >= 50) {
for (int j = 0; j < imageWithoutDots.total(); j++) {
if (labels.at<int>(j) == i) {
imageWithoutDots.data[j] = 255;
}
}
}
}
cv::bitwise_not(imageWithoutDots, imageWithoutDots);
编辑:
另见
OpenCV documentation for connectedComponentsWithStats
How to use openCV's connected components with stats in python
关于python - 如何在不损坏文本的情况下去除点/噪音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48681465/
我是一名优秀的程序员,十分优秀!