Basic Image Operations

# Useful libraries
import numpy as np
import cv2
import os
import sys

# Carnot Research Vision Library
from cr import vision
%matplotlib inline

# Images supplied with this library
MODULE_DIR = os.getcwd()
PACKAGE_DIR = os.path.dirname(MODULE_DIR)
DATA_DIR = os.path.join(PACKAGE_DIR, 'data')
IMAGES_DIR = os.path.join(DATA_DIR, 'images')

Creating Simple Images

A gray scale blank image

black = vision.blank_image(300, 300)

Image Cleanup via Adaptive Thresholding

Let’s load an image of a page of text scanned in low light conditions.

image_path = os.path.join(IMAGES_DIR, 'bookpage_dark_scan.jpg')
image = cv2.imread(image_path)

Let’s convert the image to gray scale (note that OpenCV loads images in BGR format)

gray_image = vision.bgr_to_gray(image)

Let’s apply an adaptive thresholding algorithm to cleanup the image

thresholded_image = vision.adaptive_threshold_gaussian(
    gray_image, block_size=115, constant=1)
[ ]: