Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. clc; clear, close all;
  2. images = {'book-cropped.jpg'};
  3. for nImage = 1 : length (images)
  4.  
  5. % Load image
  6. [pathStr, name, ext] = fileparts (images{nImage});
  7. img = imread (images{nImage});
  8.  
  9. % Define parameters for local Otsu
  10. winSize = 32;
  11. stepSize = 16;
  12. heightCrop = floor (size(img,1) / winSize) * winSize;
  13. widthCrop = floor (size (img,2) / winSize) * winSize;
  14. img = img (1 : heightCrop, 1: widthCrop, :);
  15. imgGray = rgb2gray (img);
  16. imgDouble = double (img);
  17. imgGrayDouble = double (imgGray);
  18.  
  19. % Adaptive binarization
  20. localThresh = 60;
  21. [imgBW, localUsed, localThresh] = localAdaptiveOtsu ( imgGray, winSize, stepSize, localThresh );
  22. imgBW = 1 - imgBW;
  23. figure; clf;
  24. set (gcf, 'Position', [150 150 300 200]);
  25. subplot (2, 3, 1); imshow (imgBW); title ('Binarized Image');
  26. subplot (2, 3, 2); imshow (localThresh, [0 255]); title ('Local Thresholds');
  27. subplot (2, 3, 3); imshow (localUsed) ; title ('Local versus Global Thresholded Tiles');
  28. imwrite (imgBw, [name '-binarize.jpg']);
  29.  
  30. %Keep eccentric blobs
  31. imgBwLabel = bwlabel(imgBW);
  32. shapeProps = regionprops (imgBWLabel, 'MajorAxisLength', 'MinorAxisLength');
  33. for nRegion = 1 : length (shapeProps)
  34. idx = find (imgBWLabel == nRegion);
  35. eccentricity = shapeProps (nRegion) . MajorAxisLength / shapeProps (nRegion) .MinorAxisLength;
  36. if eccentricity < 5
  37. imgBW(idx) = 0;
  38. end
  39. end %nRegion
  40. subplot (2, 3, 4); imshow (imgBW) ; title ('Eccentric Blobs');
  41. imwrite (imgBW, [name '-binarize-eccentricity.jpg']);
  42. % Find major axis length histogram
  43. imgBWLabel = bwlabel (imgBW);
  44. shapeProps = regionprops (imgBWLabel, 'MajorAxisLength');
  45. majorLengths = zeros (1, length (shapeProps));
  46. for nRegion = 1 : length (shapeProps)
  47. majorLengths (nRegion) = shapeProps (nRegion) . MajorAxisLength;
  48. end % nRegion
  49. histBins = linspace (0, max (majorLengths));
  50. majorHist = hist (majorLengths, histBins);
  51. [maxHist, maxHistIdx] = max (majorHist);
  52. maxMajorLength = histBins (maxHistIdx);
  53.  
  54. % Keep blobs with major axis length close to histogram-peak length
  55. for nRegion = 1 : length (shapeProps)
  56. idx = find (imgBWLabel == nRegion);
  57. if abs (shapeProps (nRegion) .MajorAxisLength - maxMajorLength) > 0.4 * maxMajorLength
  58. imgBW (idx) = 0;
  59. end
  60. end % nRegion
  61.  
  62. subplot (2, 3, 5); imshow (imgBW); title ('Filtered by Major Axis Length');
  63. imwrite (imgBW, [name '-binarize-length.jpg']);
  64. % Find angle histogram
  65. imgBWLabel = bwlabel (imgBW);
  66. shapeProps = regionprops (imgBWLabel, 'Orientation');
  67. orientations = zeros (1, length(shapeProps));
  68. for nRegion = 1 : length (shapeProps)
  69. orientations (nRegion) = shapeProps (nRegion) .Orientation;
  70. end
  71. histBins = linspace (-90,90);
  72. orientationHist = hist (orientation, histBins);
  73. [maxHist, maxHistIdx] = max (orientationHist);
  74. maxAngle = histBins (maxHistIdx);
  75.  
  76. % Keep blobs with orientation close to histogram- peak orientation
  77. for nRegion = 1 : length (shapeProps)
  78. idx = find (imgBWLabel == nRegion);
  79. if abs (shapeProps (nRegion) .Orienatation - maxAngle) > 15
  80. end
  81. end
  82.  
  83. subplot (2, 3, 6); imshow (imgBW); title ('Filtered by Orientation');
  84. imwrite (imgBW,[name '-binarize-angle.jpg']);
  85.  
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement