clear all close all % Choose directory containing images dn = uigetdir('c:\MyDefaultPictureFolder', 'Select the directory'); % Make list of image files, change to jpg if in other format fn=dir([dn '\*.jpg']); % Loop through all images for n=1:length(fn) disp(['Opening file ' num2str(n) ' of ' num2str(length(fn))]); % Import image I=imread([dn '\' fn(n).name]); %Display image figure imshow(I) % Promt user for input, add more fields here Name{n}=input('Name: ','s');; Pop{n}=input('Pop: ','s'); Sex{n}=input('Sex: ','s'); % Focus on region of interest disp('Choose coarse region of interest') I=imcrop(I); % Select precise ROIs disp('Select the trunk of the lizard') [BW, px, py]=roipoly(I); imshow(I(min(py):max(py),min(px):max(px),:)) disp('Select a blue spot on the lizard') [BWb, pxb, pyb]=roipoly(I); disp('Select the regions of the lizard and photograph that are not blue spots') [BWo, pxo, pyo]=roipoly(I); % Contrast function based on multivariate regression BW=BW(:); BWb=BWb(:); BWo=BWo(:); x = find(BW); xb = find(BWb.*BW); xo = find(BWo.*BW); res=size(I); RGB=double(reshape(I,[res(1)*res(2) res(3)]))/255; lizBlue=RGB(xb,:); lizOther=RGB(xo,:); obs=[lizBlue; lizOther]; obs=[obs(:,1).^0 obs obs(:,1:2).*obs(:,2:3)]; contrastf=obs\([lizBlue(:,1).^0; lizOther(:,1)*0]); isBlue=[RGB(:,1).^0 RGB RGB(:,1:2).*RGB(:,2:3)]*contrastf; isBlue=BW.*isBlue>sqrt(mean([lizBlue(:,1).^0; lizOther(:,1)*0])); % Calculate ratio of trunk area containing blue spots in percent. blueRatio{n}=100*sum(isBlue)/sum(BW); disp([num2str(blueRatio{n}) '% blue']) % Display sub region figure subplot(2,1,1) imshow(I) % Display gray scale image with detected patches overlay subplot(2,1,2) %imcol=double(reshape(I,[res(1)*res(2) res(3)]))/255; %imBlue=[imcol(:,1).^0 imcol imcol(:,1:2).*imcol(:,2:3)]*contrastf; %imBlue=imBlue>(sqrt(mean([lizBlue(:,1).^0; lizOther(:,1)*0]))); isBlue=reshape(isBlue,[res(1) res(2) 1]); BW=reshape(BW,[res(1) res(2) 1]); comp(:,:,1)=mean(I,3)/255; comp(:,:,2)=mean(I,3)/255; comp(:,:,3)=mean(I,3)/255+BW.*isBlue; imshow(comp) clear comp end % Write spread sheet captions={'File' 'Name','Pop', 'Sex','blueRatio'}; xlswrite([dn '\BlueSideData.xls'], [captions;{fn.name}' Name' Pop' Sex' blueRatio'])