0% found this document useful (0 votes)
3 views7 pages

Preprocessing Task

The document provides a Python script that utilizes the Tesseract OCR library to preprocess an image for text extraction. It includes steps for loading an image, converting it to grayscale, applying binarization, noise removal, morphological operations, and deskewing before extracting text. The output consists of the processed text extracted from the image after these preprocessing tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Preprocessing Task

The document provides a Python script that utilizes the Tesseract OCR library to preprocess an image for text extraction. It includes steps for loading an image, converting it to grayscale, applying binarization, noise removal, morphological operations, and deskewing before extracting text. The output consists of the processed text extracted from the image after these preprocessing tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

NAME: RAVULA SHIVA KUMAR GMAIL: ravula.shivakumar11@gmail.

com

Preprocessing Task
Source code :
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\


tesseract.exe"
import cv2
import numpy as np
from PIL import Image

# Load the image


image_path = "C:/Users/ravul/OneDrive/Desktop/p24.jpg"
image = cv2.imread(image_path)
# Convert OpenCV image to PIL format
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# Perform OCR
text = pytesseract.image_to_string(pil_image)
print(text)
#grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Grayscale", gray)
cv2.waitKey(0)
#binarizarion
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
[1]
cv2.imshow("Thresholded", thresh)
cv2.waitKey(0)
#noice Removal
denoised = cv2.fastNlMeansDenoising(thresh, None, 30, 7, 21)
cv2.imshow("Denoised", denoised)
cv2.waitKey(0)
#Morphological Operations
kernel = np.ones((1, 1), np.uint8)
morph = cv2.morphologyEx(denoised, cv2.MORPH_CLOSE, kernel, iterations=1)
cv2.imshow("Morphological", morph)
cv2.waitKey(0)
#Deskewing (Correcting Skewed Text)
# Deskewing (Correcting Skewed Text)
coords = np.column_stack(np.where(thresh > 0))
rect = cv2.minAreaRect(coords)
angle = rect[-1]
if angle < -45:
angle += 90
elif angle > 45:
angle -= 90
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
deskewed = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC,
borderMode=cv2.BORDER_REPLICATE)
cv2.imshow("Deskewed Image", deskewed)
cv2.waitKey(0)
cv2.destroyAllWindows()
#Extract Text After Preprocessing
processed_text = pytesseract.image_to_string(deskewed)
print(processed_text)

Output:
Input image:
Grayscale image
Binarization
noise Removal:
Morphological Operations

Extracted Text After Preprocessing:

You might also like