FlyOrient.m 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. function orientation = FlyOrient(subset_frame, threshold)
  2. %FLYORIENT
  3. % Usage:
  4. % orientation = FlyOrient(subset_frame, threshold)
  5. %
  6. % This function takes in a subset frame around the fly (calculated by
  7. % FindFly) and discards the 3D data by placing points where the pixel intensity
  8. % is larger than the user chosen threshold. Then, Principal Components
  9. % Analysis (by way fot the pca1 function) is performed on the resulting
  10. % scatter plot to find the direction of maximum variance --- this direction
  11. % is taken to be the fly's (ambiguous) orientation.
  12. % orientation is a vector consisting of two angles (complements) that comprise
  13. % the body axis. The first element is an angle in the upper half plane; the
  14. % second element is an angle in the lower half plane.
  15. % Written by Dan Valente
  16. % 11 October 2006
  17. %Normalize frame data by pixel of maximum intensity
  18. subset_frame = subset_frame/max(max(subset_frame));
  19. % Put dots where fly is and do PCA on reduced data set
  20. [rows, cols] = find(subset_frame >= threshold);
  21. rows = length(subset_frame(:,1))-rows+1;
  22. x = [cols';rows'];
  23. [xnew, PC, V, data] = pca1(x);
  24. % Find orientation vectors (two, mirrored across diagonal), and group into
  25. % upper half and lower half planes.
  26. a1 = PC(1,1);
  27. b1 = PC(2,1);
  28. a2 = -PC(1,1);
  29. b2 = -PC(2,1);
  30. if (b1 >= 0 );
  31. orientUHP = atan2(b1,a1);
  32. orientLHP = atan2(b2,a2);
  33. elseif (b2 >=0);
  34. orientUHP = atan2(b2,a2);
  35. orientLHP = atan2(b1,a1);
  36. else
  37. end
  38. % The vector we will return
  39. orientation = [orientUHP orientLHP];
  40. return;