Skip to main content

A package for counting BLOB objects

Project description

A new tool for counting the number of small objects.

Open in Code Ocean

DOI: https://doi.org/10.24433/CO.4823110.v1

This is under review.

bugcount is a useful tool for entomologists to count the number of bugs.

bugcount has been downloaded by 12155 users worldwide as of July 21, 2023.

How to install this package:

$ pip install bugcount

$ pip install bugcount --force-reinstall --no-cache-dir --no-binary :all:

You need to update the opencv-python library to the latest version:

$ pip install -U opencv-python --user

How to use this bugcount:

In command terminal, type bugcount file-name or bugcount file-name Canny-coeffient

$ bugcount filename

or

You must download pillbug.png file:

https://github.com/y-takefuji/counting-for-entomologists/raw/main/pillbug.png

$ bugcount pillbug.png

$ bugcount filename Canny-coeffient

Default Canny-coeffient is 75.

or

$ bugcount pillbug.png 10

$ bugcount pillbug.png 100

pillbug.png file is a target image for counting the number of bugs.

The smaller Canny coeffient, the more minute objects can be detected.

Detailed program of bugcount.py and pillbug_count.py

bugcount has been downloaded by 5513 users worldwide.

This new repository introduces a new tool on object counting for entomologists.

bugcount.py or bugcount is an executable pypi version program while 
pillbug_count.py is a modifiable program for researchers. 

Counting the number of dead bugs or insect corpses on a plane is tedius 
and a very time-consuming task. 

The proposed approach is based on BLOB. The term "BLOB" stands for 
"Binary Large OBject".

The proposed algorithm is based on the state-of-the-art Canny edge 
detection and Gaussian Blur function using OpenCV library in Python.

Canny edge detection is used for creating blobs and Gaussian Blur function 
for eliminating noises.

Without machine learning, the Python program (pillbug_count.py) can count 
the number of objects in a picture with a uniform colored background.

The conventional machine learning needs to collect and annotate a variety 
of images which is a time-consuming task.

Since I am one of pioneers of neural computing, I know better than anyone 
that collecting images and annotating them is tedious and a waste of time.
The citation number of the following publications is based on Google Scholar:

Deep learning vs shallow learning published in IEEE COMPUTER in 1992: Functional Link Net Computing: Theory, system Architecture and Functionalities (578 cited)

First paper on channel assignment problems published in IEEE Trans. on Vehicular technology in 1992: A neural network parallel algorithm for channel assignment problems in cellular radio networks (349 cited )

First book for neural optimization published in 1992: Neural Network Parallel Computing (324 cited)

First Quiz bowl world champion in NIPS 2017: AI defeating human champions using Wikipedia knowledge (241 cited)

First image processing in IEEE medical imaging in 1992: Optimization neural networks for the segmentation of magnetic resonance images (232 cited)

First paper published in Science in 1989: A near-optimum parallel planarization algorithm, Science, pp.1221-1223 (125 cited)

How to run pillbug_count.py

The target objects in a picture for counting must be separated or isolated. 
Overlapped objects can be counted as one object.

This repository shows how to use a Python program (pillbug_count.py) 
for novice users to automatically count the number of dead bugs or 
the number of insect corpses on a plane for entomologists.

In order to run pillbug_count.py, you should see the following repository 
for installing the necessary libraries:
 How to install and use libraries in Python for novice users

1. To install an executable Python environment on your operating system 
   (Windows, Mac, Linux), 
   download a Miniconda3 .sh file or .exe file for Python3.8 or Python3.7 
   from the following site:

https://docs.conda.io/en/latest/miniconda.html

2. Double-click Miniconda3 .exe file or run the following bash command on
   your terminal:
   $ bash Miniconda3XXX.sh
3. You need to install opencv libray, 
   type the following pip command for installation:
   $ pip install opencv-python
4. To run pillbug_count.py, type the following command for testing:
   $ python pillbug_count.py pillbug.png
Result: 53 objects (blobs) are found in pillbug.png file.
Use flies.png for further testing.
$ python pillbug_count.py flies.png

You should resize an original picture for counting the number of dead bugs. And/or you should tune the Canny coefficient.

How to tune parameters for counting in general

pillbug_count.py is shown:

import cv2,sys
if len(sys.argv)<2: 
 print("enter figure name")
 sys.exit()

img = cv2.imread(sys.argv[1])
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5,5), 0)
blurred = cv2.GaussianBlur(blurred, (7,7), 0)

cv2.imshow("gray scale", gray)
cv2.imwrite("gray.png", gray)
cv2.imshow("blurred", blurred)
cv2.imwrite("blur.png", blurred)
coeff=int((blurred.max()-blurred.min())/100)
if coeff==1: coeff=1 
else: coeff=3
outline = cv2.Canny(blurred, 0, 75*coeff)
outline= cv2.GaussianBlur(outline, (3,3), 0)
cv2.imshow("The edges", outline)
cv2.imwrite("edges.png", outline)
(_, cnts, _) = cv2.findContours(outline, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#version4
#( cnts, _)=cv2.findContours(outline,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, cnts, -1, (0, 255, 0), 2)
cv2.putText(img,str(len(cnts)),(30,30),cv2.FONT_HERSHEY_SIMPLEX,1,(255,0,0),2)
cv2.imshow("Result", img)
cv2.imwrite("r.png",img)
print("%i blobs" % len(cnts))
cv2.waitKey(0)

How to resize a picture using resize.py

The most important tuning point lies in the size of a picture containing 
the target objects.
The following Python program can be used for resizing the picture of p.jpg
(picture size: 4032x3024). This program is tuned for a smartphone.
resize.py can convert p.jpg (size: 4032x3024) to p.png (size: 550 x 540).
The photo p.jpg was taken with my smartphone. 
resize.py is to generate the resized p.png file from p.jpg.

$ python resize.py p.jpg

resize.py can be changed by modifying integer 756 in img = img.resize((756,int(756*h/w)), Image.ANTIALIAS).

resize.py can rotate, resize, change brightness, image-enhance an image, and save the result in p.png file.

from PIL import Image,ImageEnhance as ie
import sys

img = Image.open(sys.argv[1])
w,h=img.size
img=img.rotate(270,expand=False)
img = img.resize((756,int(756*h/w)), Image.ANTIALIAS)
img=ie.Brightness(img)
eimg=img.enhance(1.9)
eimg=eimg.crop((100,10,650,550))
eimg.save("p.png") 

How to tune Canny coefficient in pillbug_count.py

You should tune and change Canny coefficient instead of "75". 
outline = cv2.Canny(blurred, 0, 75*coeff)

Cell counting

In bioprocessing, reagents such as A100 can be used to disaggregate 
aggregated cell masses and automate cell counting.

Image source: https://bitesizebio.com/30184/quick-easy-automatic-cell-counting/
$ bugcount cells.png

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bugcount-0.0.11.tar.gz (5.7 kB view hashes)

Uploaded Source

Built Distribution

bugcount-0.0.11-py3-none-any.whl (5.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page