Image Features Extraction Package¶
package doc: https://rempic.github.io/Image-Features-Extraction/¶
Tutorial¶
This Python package allows the fast extraction and classification of features from a set of images. The resulting data frame can be used as training and testing set for machine learning classifier.
This package was originally developed to extract measurements of single cell nuclei from microscopy images (see figure above). The package can be used to extract features from any set of images for a variety of applications. Below it is shown a map of Boston used for city density and demographic models.
% matplotlib inline
import matplotlib.pyplot as plt
import image_features_extraction.Images as fe
IMGS = fe.Images('../images/REMI')
for i in IMGS:
print(i.file_name())
IMG_POINTS = IMGS.item(1)
IMG_8BIT = IMGS.item(0)
#IMG_POINTS.set_image_intensity(IMG_8BIT)
fig, ax = plt.subplots(figsize=(20, 20))
ax.imshow(IMG_POINTS.get_image_segmentation(overlap_image=IMG_8BIT))
vor = IMG_POINTS.Voronoi()
fig = plt.figure(figsize=(20,20))
plt.imshow(vor.get_voronoi_map(), cmap=plt.get_cmap('pink'))
IMG_MIX = IMG_8BIT.get_image()/7 + vor.get_voronoi_map() + IMG_POINTS.get_image()/30
fig = plt.figure(figsize=(20,20))
plt.imshow(IMG_MIX, cmap=plt.get_cmap('pink'))
features = vor.features(['area','perimeter','centroid','minor_axis_length','major_axis_length', 'eccentricity','extent','bbox_area', 'convex_area', 'equivalent_diameter', 'euler_number', 'orientation','solidity'], prefix='remi_',)
features.set_class_name('class')
features.set_class_value('REMI')
df = features.get_dataframe(include_class=True)
df
import numpy as np
np.log(df.mean()).plot(kind='barh', figsize=(10,10))
i1
Features extraction for spatial classification of images¶
The image below shows a possible workflow for image feature extraction: two sets of images with different classification labels are used to produce two data sets for training and testing a classifier
An example of Collection-object and Iterator implementation¶
The object 'Image' includes the function Voronoi(), which returns the object Voronoi of my package Voronoi_Features. The Voronoi object can be used to measure the voronoi tassels of each image regions. It includes >30 measurements. Below an example of voronoi diagrams from the image shown above
Image features extraction for city density and demographic analysis modelling¶
Create the Images root object and laod the images contained in the folder
% matplotlib inline
import matplotlib.pyplot as plt
import image_features_extraction.Images as fe
IMGS = fe.Images('../images/CITY')
IMG = IMGS.item(0)
print(IMG.file_name())
fig, ax = plt.subplots(figsize=(20, 20))
ax.imshow(IMGS.item(0).get_image_segmentation())
features = IMG.features(['label', 'area','perimeter', 'centroid', 'moments'])
df2 = features.get_dataframe()
df2.head()
# SHOW THE FOUND CENTROIDS
fig, ax = plt.subplots(figsize=(20, 20))
plt.plot(df2.centroid_x,df2.centroid_y,'.r' )
h = plt.hist(df2.area,100)
Image features extraction for cellular spatial analysis¶
Images show cell nuclei
% matplotlib inline
import matplotlib.pyplot as plt
import image_features_extraction.Images as fe
IMGS = fe.Images('../images/CA/1')
# the iterator at work ...
for IMG in IMGS:
print(IMG.file_name())
fig, ax = plt.subplots(figsize=(20, 20))
ax.imshow(IMGS.item(1).get_image_segmentation())
An example of measurement and visualization of a property, e.g., area¶
IMG = IMGS.item(1)
REGS = IMG.regions()
areas = REGS.prop_values('area')
plt.plot(areas)
plt.ylabel('region area (px^2)')
h = plt.hist(df2.area,100)
VORONOI FEATURES¶
vor = IMG.Voronoi()
vor = IMG.Voronoi()
IMG_VOR = vor.get_voronoi_map()
fig = plt.figure(figsize=(20,20))
plt.imshow(IMG_VOR, cmap=plt.get_cmap('jet'))
i1 = IMGS.item(0).get_image_segmentation()
i2 = vor.get_voronoi_map()
i3 = i1[:,:,0] + i2/1000
fig = plt.figure(figsize=(yinch,xinch))
plt.imshow(i3, cmap=plt.get_cmap('Reds'))
Feature from the image only¶
features1 = IMG.features(['area','perimeter','centroid','bbox', 'eccentricity'])
features1.get_dataframe().head()
Features from the voronoi diagram only¶
features2 = vor.features(['area','perimeter','centroid','bbox', 'eccentricity'])
features2.get_dataframe().head()
Merge features from the image + the voronoi diagram¶
features3 = features1.merge(features2, how_in='inner')
features3.get_dataframe().head()
Add class name and value¶
features3.set_class_name('class')
features3.set_class_value('test_class_val')
features3.get_dataframe(include_class=True).head()
To measure intensity from image regions¶
The example below shows how to associate a grayscale image to a binary one for intensity measurement. The package uses intenally a very simple segmentation algorithm based on an Otsu Thresholding method for segmentation of binary images. The goal of the package in not to segment images but to measurement their segmented features. The corect way to use this package is by using as input pre-segmented binary images and if intensity measurement are needed you can assaciate the original grayscale image.
IMG = IMGS.item(1)
IMG.set_image_intensity(IMGS.item(0))
features = IMG.features(['label', 'area','perimeter', 'centroid', 'moments','mean_intensity'])
df = features.get_dataframe()
df.head()
Plot area vs perimeter and area histogram¶
plt.plot(df.area, df.mean_intensity, '.b')
plt.xlabel('area')
plt.ylabel('mean_intensity')
An example of how save measured features¶
This package includes the class Features for data managment layer, which is used to separate the business from the data layer and allow easy scalability of the data layer.
import image_features_extraction.Images as fe
IMGS = fe.Images('../images/EDGE')
storage_name = '../images/DB1.csv'
class_value = 1
for IMG in IMGS:
print(IMG.file_name())
REGS = IMG.regions()
FEATURES = REGS.features(['area','perimeter', 'extent', 'equivalent_diameter', 'eccentricity'], class_value=class_value)
FEATURES.save(storage_name, type_storage='file', do_append=True)
Pytest: Units test¶
!py.test