Advertisement
makispaiktis

Image Processing Onramp - Course 9 - Classify images from datastore

Aug 4th, 2023 (edited)
888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.97 KB | None | 0 0
  1. % 1. Image Datastore
  2. ds = imageDatastore("testimages");
  3. nFiles = numel(ds.Files);
  4. isReceipt = false(1, nFiles);
  5.  
  6. % 2. Classify images
  7. for i = 1 : nFiles
  8.     I = readimage(ds, i);
  9.     isReceipt(i) = classifyImage(I);
  10. end
  11.  
  12. % 3. Receipt and non-receipt images
  13. receiptFiles = ds.Files(isReceipt)
  14. montage(receiptFiles)
  15. nonreceiptFiles = ds.Files(~isReceipt)
  16. montage(nonreceiptFiles)
  17.  
  18.  
  19.  
  20.  
  21.  
  22. function isReceipt = classifyImage(I)
  23.    
  24.     gs = im2gray(I);
  25.     gs = imadjust(gs);
  26.    
  27.     mask = fspecial("average",3);
  28.     gsSmooth = imfilter(gs,mask,"replicate");
  29.    
  30.     SE = strel("disk",8);  
  31.     Ibg = imclose(gsSmooth, SE);
  32.     Ibgsub =  Ibg - gsSmooth;
  33.  
  34.     Ibw = ~imbinarize(Ibgsub);
  35.    
  36.     SE = strel("rectangle",[3 25]);
  37.     stripes = imopen(Ibw, SE);
  38.     signal = sum(stripes,2);  
  39.  
  40.     % Classification
  41.     minIndices = islocalmin(signal,"MinProminence",70,"ProminenceWindow",25);
  42.     Nmin = nnz(minIndices);
  43.     isReceipt = Nmin >= 9;
  44.    
  45. end
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement