[PYTHON] Circular object recognition using Hough transform

Note that I learned that there is a way to recognize and cut out a circular object.

Preparation

OpenCV preparation

First, make OpenCV available from Python.

In the recent Anaconda environment, Python 3.6 is installed, but the OpenCV3 conda package for the osx-64 environment that can be installed on Python 3.6 has not been released, so prepare a Python 3.5 environment.

For the time being, I created a virtual environment with conda.

conda create -n py35 python=3.5 anaconda

Then, after source ~ / .pyenv / versions / anaconda3-4.4.0 / bin / activate py35, do conda install --channel https://conda.anaconda.org/menpo opencv3.

Preparation of sample image

I didn't have the right image, so I downloaded and used the ImageJ sample.

embryos_mini.jpg

Implement

Try to implement it while referring to http://docs.opencv.org/3.1.0/da/d53/tutorial_py_houghcircles.html.

houghcircles_sample.py


import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('img/embryos.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.medianBlur(gray, 5)

circles = cv2.HoughCircles(blur, cv2.HOUGH_GRADIENT,
								dp=1, minDist=20, param1=50, param2=30,
								minRadius=10, maxRadius=100)
circles = np.uint16(np.around(circles))
for (x, y, r) in circles[0]:
	cv2.circle(img, (x, y), r, (0, 255, 0), 2)
	cv2.circle(img, (x, y), 2, (0, 0, 255), 3)

plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

Execution result

result.png

Not bad. Depending on the adjustment of param1 and param2, it seems that cells can be counted properly.


Today's code

Recommended Posts

Circular object recognition using Hough transform
Try real-time object recognition using YOLOv2 (TensorFlow)
Age recognition using Pepper's API
I tried face recognition using Face ++
Discrete cosine transform using chainer.links.Linear
Perform handwriting recognition using Pylearn2
Object co-localization for face recognition