示例#1
0
from matplotlib import pyplot as plt
from PIL import Image
import sys
import math

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

# blur the image
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)
示例#2
0
# 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
blur = cv2.GaussianBlur(img, (XFilterSize, XFilterSize), 0)

BlurredImageArray = Array.HorizontalArrayFromImage(Image.fromarray(blur))
RegularImageArray = Array.HorizontalArrayFromImage(Image.fromarray(img))

MeanArray = Array.MeanArray(RegularImageArray, SmoothSize)
MeanArrayBlurred = Array.MeanArray(BlurredImageArray, SmoothSize)

fig, ((ax1, ax2)) = plt.subplots(2, 1, sharex=False, sharey=False)

# LocalMaxes = sorted(Array.FindAllLocalMaxes(BlurredImageArray) + Array.FindAllLocalMins(BlurredImageArray))
LocalMaxes = Array.FindAllLocalMaxes(BlurredImageArray)
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