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) # sets how different maximums from the 'same' point can be # this may be incorrect and will F**K us NewMinimumThreshold = 5 # make sure that clusters of data are treated as one individual point FilteredLocalMaxes = [] # add the first point because it automatically counts FilteredLocalMaxes.append(LocalMaxes[0]) for i in range(1, len(LocalMaxes)): # the current crop point is set here CurrentCropPoint = FilteredLocalMaxes[len(FilteredLocalMaxes) - 1] # create an array of difference values between the two crop points
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