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
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) # 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