9951 explained code solutions for 126 technologies


tesseract-ocrHow can I use Tesseract OCR with OpenCV?


Tesseract OCR can be used with OpenCV to perform optical character recognition (OCR) on images.

The basic steps for using Tesseract OCR with OpenCV are:

  1. Load the image into OpenCV.
  2. Pre-process the image, such as converting it to grayscale, blurring it, and/or thresholding it.
  3. Pass the pre-processed image to Tesseract OCR to extract the text from the image.

Example code

# import the necessary packages
import cv2
import pytesseract

# load the example image and convert it to grayscale
image = cv2.imread("example.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# apply Otsu's thresholding to binarize the image
thresh = cv2.threshold(gray, 0, 255,
    cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

# pass the image to Tesseract OCR
text = pytesseract.image_to_string(thresh)

print(text)

Output example

Hello World!

The code above performs the following steps:

  1. Loads the image into OpenCV.
  2. Converts the image to grayscale.
  3. Applies Otsu's thresholding to binarize the image.
  4. Passes the binarized image to Tesseract OCR to extract the text from the image.
  5. Prints the extracted text.

Helpful links

Edit this code on GitHub