0

enter image description here

I tried these approaches but didn't get any real changes. actually, I am trying to build a handwritten OCR using Google cloud vision API. please suggest to me what can I do for preprocessing steps.

1.

image = cv2.fastNlMeansDenoisingColored(image, None, 10, 10, 7, 15)
    kernel  = np.ones((5, 5), np.uint8)
    image   = cv2.dilate(image, kernel, iterations = 1) 
    kernel  = np.ones((5, 5), np.uint8)
    image   = cv2.erode(image, kernel, iterations = 1)

2 Answers2

3

Another way is HSV color filter. Because you are using blue pen, so we can choice the color that we want. Sample code:

import cv2
import numpy as np

image = cv2.imread('9rS31.jpg')
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_green = np.array([100, 43, 20])
upper_green = np.array([130, 255, 255])
mask = cv2.inRange(hsv, lower_green, upper_green)
res = cv2.bitwise_and(image, image, mask=mask)

gray = cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)
ret, generator = cv2.threshold(gray, 1,255,cv2.THRESH_BINARY)

cv2.imwrite("img.jpg",generator)

Generated image: enter image description here

CodingPeter
  • 241
  • 2
  • 10
1

The noise is including horizontal line in your text book. So one method is using

cv2.getStructuringElement

You can find more information on the internet. Sample code:

import cv2

# Load image
image = cv2.imread('9rS31.jpg')
img=image.copy()

# Remove border
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50,1))
temp2 = 255 - cv2.morphologyEx(image, cv2.MORPH_CLOSE, horizontal_kernel)
result = cv2.add(temp2, image)

# Convert to grayscale and Otsu's threshold
gray = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(5,5),0)
_,thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)

cv2.imwrite('img.jpg',thresh)
cv2.imshow('img', thresh)
cv2.waitKey()

Generated image:enter image description here

CodingPeter
  • 241
  • 2
  • 10