def CropImageAroundEdges(InputImage):
	# convert the input image to black
	InputImage = InputImage.convert('L')

	# extract the edges of the letters
	RawImage = PILToCV2(InputImage)
	GrayImage = cv2.cvtColor(RawImage, cv2.COLOR_BGR2GRAY)
	# convert back to a PIL image
	ImageEdges = Image.fromarray(InvertColor(cv2.Canny(GrayImage, 50, 150, apertureSize = 3)))


	CharacterHorizontalArray = Array.HorizontalArrayFromImage(ImageEdges)
	CharacterVerticalArray = Array.VerticalArrayFromImage(ImageEdges)

	YMin = 0
	YMax = 0
	XMin = 0
	XMax = 0

	# find the min and max for y
	for i in range(0, len(CharacterVerticalArray)):
		if CharacterVerticalArray[i] != 255:
			YMin = i
			break
	for i in range(ImageEdges.size[1] - 1, -1, -1):
		if CharacterVerticalArray[i] != 255:
			YMax = i
			break

	# find the min and max for x
	for i in range(0, len(CharacterHorizontalArray)):
		if CharacterHorizontalArray[i] != 255:
			XMin = i
			break
	for i in range(ImageEdges.size[0] - 1, -1, -1):
		if CharacterHorizontalArray[i] != 255:
			XMax = i
			break

	# +1's are to compensate for how crop function works
	InputImage = InputImage.crop((XMin, YMin, XMax + 1, YMax + 1))
	return InputImage
Exemple #2
0
img = cv2.imread('Processed/Cropped5.png')
YFilterSize = Statistics.RoundToOdd(len(img) / 20)
XFilterSize = Statistics.RoundToOdd(len(img[0]) / 20)

SmoothSize = Statistics.RoundToEven(len(img) / 20)

print "smooth size", SmoothSize

# use the recomended standard deviation
blur = cv2.GaussianBlur(img, (XFilterSize, YFilterSize), 0)
fig, ((ax1, ax2), (ax3, ax4), (ax5, ax6)) = plt.subplots(3,
                                                         2,
                                                         sharex=False,
                                                         sharey=False)

BlurredImageArray = Array.VerticalArrayFromImage(Image.fromarray(blur),
                                                 BandPercentage=.5)
MeanArray = Array.MeanArray(BlurredImageArray, SmoothSize)
MedianArray = Array.MedianArray(BlurredImageArray, SmoothSize)
MeanMedianArray = Array.MedianArray(MeanArray, SmoothSize)

ax1.plot(MeanMedianArray)
ax1.set_title("Blurred then Mean then Median")
ax2.plot(BlurredImageArray)
ax2.set_title("Blurred")
ax3.plot(MeanArray)
ax3.set_title("Blurred and Mean")
ax4.plot(Array.VerticalArrayFromImage(Image.fromarray(img), BandPercentage=.5))
ax4.set_title("unprocessed")
ax5.plot(MedianArray)
ax5.set_title("Blurred then Median")
plt.show()
Exemple #3
0
from os import listdir
import os
import cv2
import numpy as np
import glob

# add the tools folder to the path
sys.path.append("../Tools")
import Array
import Statistics

img = cv2.imread("Lines/4.png")
# 111 (0-6), min-max method
# 11
# compute the raw array
RawArray = Array.VerticalArrayFromImage(Image.fromarray(img))

# calculate filter and smooth sizes based on how big the image is
YFilterSize = Statistics.RoundToOdd(len(img) / 40)
XFilterSize = Statistics.RoundToOdd(len(img[0]) / 40)

# get the line height
LineHeight = 0
with open('SharedData/LineHeight.txt', 'r') as content_file:
    LineHeight = int(float(content_file.read()))

# use the line height, and make the smoothing size 1/2 of that
SmoothSize = Statistics.RoundToEven(LineHeight / 2.0)
print "smooth size", SmoothSize

# use the recomended standard deviation
def CalculateCropPoints(LineHeight, ShowGraphs=False):
    global FileName

    # do the gaussian smoothing that enables us to find the crop points
    # blur the image
    img = cv2.imread("Processed/" + FileName)

    # compute the raw array
    RawArray = Array.VerticalArrayFromImage(Image.fromarray(img))

    # calculate filter and smooth sizes based on how big the image is
    YFilterSize = Statistics.RoundToOdd(len(img) / 20)
    XFilterSize = Statistics.RoundToOdd(len(img[0]) / 20)
    SmoothSize = Statistics.RoundToEven(len(img) / 20)

    # use the line height, and make the smoothing size 1/2 of that
    SmoothSize = Statistics.RoundToEven(LineHeight / 2.0)

    # use the recomended standard deviation
    blur = cv2.GaussianBlur(img, (XFilterSize, YFilterSize), 0)

    BlurredImageArray = Array.VerticalArrayFromImage(Image.fromarray(blur),
                                                     BandPercentage=.5)
    MeanArray = Array.MeanArray(BlurredImageArray, SmoothSize)
    MedianArray = Array.MedianArray(BlurredImageArray, SmoothSize)

    # get the crop points by finding local maxes
    CropPoints = Array.FindAllLocalMaxes(MeanArray)

    # check to see if there should be any extra crop points added
    SlopeArray = Array.FindSlope(MeanArray)
    # if the initial slope is less than 0
    if SlopeArray[0] < 0:
        # insert 0 at position 0
        CropPoints.insert(0, 0)
    # if the final slope is more than 0 or 0
    if SlopeArray[len(SlopeArray) - 1] >= 0:
        CropPoints.append(len(MeanArray) - 1)

    if ShowGraphs:
        # graph / visualize some stuff
        TempArray = []
        for i in range(0, len(MeanArray)):
            if i in CropPoints:
                TempArray.append(255)
            else:
                TempArray.append(150)

        fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,
                                                     2,
                                                     sharex=False,
                                                     sharey=False)
        ax1.set_title("Blurred then Mean")
        ax1.plot(MeanArray)
        ax1.plot(RawArray)
        ax2.plot(BlurredImageArray)
        ax2.plot(RawArray)
        ax2.set_title("Blurred")
        ax3.imshow(img)
        ax3.set_title("Image")
        ax4.imshow(blur)
        ax4.set_title("Blur Image")
        plt.show()

    return CropPoints