Guest User

Untitled

a guest
Jan 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.97 KB | None | 0 0
  1. function CV2011_1HW4_ben_delillo()
  2.  
  3. imdir = 'C:\Users\Ben\Downloads\'; % Change this as appropriate
  4. I = imread(strcat(imdir,'TextImage.tif'));
  5.  
  6. % Use inverted binary image. Inversion changes the letters and other ink
  7. % from holes into connected components.
  8. Iinv = im2bw(I) == 0;
  9.  
  10. % After many variatons on refining edge detection I stumbled upon
  11. % morphological operations and that using a structuring element made up
  12. % of one element at the origin and one element three pixels above,
  13. % approximating the separation between the base of an i and its tittle,
  14. % was enough to change an i into a single connected component without
  15. % compromising the integrity of the rest of the image as a blur would have.
  16. Iprocessed = imdilate(Iinv, strel('pair', [-3 0]));
  17.  
  18. % Manualy extract example instance of what we want to find
  19. target = imcrop(Iprocessed, [682 467 16 30]);
  20.  
  21. % Split into connected comonents
  22. [components count] = bwlabel(Iprocessed);
  23.  
  24. % Using regionprops FilledImage option lets us easily analyze each
  25. % candidate letter
  26. stats = regionprops(components, 'BoundingBox', 'FilledImage');
  27.  
  28. % Making all letter candidates the same size as the target makes
  29. % comparison easier (also allows for finding off-scale matches)
  30. % It also makes rejecting small (< 10 pixels) clumps as they stretch
  31. % out as to almost make a solid white box.
  32. sizeToTemplate = @(img) imresize(img{1},size(target));
  33.  
  34. % Embed each image in a cell because arrayfun needs uniformly sized input
  35. letters = arrayfun(sizeToTemplate, {stats.FilledImage},...
  36.     'UniformOutput', false); % non-scalar output requires manual override
  37.  
  38. % This algorithm relies on two assumptions. The first is that shape
  39. % matching the target shape will have a similar 'density' of white pixels
  40. % per row as the target shape. The second is that the pool of candidates
  41. % will not have non-matching shapes with a similar density 'fingerprint'.
  42. % That second assumption actually fails for this text when using columns to
  43. % calculate that fingerprint because of how letters are taller than they
  44. % are wide and how similar l is to i. However, using the sum of rows
  45. % instead is high enough resolution that the algorithm can successfully
  46. % differentiate between l and i.
  47. sumOfDiffsOfRowPixelSums = @(ltr) sum(abs(sum(ltr, 2) - sum(target,2)));
  48. colSumDiffs = cellfun(sumOfDiffsOfRowPixelSums, letters);
  49.  
  50. % Is there an equivalent of the functional 'filter' idiom?
  51. results = []; bbs = [];
  52. for n=1:count
  53.     if colSumDiffs(n) < 50
  54.         results = cat(4, results, letters{n});
  55.         bbs = cat(2, bbs, stats(n).BoundingBox.');
  56.     end
  57. end
  58.  
  59. foundCount = length(results);
  60. titleStr = ['Found ', num2str(foundCount), ' instances of the letter i'];
  61.  
  62. figure('Name', titleStr), imshow(I);
  63. plotBoundingBoxes(bbs);
  64.  
  65. function plotBoundingBoxes(boxes)
  66. hold on;
  67. for cnt = 1:length(boxes)
  68.     rectangle('position', boxes(:,cnt), 'edgecolor', 'r');
  69. end
  70.  
  71. function imSxS(im1, im2)
  72. figure;
  73. subplot(1,2,1), subimage(im1);
  74. subplot(1,2,2), subimage(im2);
Add Comment
Please, Sign In to add comment