Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. %%
  2. %Assignment1 by Baylie Meredith
  3. %%
  4. function assign(folder)
  5. clc();
  6. if ~exist('folder','var')
  7. folder = '.\data\HomeC002\';
  8. end
  9.  
  10. disp(folder);
  11.  
  12. % load Depth and RGB images.
  13. tempload = load([folder,'\PSLR_C01_120x160.mat']);
  14. RGB = tempload.CC;
  15. tempload = load([folder,'\PSLR_D01_120x160.mat']);
  16. DEPTH= tempload.CR;
  17.  
  18. L = DEPTH.N;
  19. global globvar1;
  20. globvar1=[];
  21. globvar1.flagPause = 0;
  22. globvar1.flagrotate = 1;
  23. globvar1.flagfilter = 1;
  24.  
  25. %% Create figure, where we will show Depth & RGB
  26. figure(1);
  27. clf();
  28.  
  29. % Subfigure, for Depth
  30. subplot(2,1,1) ;
  31. depthinr = DEPTH.R(:,:,1);
  32. HandleDepth = imagesc(depthinr);
  33. title('Depth');
  34. colormap gray;
  35. set(gca(),'xdir','reverse'); %flipping axes
  36.  
  37. % In another subfigure, we show the associated RGB image.
  38. subplot(2,1,2);
  39. HandleColour = image(RGB.C(:,:,:,1));
  40. title('RGB');
  41. set(gca(),'xdir','reverse'); %flipping axes
  42.  
  43. %% 3D points figure.
  44. figure(2);
  45. clf();
  46.  
  47. HandleAxes = axes('position',[0.20,0.1,0.75,0.85]); %moves the 3D in the graph
  48. HandlePlot1 = plot3(HandleAxes,0,0,0,'.','markersize',2) ;
  49. HandlePlotr = plot3(HandleAxes,0,0,0,'.r','markersize',2) ;
  50. axis([0,3,-1.5,1.5,-0.4,0.9]);
  51. title('3D');
  52. xlabel('X (m)');
  53. ylabel('Y (m)');
  54. zlabel('Z (m)');
  55. grid on;
  56. rotate3d on ;
  57.  
  58. %% Some control buttons (you may define extra buttons, for other purposes)
  59.  
  60. uicontrol('Style','pushbutton','String','Pause/Cont.','Position',[10,1,80,20],'Callback',{@MyCallBackA,1});
  61. uicontrol('Style','pushbutton','String','ETC','Position',[90,1,80,20],'Callback',{@MyCallBackA,2});
  62. uicontrol('Style','slider','Position',[10,50,40,150],'Callback',{@MyCallBackB,1});
  63. uicontrol('Style','checkbox','Position',[20,250,30,50],'Callback',{@MyCallBackRotate});
  64. uicontrol('Style','checkbox','Position',[10,250,30,50],'Callback',{@MyCallBackFilter});
  65.  
  66. iradius = input('Inner radius =');
  67. oradius = input('Outer radius =');
  68. %%
  69. %theta = 0:0.01:2*pi
  70. %r = oradius
  71. %x = r*cos(theta)
  72. %y = r*sin(theta)
  73. %z = [0]
  74. %plot3(x,y,z);
  75. %r = iradius
  76. %x = r*cos(theta)
  77. %y = r*sin(theta)
  78. %plot3(x,y,z);
  79.  
  80. i=0;
  81. while 1 %neverending loop like 2121
  82. while (globvar1.flagPause) %while this flagpause is true
  83. pause(0.3); %stops and checks every 0.3 seconds whether flagpause has been turned off
  84. end
  85.  
  86. i=i+1;
  87. if i > L
  88. break;
  89. end
  90.  
  91. depthinr = DEPTH.R(:,:,i);
  92. set(HandleColour,'cdata',RGB.C(:,:,:,i)); % show RGB image
  93. set(HandleDepth,'cdata',depthinr);
  94.  
  95. % obtain 3D points, for those pixels which are not faulty.
  96. Goodpixel = find(depthinr>0);
  97. [row,col] = find(depthinr>0);
  98. %changes the numbers on the 3D plot
  99. if (globvar1.flagrotate == 1)
  100. [xx,yy,zz] = ConvertSelectedDepthsTo3DPoints(single(depthinr)*0.001,Goodpixel,row,col);
  101. [xx,zz] = Rotate2D(xx,zz,10);
  102. zz = zz + 0.2;
  103. end
  104.  
  105. % Show the 3D points but not below -5 or above 100cm
  106. cutindexes = (zz < 1) & (zz < 1);
  107. [xx,yy,zz] = CutOff(xx,yy,zz,cutindexes);
  108.  
  109. if(globvar1.flagfilter == 1)
  110. radius = (xx.^2 +yy.^2).^(1/2);
  111. redpoints = ((radius < oradius ) & (radius > iradius) & (zz > 0.15))
  112. %gets a logically vector of the indices that are red hence ~red will be
  113. %blue points
  114. xred = xx(redpoints)
  115. xblue = xx(~redpoints)
  116. yred = yy(redpoints)
  117. yblue = yy(~redpoints)
  118. zred = zz(redpoints)
  119. zblue = zz(~redpoints);
  120. hold on;
  121. set(HandlePlot1,'xdata',xblue,'ydata',yblue,'zdata',zblue);
  122.  
  123. set(HandlePlotr,'xdata',xred,'ydata',yred,'zdata',zred);
  124. hold off;
  125. %else
  126. % set(HandlePlot1,'xdata',xx,'ydata',yy,'zdata',zz);
  127. end
  128. pause(0.1);
  129. end
  130. end
  131. % ---------------------------------------
  132. % Callback function. I defined it, and associated it to certain GUI button,
  133. function MyCallBackA(~,~,x)
  134. global globvar1;
  135.  
  136. if (x == 1)
  137. globvar1.flagPause = ~globvar1.flagPause; %Switch ON->OFF->ON -> and so on.
  138. disp(x);disp(globvar1.flagPause);
  139. return;
  140. end
  141. if (x==2)
  142. disp('you pressed ETC!');
  143. return;
  144. end
  145. return;
  146. end
  147.  
  148. % ...............................................
  149. % I associated the following function, as a callback function for one slider control.
  150. % Each time a new value is set, in the slider, our function is called
  151. function MyCallBackB(a,~,~)
  152. global globvar1
  153. globvar1 = get(a,'value'); % the property "value" is the current
  154. % value of the slider (position of the selector)
  155. % You may use it to set the value of certain relevant variable,
  156. % in your program.
  157. % Here, I just print its value, for testing purposes.
  158. disp(v) ;
  159.  
  160. % BTW: the object ("a"), has many other properties; you may inspect them.
  161. return;
  162. end
  163. % ---------------------------------------
  164. % I associated this function, as a callback function for one CheckBox.
  165. % Each time the state of the checkbx is modified, our function is called
  166. function MyCallBackRotate(a,~,~)
  167. global globvar1
  168. globvar1.flagrotate = get(a,'value');
  169. return;
  170. end
  171. function MyCallBackFilter(a,~,~)
  172. global globvar1
  173. globvar1.flagFilter = get(a,'value');
  174. return;
  175. end
  176. % ---------------------------------------
  177. function [xx,yy,zz] = ConvertSelectedDepthsTo3DPoints(depthfunc, Goodpixels, r,c)
  178. xx = depthfunc(Goodpixels);
  179. yy = depthfunc(Goodpixels).*(c-80)*4/594;
  180. zz = -depthfunc(Goodpixels).*(r-60)*4/592;
  181. end
  182. % ---------------------------------------
  183. function [xx,zz] = Rotate2D(xcoord,zcoord,angle)
  184. angle = angle*pi/180;
  185. xx = xcoord*cos(angle)+ zcoord*sin(angle);
  186. zz = xcoord*-sin(angle)+ zcoord*cos(angle);
  187. end
  188. %---------------------------------------
  189. function [xx,yy,zz] = CutOff(xx,yy,zz,cutindices)
  190. zz = zz(cutindices);
  191. yy = yy(cutindices);
  192. xx = xx(cutindices);
  193. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement