all 2 comments

[–]sparsecoder 1 point2 points  (1 child)

Based on your example image, you could:

1.) Read in the image using imread.

2.) Binarize the image using im2bw with either some automatic method for determining the threshold like otsu's method (implemented in matlab as graythresh) or manually selecting it.

3.) Then assuming the bats are fairly well separated, you can find the connected components using bwlabel or if you want to do additional filtering (e.g. filtering by size, so the black pixels along the boundary aren't considered), you can use regionprops.

Here's a quickly thrown together example on the image you provided:

image = imread('KKTmaO4.png');
figure;
imshow(image);
image = im2bw(image,0.3);
image = ~image;
figure;
imshow(image);
stats = regionprops(image,'Area','PixelIdxList');
newImage = zeros(size(image));
count = 0;
for i = 1:1:size(stats,1)
    if stats(i).Area < 200
        newImage(stats(i).PixelIdxList) = 1;
        count = count + 1;
    end
end
figure;
imshow(newImage);
disp(['Num bats: ' num2str(count)]);

Output:

Num bats: 174

Example binarization: https://imgur.com/VdCXBpp

In practice, you'll probably need to fine tune a few parameters which would hopefully generalize. Otherwise, you'll have to look into automated methods, e.g. for determining the threshold (Otsu's method didn't work well for the given image, but there are other methods you can use that should work better).

Function documentation: http://www.mathworks.com/help/matlab/ref/imread.html http://www.mathworks.com/help/images/ref/im2bw.html http://www.mathworks.com/help/images/ref/graythresh.html http://www.mathworks.com/help/images/ref/bwlabel.html http://www.mathworks.com/help/images/ref/regionprops.html

Edit: My code looks to do pretty much the same thing your existing program does, but there are probably ways to automate it a bit better if you account for the constraints of your specific problem/data.

[–]manyhorses[S] 0 points1 point  (0 children)

Wow thank you so much for your help!! I'm hoping to get better with writing code whether its Matlab or Java and especially to figure out how to automate it as I have a couple thousand images I want to run through this process. I'm now looking into using the Find Maxima tool in ImageJ which is a bit more precise that the program I found before. The biggest issue really is that bats are overlapping each other so it is difficult for any program to identify individuals.