% x = ImageRecognition(img,minsize,threshold,crop,rotate,plotting) % % Helper function for ExtractCoordinates. Finds the x-coordinate of the % centroid of an object in an image. % % img = Image % 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 % rotate = Video rotation angle (deg). Used to straighten skewed videos. % plotting = Whether the image is showed (1) or not (0). % % x = x-coordinate of the centroid of an object % % Image must contain only one object in specified size range. function [x] = ImageRecognition(RGB,minsize,threshold,crop,rotate,plotting) % This script is not relevant for writing the report, but it might be % interesting. Probably not. % If no objects are found, return -1; x=-1; % Rotate image if needed if rotate ~= 0 temp=imrotate(RGB,rotate); % White border instead of black rotationmask = ~imrotate(true(size(RGB)),rotate); temp(rotationmask&~imclearborder(rotationmask)) = 255; RGB=temp; end % Convert to black and white BW = im2bw(rgb2gray(RGB),threshold); % Invert image BW=not(BW); % Crop image if needed if crop ~= 0 BW=imcrop(BW,[0 0, size(BW,2), size(BW,1)-crop]); end % Fill any holes, so that regionprops can be used to estimate % the area enclosed by each of the boundaries BW = imfill(BW,'holes'); % Find boundaries [B,L] = bwboundaries(BW,'noholes'); % Find centroid and area of objects stats = regionprops(L,'Area','Centroid'); % Counts how many objects are found. Must not exceed 1. count=0; % Show image if plotting == 1 imshow(not(BW)); end % Loop over objects for k = 1:length(B) % Find objects of spesified size if (stats(k).Area >= minsize) count=count+1; if count > 1 % Too many droplets, return -2 x=-2; return; end; x=stats(k).Centroid(1); end; end; if plotting == 1 hold on; plot([x,x],[0, size(BW,1)],'-r'); hold off; end