Image segmentation with scikit-image and scikit-learn

The goal of this article is to experience a clustering image division algorithm. Let's target the camera man. (Everything runs on ipython --pylab.)

from skimage import data,io
image=data.camera()
io.imshow(image)

camera_original.png

The clustering target is each pixel. When the number of data is n and the number of dimensions is d, an n × d matrix must be created to use the clustering algorithm. Since the target is each pixel this time, first create a 262144 × 1 matrix with n = 512 × 512 = 262144 and d = 1.

X=image.reshape((-1,1))

Now you are ready. Let's cluster. Use Mean-Shift clustering. Mean-Shift clustering is considered to be suitable for image division because it is not necessary to determine the number of clusters in advance. This is already implemented in scikit-learn. How convenient ... There are some parameters, but I'll stick to the references.

from sklearn.cluster import MeanShift,estimate_bandwidth
bandwidth=estimate_bandwidth(X,quantile=0.2,n_samples=500)
ms=MeanShift(bandwidth=bandwidth,bin_seeding=True)
ms.fit(X)

This completes clustering. Let's see the result.

segmented_image=ms.labels_
segmented_image.shape=image.shape
imshow(segmented_image)
#matshow(segmented_image)

camera_segmented.png

Let's also look at the number of clusters.

print len(unique(ms.labels_))
3

The camera man has been split into three parts. You need the correct data to evaluate whether this is good or bad, but is it somewhere? The performance of Mean-Shift clustering depends largely on bandwidth. This time, sklearn had a function to estimate the bandwidth, so I tried using it, but I can't say anything because I haven't seen what I'm doing inside. For the time being, I am glad that I was able to experience image division using the scikit series.

Thank you very much.

References

Recommended Posts

Image segmentation with scikit-image and scikit-learn
[Python] Image processing with scikit-image
Photo segmentation and clustering with DBSCAN
Image segmentation with CaDIS: a Cataract Dataset
Isomap with Scikit-learn
DBSCAN with scikit-learn
Clustering with scikit-learn (1)
Clustering with scikit-learn (2)
PCA with Scikit-learn
kmeans ++ with scikit-learn
Generate and post dummy image data with Django
Cross Validation with scikit-learn
Image processing with MyHDL
Notes on HDR and RAW image processing with Python
Image recognition with keras
Make more visible performance indicators with PyTorch and scikit-learn
Multi-class SVM with scikit-learn
Clustering with scikit-learn + DBSCAN
Deep learning image analysis starting with Kaggle and Keras
Easy machine learning with scikit-learn and flask ✕ Web app
Learn with chemoinformatics scikit-learn
Image capture / OpenCV speed comparison with and without GPU
Image segmentation using U-net
Image processing with Python
POST the image with json and receive it with flask
DBSCAN (clustering) with scikit-learn
Practical machine learning with Scikit-Learn and TensorFlow-TensorFlow gave up-
With and without WSGI
Install scikit.learn with pip
Calculate tf-idf with scikit-learn
Image Processing with PIL
Image classification with self-made neural network by Keras and PyTorch
Build a machine learning scikit-learn environment with VirtualBox and Ubuntu
Web crawling, web scraping, character acquisition and image saving with python
[Let's play with Python] Image processing to monochrome and dots
Build GPU environment with GCP and kaggle official image (docker)
With me, cp, and Subprocess
Image download with Flickr API
Image processing with Python (Part 2)
Programming with Python and Tkinter
Encryption and decryption with Python
Read image coordinates with Python-matplotlib
Python and hardware-Using RS232C with Python-
Image editing with python OpenCV
Neural network with Python (scikit-learn)
Image upload & customization with django-ckeditor
Sorting image files with Python (2)
Sorting image files with Python (3)
100 image processing knocks !! (001 --010) Carefully and carefully
Easy image classification with TensorFlow
Parallel processing with Parallel of scikit-learn
Create Image Viewer with Tkinter
Image processing with Python (Part 1)
Tweet with image in Python
Sorting image files with Python
Image processing with Python (Part 3)
Image caption generation with Chainer
[Python] Linear regression with scikit-learn
Get image features with OpenCV
Super-resolution with SRGAN and ESRGAN
group_by with sqlalchemy and sum