FINDFLY Usage: [x, y, bodyline, sqr] = FindFly(chunk, sqrsize) This function takes in a single image matrix (chunk) and finds the fly. First, it finds the brightest pixel in the image, then grabs a square of size 2*sqrsize pixels around the fly. The center of mass of this square is calculated (mass = pixel level) and that is returned as the fly position. The variable 'sqr' can be returned, which contains the limits defining a bounding box around the fly. This function also returns the body axis orientation vector. The body axis is calcuated using FlyOrient. Fly Orient returns the bodyline vector, which is a 2-element vector containing two angles, one in the upper half plane, and one in the lower half plane. These angles (which are complementary) define the body axis. The noise threshold required for FlyOrient is currently fixed at 0.2. If you wish to change this threshold, you must go into the FindFly.m file and hard-code a different value.
0001 function [x, y, bodyline, sqr] = FindFly(chunk, sqrsize) 0002 0003 %FINDFLY 0004 % 0005 % Usage: 0006 % [x, y, bodyline, sqr] = FindFly(chunk, sqrsize) 0007 % 0008 % This function takes in a single image matrix (chunk) and finds the fly. 0009 % First, it finds the brightest pixel in the image, then grabs a square 0010 % of size 2*sqrsize pixels around the fly. The center of mass of this 0011 % square is calculated (mass = pixel level) and that is returned as the 0012 % fly position. The variable 'sqr' can be returned, which contains the 0013 % limits defining a bounding box around the fly. 0014 % 0015 % This function also returns the body axis orientation vector. The 0016 % body axis is calcuated using FlyOrient. Fly Orient returns the bodyline 0017 % vector, which is a 2-element vector containing two angles, one in the upper 0018 % half plane, and one in the lower half plane. These angles (which are 0019 % complementary) define the body axis. The noise threshold required for 0020 % FlyOrient is currently fixed at 0.2. If you wish to change this 0021 % threshold, you must go into the FindFly.m file and hard-code a 0022 % different value. 0023 0024 0025 % Written by Dan Valente 0026 % 28 September 2006 0027 0028 noise_thresh = 0.2; % for the FlyOrient function 0029 0030 brightest_pixel_level = max(max(chunk)); 0031 [brightpix_row brightpix_col] = find(chunk >= (brightest_pixel_level)); 0032 0033 %just in case more spots have the same brightness, only take one... 0034 width = length(chunk(1,:)); 0035 height = length(chunk(:,1)); 0036 0037 row_pos = brightpix_row(1); 0038 col_pos = brightpix_col(1); 0039 0040 y = row_pos; 0041 x = col_pos; 0042 0043 0044 %take subset of pixels around fly. need to take enough to ensure 0045 %entire fly is captured. 0046 row_lower_limit = row_pos-sqrsize; 0047 row_upper_limit = row_pos+sqrsize; 0048 if (row_lower_limit <= 0) 0049 row_lower_limit = 1; 0050 end 0051 if (row_upper_limit >= height ) 0052 row_upper_limit = height; 0053 end 0054 0055 col_lower_limit = col_pos-sqrsize; 0056 col_upper_limit = col_pos+sqrsize; 0057 if (col_lower_limit <= 0) 0058 col_lower_limit = 1; 0059 end 0060 if (col_upper_limit >= width ) 0061 col_upper_limit = width; 0062 end 0063 0064 sqr = [row_lower_limit row_upper_limit col_lower_limit col_upper_limit]; 0065 0066 %Now grab center of mass of fly (i.e. CM of subset image pixel intensities) 0067 temp_mat = chunk(row_lower_limit:row_upper_limit,col_lower_limit:col_upper_limit); 0068 0069 x2 = [1:length(temp_mat(1,:))]'; 0070 y2 = [1:length(temp_mat(:,1))]'; 0071 total = sum(sum(temp_mat)); 0072 x = sum(temp_mat*x2)/total+col_lower_limit-1; 0073 y = sum(temp_mat'*y2)/total+row_lower_limit-1; 0074 0075 0076 % Find Fly orientation from this frame (in radians) 0077 bodyline = FlyOrient(temp_mat, noise_thresh); 0078 0079 return;