Advertisement
makispaiktis

Image Processing Onramp - BASICS

Aug 1st, 2023 (edited)
1,032
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.80 KB | None | 0 0
  1. %% 1. Preprocessing - Read, grayscale, adjust, binarize
  2.  
  3. % 1a. Read
  4. I = imread(imgFile);
  5. % 1b. To grayscale
  6. gs = im2gray(I);
  7. % 1c. Adjust - Increase the contrast
  8. gsAdj = imadjust(gs);
  9. % 1d. Binarize - Adaptive thresholding
  10. BW = imbinarize(gsAdj,"adaptive","ForegroundPolarity","dark");
  11. imshowpair(I,BW,"montage")
  12. % 1e. Find patterns
  13. S = sum(BW,2);
  14. plot(S)
  15.  
  16.  
  17.  
  18. %% 2. Preprocessing - Read, grayscale, adjust, filtering, binarize
  19.  
  20. % 2a. Read
  21. I = imread("IMG_001.jpg");
  22. % 2b. To grayscale
  23. gs = im2gray(I);
  24. % 2c. Adjust
  25. gs = imadjust(gs);
  26. % 2d. Filtering
  27. H = fspecial("average",3);
  28. gs = imfilter(gs,H,"replicate");
  29. % 2e. Binarize
  30. BW = imbinarize(gs,"adaptive","ForegroundPolarity","dark");
  31. imshowpair(gs,BW,"montage")
  32.  
  33.  
  34. %% 3. Preprocessing - Filtering, closing, background removal
  35.  
  36. % 3a. Read
  37. I = imread("IMG_002.jpg");
  38. % 3b. To grayscale
  39. gs = im2gray(I);
  40. % 3c. Adjust
  41. gs = imadjust(gs);
  42. % 3d. Filtering
  43. H = fspecial("average",3);
  44. gs = imfilter(gs,H,"replicate");
  45. % 3e. Closing the image (make it brighter)
  46. SEdisk = strel("disk",8);
  47. Ibg = imclose(gs,SEdisk);
  48. % 3f. Subtract images to remove background
  49. gsSub =  Ibg - gs;
  50. % 3g. Invert the image
  51. BW = ~imbinarize(gsSub);
  52. imshowpair(I,BW,"montage")
  53.  
  54.  
  55. %% 4. Adjust, filter, close, binarize function
  56. function [signal,Ibw,stripes] = processImage(img)
  57.     % This function processes an image using the algorithm
  58.     % developed in Image Processing Onramp.
  59.    
  60.     gs = im2gray(img);
  61.     gs = imadjust(gs);
  62.    
  63.     H = fspecial("average",3);
  64.     gssmooth = imfilter(gs,H,"replicate");
  65.    
  66.     SE = strel("disk",8);  
  67.     Ibg = imclose(gssmooth, SE);
  68.     Ibgsub =  Ibg - gssmooth;
  69.     Ibw = ~imbinarize(Ibgsub);
  70.    
  71.     SE = strel("rectangle",[3 25]);
  72.     stripes = imopen(Ibw, SE);
  73.    
  74.     signal = sum(stripes,2);
  75.    
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement