johnny2x2

LaserScannerScript

Jul 16th, 2016
1,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.94 KB | None | 0 0
  1. clear all
  2. close all
  3. vidobj = videoinput('winvideo', 1, 'RGB24_960x720'); %Setup Camera
  4. aMega  = arduino('COM9');
  5. pause_time = 0.005;
  6. LaserPin = 'D22';
  7. Ang_of_onerot = 0.7;
  8. onerot_loops = 2;
  9.  
  10. pic1 = getsnapshot(vidobj);
  11. imgSize = size(pic1);
  12. %% Model Variables
  13. f = 919; % Focal length
  14. cx = 495; % Optical center x
  15. cy = 335; % Optical center y
  16. b0 = 140; % Laser distance from camera
  17. C0 =65.9534; % Angle of laser
  18. Y_mltply = 0.234;%Y axis scaling factor
  19. y_trans = 14; % Transform value for Y
  20. z_trans = -315; % Transform value for Z
  21. Ang_per_loop = Ang_of_onerot*onerot_loops;
  22. Main_loops = round(360/Ang_per_loop);
  23. %% Set Constants
  24. MaxPoints = imgSize(1);
  25. cordW = zeros(MaxPoints,3,Main_loops);
  26. %% Start Loop
  27. for Tm = 1:Main_loops    
  28. LaserPts = []; % Camera Coordinates of Laser line
  29. CamCoordPts = []; % List of X; Y; Z; points
  30. BW = NewFilter(vidobj,imgSize,aMega,LaserPin);
  31. imshow(BW);
  32. %% Convert Single Line to camera coordinates
  33. counter = 1;
  34. for i = 1:imgSize(1)
  35.     for i1 = 1:imgSize(2)
  36.         testpt = BW(i,i1);
  37.         if testpt == 1 % if point on Y
  38.         LaserPts(1,counter) = i1-cx; % add to LaserPts list in Camera Coordinates
  39.         LaserPts(2,counter) = cy-i;
  40.         counter = counter+1;
  41.         end
  42.     end
  43. end
  44. %% Determine World Coordinates for each Y
  45. sizeDisx = size(LaserPts);
  46. for i = 1:sizeDisx(2)
  47.     delta_px_x0 = LaserPts(1,i);
  48.     A0 = 90+atand(delta_px_x0/f);
  49.     B0 = 180 - (A0+C0);
  50.     a0 = (sind(A0)*b0)/sind(B0);
  51.     h1 = a0*cosd(atand(delta_px_x0/f));
  52.     CamCoordPts(3,i)=h1;
  53.     CamCoordPts(2,i) = Y_mltply*LaserPts(2,i);
  54. end
  55. CamCoordPts(1,:) = 0;
  56.  
  57.  
  58.     %% Transform CamCoords to World Coordinate
  59.     Mext = [ 1 0 0 0;
  60.         0 1 0 y_trans;
  61.         0 0 1 z_trans;
  62.         0 0 0 1;];
  63.    
  64.     thetaW=(Tm-1)*Ang_per_loop; % New angle
  65.     Trans = [cosd(thetaW),0,sind(thetaW),0;0,1,0,0;-sind(thetaW),0,cosd(thetaW),0;0,0,0,1]; %Translation Matrix
  66.  
  67.     CordCT = [];
  68.     counterct = 1;
  69.    
  70.     for i = 1:sizeDisx(2)
  71.        % if CamCoordPtsR(3,i)<350
  72.         CordCT(:,i) = Mext*[CamCoordPts(1,i);CamCoordPts(2,i);CamCoordPts(3,i);1];
  73.          counterct = counterct+1;
  74.        % end
  75.     end
  76.    
  77.     for i = 1:sizeDisx(2)
  78.         CordW(:,i) = Trans*[CordCT(1,i);CordCT(2,i);CordCT(3,i);1];
  79.     end
  80.    
  81.        
  82.      transCW = transpose(CordW(1:3,:));
  83.      sRc = size(CordW);
  84.      if sRc(2)==MaxPoints
  85.          cordW(:,:,Tm) =transCW;
  86.      else
  87.     transCW(sRc(2):MaxPoints,:)=0;
  88.     cordW(:,:,Tm) =transCW;
  89.      end
  90.    
  91.  
  92.     %% Rotate platform
  93.      onerot(aMega,pause_time,onerot_loops);
  94. end
  95.  
  96. xyzPoints = [];
  97. scW = size(cordW);
  98. counter = 1;
  99. for i0 = 1:scW(3)
  100.     for i1 = 1:scW(1)
  101.        %if cordWR(i1,2,i0)>1
  102.         xyzPoints(counter,:)=cordW(i1,:,i0);
  103.         counter=counter+1;
  104.        %end
  105.     end
  106. end
  107.  
  108.  
  109. ptCloud =pointCloud( xyzPoints);
  110. ptCloud = pcdenoise(ptCloud);
  111. pcwrite(ptCloud,'PointCloudOutput','PLYFormat','binary');
  112. figure; pcshow(ptCloud);
Advertisement
Add Comment
Please, Sign In to add comment