for y in range(0, TextImage.size[1]): # discount everything that's completely white if TextImagePixels[x, y] != 255: ImageSlice.append(TextImagePixels[x, y]) Difference = 0 SecondDifference = 0 if len(ImageSlice) > 2: Difference = sorted(ImageSlice, reverse=True)[0] - sorted(ImageSlice)[0] DifferenceArray.append(Difference) # find all of the local mins LocalMins = Array.FindAllLocalMins(DifferenceArray) Slope = Array.FindSlope(DifferenceArray) # make sure that clusters of data are treated as one individual point UnfilteredCropPoints = [] # add the first point because it automatically counts UnfilteredCropPoints.append(LocalMins[0]) for i in range(1, len(LocalMins)): # the current crop point is set here CurrentCropPoint = UnfilteredCropPoints[len(UnfilteredCropPoints) - 1] # create an array of difference values between the two crop points ValueOffsetArray = DifferenceArray[CurrentCropPoint:LocalMins[i]] # compute the TempArray = []
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