Oil Red Count

%%%%%%%%%%%%%%%%%%%%%%%%

% Script for Oil Red Count in .jpg images.

%%%%%%%%%%%%%%%%%%%%%%%%

% Read images in current folder.

files = dir('file.jpg');

fileID = fopen('file2.txt','w');

pas=pwd;

for i=1:length(files)

[pathname,filename,extension] = fileparts(files(i).name);

I = imread(files(i).name);

%I_cropped = I(400:900, 465:965); % Crop image. (optional)

figure; imshow(I)

% Adjust color intensity (0=max intensity; 1=less intensity). (optional)

RED=1;

GREEN=1;

BLUE=1;

I = imadjust(I,[0 0 0; RED GREEN BLUE],[]);

figure; imshow(I)

I = rgb2gray(I); % Convert to grayscale

figure; imshow(I)

I = imcomplement(I); % Invert image colors (optional) Useful for very bright images.

% Equalize. (optional)

I_eq = imadjust(I);

imshow(I_eq)

% Convert to black and white.

bw = im2bw(I_eq, graythresh(I_eq));

imshow(bw)

i=1;

st=100; % Step to deacreas large object threshold.

MAX=30000;

for tam=MAX:-st:0

bw4 = bwareaopen(bw, tam);

imshow(bw4)

bw5=bw-bw4; % Remove large objects from original bw image.

TotalArea(i)=bwarea(bw5);

% Count

[L, num] = bwlabel(bw4);

TotalNumber(i)=num;

fprintf(fileID, ' %s %s %s\t %s\t %6.0f\n', pas, '/', filename,'Count= ', num);

i=i+1; tam

end

% Conversion factor from pix to um2.

pix_area=(50/200)^2; % Area of one pixel in um2

f=pix_area;

tot=sum(TotalNumber(1:end-1));

plot(TotalArea(1:end-1)*f,TotalNumber(1:end-1)/tot,'k','Linewidth',4)

set(gca,'FontSize',25,'Fontname','Timesnewroman')

xlabel('Oil Red Droplets Size [\mum^2]','FontSize',25)

ylabel('n/N','FontSize',25)

end

fclose(fileID);