Estoy procesando las imágenes con OpenCV y Python. Necesito quitar los puntos/ruido de la imagen.
Probé la dilatación que hizo que los puntos fueran más pequeños, sin embargo, el texto se está dañando. También probé la dilatación en bucle dos veces y la erosión una vez. Pero esto no dio resultados satisfactorios.
¿Hay alguna otra manera en que pueda lograr esto?
Gracias :)
EDITAR:
Soy nuevo en el procesamiento de imágenes. Mi código actual es el siguiente
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))
EDITAR 2:
Probé el desenfoque de la mediana. Ha resuelto el 90% del problema. Había estado usando gaussianBlurring todo este tiempo.
Gracias
¿Qué hay de eliminar pequeños componentes conectados usando 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)
Y aquí está el ejemplo de 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);
EDITAR:
Ver también
Documentación de OpenCV para componentes conectados con estadísticas
Cómo usar los componentes conectados de openCV con estadísticas en python