function EntropyTest_4Regions(SmallObjThres,RoundThresh,NhoodRad,Filter) %EntropyTest_4Regions(SmallObjThres,RoundThresh,NhoodRad,Filter) %This function process the entropy in the input *jpg images in the selected folder %Processing parameters can be changed inside the function %SmallObjThres defines the pixel count of an object which defines it as %small, and make it deletable. %Default Value = 8 %RoundThresh is a simmilarity coefficient with a perfect circle object. %the closer to 1, the closer to a perfect circle an object is. %Objects marked as circles are deleted by the preprocessing function %Default Value = 0.95 %NhoodRad defines Neighborhood size for entropy calculation 7 pixels by default---means 13x13 %Default Value = 7 %Filter is a flag which defines whether preprocessing is small and round %objects are deleted or not %Typical Example of usage: % EntropyTest_4Regions(8,0.95,7,1); % (c) Gustavo Liņan Cembrano % IMSE-CNM-CSIC % linan@imse-cnm.csic.es %% Getting input dir IM_PATH=uigetdir([],'Select Image Folder'); cd(IM_PATH) DeletePrevious = questdlg('Delete previous results?','Delete','No'); if(strcmp(DeletePrevious,'Yes')) delete('*.tiff') delete('*.csv'); elseif(strcmp(DeletePrevious,'Cancel')) return; else %Do nothing end %% Getting File name structure FileName=dir('*.jpg'); NFiles=length(FileName); msgstr=sprintf('%d Images Found in directory',NFiles); h=msgbox(msgstr); pause(1); delete(h) clc; %% Creating output variables Region1AvgEntropy=zeros(1,NFiles); Region2AvgEntropy=zeros(1,NFiles); Region3AvgEntropy=zeros(1,NFiles); Region4AvgEntropy=zeros(1,NFiles); Region1StdEntropy=zeros(1,NFiles); Region2StdEntropy=zeros(1,NFiles); Region3StdEntropy=zeros(1,NFiles); Region4StdEntropy=zeros(1,NFiles); Region1Area=zeros(1,NFiles); Region2Area=zeros(1,NFiles); Region3Area=zeros(1,NFiles); Region4Area=zeros(1,NFiles); ProcTime=zeros(1,NFiles); %Pixels count of an object which defines it as small, and make it deletable % SmallObjThres=8; %Simmilarity coefficient with a perfect circle object. 1-->Perfect Circle % RoundThresh=0.95; %Neighborhood for entropy calculation 7 pixels by default---means 13x13 %area % NhoodRad=7; STREL=strel('disk',NhoodRad); NHOOD=getnhood(STREL); %% Looping for j=1:NFiles t1=tic; %% Accessing file CURRENT_FILE=FileName(j).name; fprintf('###################################################### \n'); fprintf(' Accesing File %s\n', CURRENT_FILE); LocationsFile=strcat(CURRENT_FILE,'_Locations.mat'); LocFileExist=exist(LocationsFile,'file'); if(LocFileExist) load(LocationsFile) Nregions=length(whos('region*')); fprintf('Found location definition for %d regions\n',Nregions); DoIt=1; else MSG=sprintf('Locations file for %s does not exist skipping this file', CURRENT_FILE); h=msgbox(MSG); DoIt=0; end if(DoIt) INPUT_IM=imread(CURRENT_FILE); %% Increasing contrast INPUT_IM=imsharpen(INPUT_IM); %% Preprocessing regions before entropy calculation fprintf('Preprocessing regions and preparing images\n'); if(Nregions>=1) [Region1Image,Region1BinaryImage,Region1X,Region1Y]=PreProcessRegion(region1,INPUT_IM,SmallObjThres,RoundThresh,Filter); end if(Nregions>=2) [Region2Image,Region2BinaryImage,Region2X,Region2Y]=PreProcessRegion(region2,INPUT_IM,SmallObjThres,RoundThresh,Filter); end if(Nregions>=3) [Region3Image,Region3BinaryImage,Region3X,Region3Y]=PreProcessRegion(region3,INPUT_IM,SmallObjThres,RoundThresh,Filter); end if(Nregions==4) [Region4Image,Region4BinaryImage,Region4X,Region4Y]=PreProcessRegion(region4,INPUT_IM,SmallObjThres,RoundThresh,Filter); end %% Entropy Calculation fprintf(' Processing Entropy for file %s\n', CURRENT_FILE); fprintf('######################################################\n'); if(Nregions>=1) Region1EntropyImage=entropyfilt(Region1Image,NHOOD); Region1EntropyImage=Region1EntropyImage(:,:,1).*Region1EntropyImage(:,:,2).*Region1EntropyImage(:,:,3); Region1EntropyImage=Region1EntropyImage.*double(Region1BinaryImage); figure(1); imagesc(Region1EntropyImage);colormap jet; colorbar; print(gcf,'-dtiff',strcat(CURRENT_FILE,'_Region1EntropyImage.tiff')); Region1AvgEntropy(j)=mean(Region1EntropyImage(find(Region1BinaryImage==1))); Region1StdEntropy(j)=std(Region1EntropyImage(find(Region1BinaryImage==1))); Region1Area(j)=length(region1); end if(Nregions>=2) Region2EntropyImage=entropyfilt(Region2Image,NHOOD); Region2EntropyImage=Region2EntropyImage(:,:,1).*Region2EntropyImage(:,:,2).*Region2EntropyImage(:,:,3); Region2EntropyImage=Region2EntropyImage.*double(Region2BinaryImage); figure(2); imagesc(Region2EntropyImage);colormap jet; colorbar; print(gcf,'-dtiff',strcat(CURRENT_FILE,'_Region2EntropyImage.tiff')); Region2AvgEntropy(j)=mean(Region2EntropyImage(find(Region2BinaryImage==1))); Region2StdEntropy(j)=std(Region2EntropyImage(find(Region2BinaryImage==1))); Region2Area(j)=length(region2); end if(Nregions>=3) Region3EntropyImage=entropyfilt(Region3Image,NHOOD); Region3EntropyImage=Region3EntropyImage(:,:,1).*Region3EntropyImage(:,:,2).*Region3EntropyImage(:,:,3); Region3EntropyImage=Region3EntropyImage.*double(Region3BinaryImage); figure(3); imagesc(Region3EntropyImage);colormap jet; colorbar; print(gcf,'-dtiff',strcat(CURRENT_FILE,'_Region3EntropyImage.tiff')); Region3AvgEntropy(j)=mean(Region3EntropyImage(find(Region3BinaryImage==1))); Region3StdEntropy(j)=std(Region3EntropyImage(find(Region3BinaryImage==1))); Region3Area(j)=length(region3); end if(Nregions==4) Region4EntropyImage=entropyfilt(Region4Image,NHOOD); Region4EntropyImage=Region4EntropyImage(:,:,1).*Region4EntropyImage(:,:,2).*Region4EntropyImage(:,:,3); Region4EntropyImage=Region4EntropyImage.*double(Region4BinaryImage); figure(4); imagesc(Region4EntropyImage);colormap jet; colorbar; print(gcf,'-dtiff',strcat(CURRENT_FILE,'_Region4EntropyImage.tiff')); Region4AvgEntropy(j)=mean(Region4EntropyImage(find(Region4BinaryImage==1))); Region4StdEntropy(j)=std(Region4EntropyImage(find(Region4BinaryImage==1))); Region4Area(j)=length(region4); end %% Displaying resulting images pause(1); close all; %% Calculating mean std and area for entropy regions figure(1) imshow(INPUT_IM); if(Nregions>=1) text(Region1X,Region1Y,strcat('+ Region1Entropy=',num2str(Region1AvgEntropy(j))),'color',[0.6 0 0]); end if(Nregions>=2) text(Region2X,Region2Y,strcat('+ Region2Entropy=',num2str(Region2AvgEntropy(j))),'color',[0.6 0 0]); end if(Nregions>=3) text(Region3X,Region3Y,strcat('+ Region3Entropy=',num2str(Region3AvgEntropy(j))),'color',[0.6 0 0]); end if(Nregions==4) text(Region4X,Region4Y,strcat('+ Region4Entropy=',num2str(Region4AvgEntropy(j))),'color',[0.6 0 0]); end print(gcf, '-dtiff', strcat(CURRENT_FILE,'_ENTROPY_RESULTS.tiff')); ProcTime(j)=toc(t1); fprintf('Processing time = %1.3fs\n',ProcTime(j)); end end OutFile='Results.csv'; fid=fopen(OutFile,'w+'); fprintf(fid,'FileName,'); fprintf(fid,'Reg1AvgEntropy,Reg1StdEntropy,Reg1Area,'); fprintf(fid,'Reg2AvgEntropy,Reg2StdEntropy,Reg2Area,'); fprintf(fid,'Reg3AvgEntropy,Reg3StdEntropy,Reg3Area,'); fprintf(fid,'Reg4AvgEntropy,Reg4StdEntropy,Reg4Area'); fprintf(fid,'ProcTime(s)\n'); for j=1:NFiles fprintf(fid,'%s,%2.3f,%2.3f,%2.3f,%2.3f,%2.3f,%2.3f,%2.3f,%2.3f,%2.3f,%2.3f,%2.3f,%2.3f,%2.3f\n',FileName(j).name,... Region1AvgEntropy(j),Region1StdEntropy(j),Region1Area(j),... Region2AvgEntropy(j),Region2StdEntropy(j),Region2Area(j),... Region3AvgEntropy(j),Region3StdEntropy(j),Region3Area(j),... Region4AvgEntropy(j),Region4StdEntropy(j),Region4Area(j),... ProcTime(j)); end fclose(fid); end