Advertisement
makispaiktis

Image Processing Onramp - Course 6 - Background Removal

Aug 2nd, 2023
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.98 KB | None | 0 0
  1. % 1. Basics
  2.  
  3. I = imread("IMG_001.jpg");
  4. gs = im2gray(I);
  5. gs = imadjust(gs);
  6. H = fspecial("average",3);
  7. gs = imfilter(gs,H,"replicate");
  8. BW = imbinarize(gs,"adaptive","ForegroundPolarity","dark");
  9. imshowpair(gs,BW,"montage")
  10.  
  11. % 2. Close the image (make it whiter-brighter) using a structuring element
  12.  
  13. SE = strel("diamond", 5);
  14. SE = strel("disk", 8)
  15. Ibg = imclose(gs, SE);
  16. imshow(Ibg);
  17.  
  18. % 3. Having isolated the background, I can subtract the images, to remove the background
  19. gsSub = Ibg - gs;
  20. imshow(gsSub);
  21.  
  22. % 4. The new image has a black background now, so I have to invert the result
  23. BW = imbinarize(gsSub);
  24. BWsub = ~BW;
  25. imshow(BWsub);
  26.  
  27. % 5. Compare the sum rows
  28. S2 = sum(BW, 2);
  29. S3 = sum(BWsub, 2);
  30. figure(1);
  31. plot(S2, 'red');
  32. hold on
  33. plot(S3, 'green');
  34. legend("No closing", "Closing");
  35.  
  36. % 6. Image bottom hat preprocessing ----> "new" is the image after CLOSING and SUBTRACTING
  37.  
  38. new = imbothat(gs, SE);
  39. BW_new = imbinarize(new);
  40. BW_new = ~BW_new;
  41. imshow(BW_new);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement