Ok, here is a rather raw intent:
(*define a template font*)
i = Rasterize@Style[" A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ",
FontFamily -> "Courier", FontSize -> 24];
(*separate characters*)
cn = ColorNegate /@ Flatten@ImagePartition[i, ImageDimensions[i]/{26, 1}]
(*define a function for size adjustment*)
let[u_] := Function[{x}, ImageTake[x, Sequence @@ Reverse@Transpose@(u +
ComponentMeasurements[x, "BoundingBox"][[1, 2]])]][#] & /@ cn;
(*set of images for size scaling *)
forSize = let[0];
(*set of images for matching*)
forMatch = let[3 {{-1, -1}, {1, 1}}];
(*Mean template char size*)
sz = N@Mean[ImageDimensions /@ forSize]
(*----------------------*)
(*Now test it*)
i1 = ColorNegate@Rasterize[
Style[" M Y L I T T L E H O U S E I N T H E P R A I R I E \
W A S A M E S S O F R A T S A N D B A T S ",
FontFamily -> "Courier", FontSize -> 25], ImageSize -> 1000]
(* Compute a size factor*)
sizeFactor = -Mean[Mean[Subtract@@@(Range@38/. ComponentMeasurements[i1, "BoundingBox"])]/sz];
(* resize the image to match the template's char size*)
r = Rasterize[i1, ImageSize -> ImageDimensions@i1/sizeFactor];
(*Perform the matching*)
c[t_] := List @@ (ColorData[60][t[[1]]]);
xx = (ImageCorrelate[ r, #, NormalizedSquaredEuclideanDistance] & /@ (Binarize /@ forMatch));
cc = MapIndexed[ImageMultiply[
With[{k = c[#2]}, Image@Array[k &, Reverse@ImageDimensions[#1]]],
Dilation[Binarize[ColorNegate@#1, 0.8], DiskMatrix[15]]] &, xx];
rcc = Image[ImageAdd[cc[[#]], r], ImageSize -> {849, 60}] & /@ Range@Length@forMatch;
Fold[ImageAdd[#1, #2] &, rcc[[1]], rcc]
As you can see below the results aren't perfect. There are two obvious improvements to test:
- Accept a match after comparing the goodness of all other matchings over a character
- Refine the sensibility (0.95 in this test)


ImageCorrelate
, underApplications
i.stack.imgur.com/3tjDp.png $\endgroup$