opencv python border removal preprocessing for ocr

  • Last Update :
  • Techknowledgy :

import cv2
import numpy as np

image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
mask = np.zeros(image.shape, dtype = np.uint8)

cnts = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0]
if len(cnts) == 2
else cnts[1]

cv2.fillPoly(mask, cnts, [255, 255, 255])
mask = 255 - mask
result = cv2.bitwise_or(image, mask)

cv2.imshow('mask', mask)
cv2.imshow('result', result)
cv2.waitKey(0)

Suggestion : 2

Starting from your filtered image, anycodings_image here's a simple approach,Now we simply cv2.bitwise_or() with the anycodings_image original image to get our result,After converting to grayscale, we find anycodings_image the main contour that we want to keep anycodings_image and draw this section onto a mask. anycodings_image Afterwards, we invert the mask to get anycodings_image this image which represents the desired anycodings_image border section to fill in white,Can staticfiles in Django be served in production if they're located in non-english called directories?

I have already written some code to remove anycodings_opencv the majority of the background, but large anycodings_opencv black spots still remain as a border. The anycodings_opencv included code is my image cropping script anycodings_opencv that removes the majority of the images anycodings_opencv black border and attempts to isolate the anycodings_opencv text as much as possible, but unfortunately, anycodings_opencv it still leaves quite a significant amount anycodings_opencv of black that messes with my skew correction anycodings_opencv script.

def boarderRemoval(img):
   ""
"
Takes in a numpy array and crops the image down to isolate the text(Still leaves a small black border that varys from image to image\ n Vars: \n -
      img < -numpy array of the label\ n Returns: \n -
      Cropped down image with smaller black borders ""
      "
      contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnt = contours[0] x, y, w, h = cv2.boundingRect(cnt) correctedImage = img[y: y + h, x: x + w]

      return correctedImage

import cv2
import numpy as np

image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
mask = np.zeros(image.shape, dtype = np.uint8)

cnts = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0]
if len(cnts) == 2
else cnts[1]

cv2.fillPoly(mask, cnts, [255, 255, 255])
mask = 255 - mask
result = cv2.bitwise_or(image, mask)

cv2.imshow('mask', mask)
cv2.imshow('result', result)
cv2.waitKey(0)

Suggestion : 3

Image processing Rescaling Binarisation Noise Removal Dilation / Erosion Rotation / Deskewing Borders Transparency / Alpha channel Tools / Libraries Examples Tables recognitions ,Bold characters or Thin characters (especially those with Serifs) may impact the recognition of details and reduce recognition accuracy. Many image processing programs allow Dilation and Erosion of edges of characters against a common background to dilate or grow in size (Dilation) or shrink (Erosion).,rotation_spacing.py - python script for automatic detection of rotation and line spacing of an image of text,Tesseract does various image processing operations internally (using the Leptonica library) before doing the actual OCR. It generally does a very good job of this, but there will inevitably be cases where it isn't good enough, which can result in a significant reduction in accuracy.

convert 427 - 1. jpg - bordercolor White - border 10 x10 427 - 1 b.jpg
convert input.png - alpha off output.png
  0 Orientation and script detection(OSD) only.
  1 Automatic page segmentation with OSD.
  2 Automatic page segmentation, but no OSD, or OCR.
  3 Fully automatic page segmentation, but no OSD.(Default)
  4 Assume a single column of text of variable sizes.
  5 Assume a single uniform block of vertically aligned text.
  6 Assume a single uniform block of text.
  7 Treat the image as a single text line.
  8 Treat the image as a single word.
  9 Treat the image as a single word in a circle.
  10 Treat the image as a single character.
  11 Sparse text.Find as much text as possible in no particular order.
  12 Sparse text with OSD.
  13 Raw line.Treat the image as a single text line,
     bypassing hacks that are Tesseract - specific.

Suggestion : 4

September 17, 2018 at 9:56 pm,September 17, 2018 at 7:47 pm,September 17, 2018 at 7:34 pm,September 17, 2018 at 7:22 pm

To check your Ubuntu version you can use the lsb_release command:

$ lsb_release - a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04 .1 LTS
Release: 18.04
Codename: bionic

For Ubuntu 18.04 users, Tesseract 4 is part of the main apt-get repository, making it super easy to install Tesseract via the following command:

$ sudo apt install tesseract - ocr

Just add the alex-p/tesseract-ocr PPA repository to your system, update your package definitions, and then install Tesseract:

$ sudo add - apt - repository ppa: alex - p / tesseract - ocr
$ sudo apt - get update
$ sudo apt install tesseract - ocr

If you already have Tesseract installed on your Mac (if you followed my previous Tesseract install tutorial, for example), you’ll first want to unlink the original install:

$ brew unlink tesseract

Once you have Tesseract installed on your machine you should execute the following command to verify your Tesseract version:

$ tesseract - v
tesseract 4.0 .0 - beta .3
leptonica - 1.76 .0
libjpeg 9 c: libpng 1.6 .34: libtiff 4.0 .9: zlib 1.2 .11
Found AVX512BW
Found AVX512F
Found AVX2
Found AVX
Found SSE