% x = ExtractCoordinates(file, minsize, threshold, crop, rotate, first, last) % % Calculates droplet position as a function of time from oscillation video. % % Required input parameters: % file = Video file % minsize = Minimum size of the searched area in pixels (e.g. 100) % threshold = Threshold for black-and-white conversion (0..1) % crop = Amount video is cropped from bottom. Used to discard substrate % from analysis. % Optional input parameters: % rotate = Video rotation angle (deg). Used to straighten skewed videos. % first = First frame to be included in the analysis % last = Last frame to be included in the analysis % % Output parameters: % x = Resolved droplet coordinates. % % The function searches for areas of black pixels in the frames exceeding % minsize and calculates x-coordinate of the center of the area. In the end % the coordinates are plotted. Frames can contain only one object in the % specified size range. If there are multiple objects, the returned % coordinate is -2. If no object was found, the returned coordinate is -1. % At the beginning of video the droplet might not be compeletely visible % and the first coordinates show value -1. This is ok, these coordinates % will be discarded later. If negative values appear in the middle of % oscillation, something is wrong. You can try increasing minsize or making % the video brighter/darker. function x = ExtractCoordinates(varargin) % Read input parameters file = varargin{1}; minsize = varargin{2}; threshold = varargin{3}; crop = varargin{4}; if length(varargin) > 4 rotate = varargin{5}; else rotate = 0; end if length(varargin) > 5 first = varargin{6}; else first = 1; end if length(varargin) > 6 last = varargin{7}; else last = -1; % Will be checked later end % Open video video=VideoReader(file); % Check the number of frames if needed if last == -1 || last>video.NumberOfFrames last = video.NumberOfFrames; end xcount=1; % counts coordinates xcountmax=last-first+1; % number of coordinates count=0; x=zeros(1,xcountmax); % reserve memory for coordinates while(xcount <= xcountmax) frame=xcount+first-1; % Find the x-coordinate x(xcount)=ImageRecognition(read(video, frame),minsize,threshold,crop,rotate,0); % Delete previously printed line on screen fprintf(1, repmat('\b',1,count)); % Check for errors if x(xcount)==-2 disp(''); disp(['Error: Several "droplets" found in frame ' num2str(frame) '! Try increasing minsize or make the video brighter.']); figure; ImageRecognition(read(video, frame),minsize,threshold,crop,rotate,1); return end count = fprintf('Coordinates solved: %i/%i',xcount, xcountmax); xcount=xcount+1; end fprintf('\n'); % Plot the coordinates figure; plot(x,'k.'); xlabel('t (ms)'); ylabel('x (pixels)'); end