Advertisement
BloodknightStudios

Untitled

Apr 30th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.26 KB | None | 0 0
  1. %% Creator Block
  2. %   Marc Chapman 054888139
  3.  
  4. %% Clear Everything
  5. clc; clear all; close all;
  6.  
  7. %% Load original image
  8. image= imread('TopImg0000.bmp');
  9.  
  10.  
  11. %% Segment tray
  12.  
  13. mask = (image > 3);                                 % Create a dirty mask
  14.  
  15. clean1 = bwareaopen(mask, 50);                      % Removes all objects smaller than specified size
  16. clean2 = imfill(clean1, 'holes');                   % Fills all black holes within the masked part of the image
  17.  
  18. se = strel('disk', 50);                             % sets edge cleaning options
  19. TrayMask = imopen(clean2, se);                      % creates new image using edge cleaning poptions
  20.  
  21. tray=regionprops(TrayMask,'boundingbox');           % Creates a struct of all bounding boxes
  22. TrayFinal=imcrop(image, tray.BoundingBox);          % Crops image based on bounding box
  23.  
  24. %% Segment Label
  25.  
  26. Image=TrayFinal;
  27.  
  28. % Iteratively loop until label is very distinct
  29. % TODO: add some kind of dynamic check so that the loop can autodiscover
  30. %       the level of distinction between label and package
  31. for i=1:3
  32.     Image = imadjust(Image,[0.2 0.8],[]);
  33. end
  34.  
  35. %   Repeat the same processes as for segmenting tray
  36. % TODO: Create a function to make code reuse a practical option
  37. mask = (Image > 30);                              
  38.  
  39. clean1 = bwareaopen(mask, 5000);                    
  40. clean2 = imfill(clean1, 'holes');                  
  41.  
  42. se = strel('disk', 50);                            
  43. LabelMask = imopen(clean2, se);                    
  44.  
  45. label=regionprops(LabelMask,'boundingbox');        
  46. LabelFinal=imcrop(TrayFinal, label.BoundingBox);    
  47.  
  48. %% Segment Information
  49. %   This is an extra task not in the brief, since it was decided
  50. %   that the label itself contains lots of useless data and it is
  51. %   in fact the product specific printed parts of the label that
  52. %   are of interest to the User
  53.  
  54. Image=LabelFinal;
  55.  
  56. % loop until label data is clear
  57. % TODO: add some kind of dynamic check so that the loop can autodiscover
  58. %       the level of distinction between label and package
  59. for i=1:7
  60.     Image = imadjust(Image,[0.2 0.9],[]);
  61. end
  62.  
  63. %   Repeat the same processes as for segmenting tray
  64. % TODO: Create a function to make code reuse a practical option
  65. mask = (Image > 30);
  66.  
  67. clean1 = bwareaopen(mask, 5000);
  68. clean2 = imfill(clean1, 'holes');
  69.  
  70. se = strel('disk', 50);
  71. InfoMask = imopen(clean2, se);
  72.  
  73. info=regionprops(InfoMask,'boundingbox');
  74. InfoFinal=imcrop(LabelFinal, info.BoundingBox);
  75.  
  76. %% Display required images
  77.  
  78. figure;
  79. subplot(3,3,1),     imshow(image),              title('Tray Input');
  80. subplot(3,3,2),     imshow(TrayMask),           title('Cleaned Mask');
  81. subplot(3,3,3),     imshow(TrayFinal),          title('Tray Final Image');
  82.  
  83. subplot(3,3,4),     imshow(TrayFinal),          title('Label Input');
  84. subplot(3,3,5),     imshow(LabelMask),          title('LabelMask');
  85. subplot(3,3,6),     imshow(LabelFinal),         title('FinalLabel');
  86.  
  87. subplot(3,3,7),     imshow(LabelFinal),         title('Information input');
  88. subplot(3,3,8),     imshow(InfoMask),           title('InfoMask');
  89. subplot(3,3,9),     imshow(InfoFinal),          title('FinalInfo');
  90.  
  91. %% Write images to files
  92.  
  93. imwrite(image,'CV_Assign1_Original.jpg','jpg','comment','Image owned by Marc Chapman 054888139 if found please return');
  94. imwrite(TrayMask,'CV_Assign1_TrayMask.jpg','jpg','comment','Image owned by Marc Chapman 054888139 if found please return');
  95. imwrite(TrayFinal,'CV_Assign1_TrayFinal.jpg','jpg','comment','Image owned by Marc Chapman 054888139 if found please return');
  96. imwrite(LabelMask,'CV_Assign1_LabelMask.jpg','jpg','comment','Image owned by Marc Chapman 054888139 if found please return');
  97. imwrite(LabelFinal,'CV_Assign1_LabelFinal.jpg','jpg','comment','Image owned by Marc Chapman 054888139 if found please return');
  98. imwrite(InfoMask,'CV_Assign1_InfoMask.jpg','jpg','comment','Image owned by Marc Chapman 054888139 if found please return');
  99. imwrite(InfoFinal,'CV_Assign1_InfoFinal.jpg','jpg','comment','Image owned by Marc Chapman 054888139 if found please return');
  100.  
  101. %% Further tasks
  102.  
  103. % Implement a text and QR Code reading function to write out
  104. % the specific data of the product, this data could then be used
  105. % for database operations such as productions levels, production speeds
  106. % error counting etc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement