johnny2x2

NewFilter

Jul 16th, 2016
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.40 KB | None | 0 0
  1. function LaserMask = NewFilter(vidobj,imgSize,aMega,pin)
  2.  
  3. writeDigitalPin(aMega,pin,1);
  4. frameOn=getsnapshot(vidobj);
  5. pause(.5);
  6. writeDigitalPin(aMega,pin,0);
  7. frameOff=getsnapshot(vidobj);
  8.  
  9. frameongray=frameOn(:,:,1)-((frameOn(:,:,2)+ frameOn(:,:,3)/2));
  10. frameoffgray = frameOff(:,:,1)-((frameOff(:,:,2)+ frameOff(:,:,3)/2));
  11. snap3=imgaussfilt(frameongray-frameoffgray,1);
  12.  
  13. I_new =zeros(size(snap3));
  14.  
  15. for iy = 1:imgSize(1)
  16.     xme = max(snap3(iy,:));
  17.     xme1 = max(find(snap3(iy,:)==xme));
  18.     I_new(iy,xme1) = 1;  
  19. end
  20.  
  21. snap = I_new;
  22. snap = bwareaopen(snap,20);
  23. I_c = zeros(size(snap));
  24.  
  25. for iy = 1:imgSize(1)-1
  26.     for ix = 1:imgSize(2)
  27.         I_c(iy,ix,:) =snap(iy+1,ix,:)-snap(iy,ix,:);
  28.     end
  29. end
  30. I_R =zeros(size(snap));
  31. for iy = 1:imgSize(1)
  32.     for ix = 1:imgSize(2)-1
  33.         I_R(iy,ix,:) =snap(iy,ix+1,:)-snap(iy,ix,:);
  34.     end
  35. end
  36. I_G =zeros(size(snap));
  37. for iy = 1:imgSize(1)-1
  38.     for ix = 1:imgSize(2)-1
  39.         I_G(iy,ix,:) = sqrt((I_c(iy,ix,:)^2)+(I_R(iy,ix,:)^2));
  40.     end
  41. end
  42. I_G = bwareaopen(I_G,20);
  43. LaserMask = zeros(size(I_new));
  44. for y0 = 1:imgSize(1)
  45.         meanx = round(mean(find(I_G(y0,:)==1))); %Find center of laser on Y line
  46.         if isnan(meanx) == 1 % if no laser on Y that row is = 0;
  47.             LaserMask(y0,:)= 0;
  48.         else
  49.             LaserMask(y0,meanx)= 1; % if laser on Y set 1 Pixel to = 1 at laser center
  50.         end      
  51. end
  52. end
Advertisement
Add Comment
Please, Sign In to add comment