Cell count

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

% Octave script for cell count in .jpg images.

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

files = dir('file.jpg'); % Read all images in current folder.

fileID = fopen('CellCount.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.

imshow(I)

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

RED=1;

GREEN=1;

BLUE=0.1;

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

imshow(I)

I = rgb2gray(I); % Convert to grayscale.

imshow(I)

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

% Equalize

I_eq=imadjust(I);

imshow(I_eq)

% Convert to black and white.

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

imshow(bw)

bw2 = imfill(bw,'holes');

imshow(bw2)

bw3 = imopen(bw2, ones(5,5));

imshow(bw3)

bw4 = bwareaopen(bw3, 20000);

imshow(bw4)

bw4_perim = bwperim(bw4);

imshow(bw4_perim)

% Cell count.

[L, num] = bwlabel(bw4); num

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

end

fclose(fileID);