user2155362
user2155362

Reputation: 1703

How to denoise the image

I'm building an application to recognize some pictures. Below is the example: enter image description here

Below is the code I used to convert the picture:

img = cv2.imread('9jxs.png', 0)
ret1, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
ret2, th2 = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
plt.figure()
plt.subplot(221), plt.imshow(img, 'gray')
plt.show()

Obviously, the result of the picture is "9JXS", my question is how can I delete the noise pix, clean the picture, then I can use OCR components to recognize

Upvotes: 0

Views: 366

Answers (2)

Stef
Stef

Reputation: 30579

I'm afraid there's no simple solution to this problem, otherwise capchas were useless. You can try to manually tweak the parameters of a range selection combined with some erosion, but I guess this will be very difficult to automate for the general case.

import cv2 as cv
import numpy as np

def on_lb_trackbar(val):
    global lb
    lb = val
    cv.imshow(title, cv.erode(cv.inRange(img,lb,ub),erosion_kernel))

def on_ub_trackbar(val):
    global ub
    ub = val
    cv.imshow(title, cv.erode(cv.inRange(img,lb,ub),erosion_kernel))

img = cv.imread(r'udilN.png',cv.IMREAD_GRAYSCALE)

lb = 0
ub = 255
title = 'Range selection'
erosion_kernel = np.ones((5,3), np.uint8)

cv.namedWindow(title)
cv.createTrackbar('from', title , lb, 255, on_lb_trackbar)
cv.createTrackbar('to', title , ub, 255, on_ub_trackbar)

on_lb_trackbar(0)

enter image description here

Upvotes: 0

lenik
lenik

Reputation: 23508

For this particular type of the picture, the lines seem to be much darker compared to the rest of the picture. I'd find the min() of the picture and replaced all pixels falling within 10% of minimal value with a mean, or better yet, median value of the neighbours.

Upvotes: 1

Related Questions