In [1]:
# ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: #
# Date: 03.10.2017 #
# Author: Q.Liu #
# ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: #
""":
Compute and visualize GLCM from each texture
Histogram transforms should be applied and evaluated
Requantizing image before glcm calculation.
"""
Out[1]:
In [2]:
# Standard library imports
import os
import numpy as np
import time
# Third party imports
import matplotlib.pyplot as plt
import cv2
from skimage import img_as_float,exposure
# Private libs
from glcm_texture import *
In [3]:
# define default common used variables
output_dir = "./output"
input_dir = './data'
cmap = plt.get_cmap('jet')
features_list = ['contrast','dissimilarity','homogeneity','entropy','energy',
'correlation','ASM','cluster_shade']
weights_list = ['contrast','dissimilarity','homogeneity',
'correlation','cluster_shade']
# fist test image file
image_file = './data/sub-mosaic2-1.png'
In [4]:
# read image as grayscale
gray_img = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE)
# simple histogram equalization
equ_img = cv2.equalizeHist(gray_img)
# apply adaptive histogram equalization to original image
clahe = cv2.createCLAHE(clipLimit=4.5, tileGridSize=(9, 9))
adap_equ = clahe.apply(gray_img)
In [5]:
def get_filename(filepath):
return os.path.splitext(os.path.basename(filepath))[0]
plt.subplot(2, 3, 1)
plt.title("original image\n ({0})".format(get_filename(image_file)))
plt.imshow(gray_img,'gray')
plt.subplot(2, 3, 4)
plt.title("hist of \noriginal image ")
plt.hist(gray_img.ravel(), 256, [0, 256])
plt.subplot(2, 3, 2)
plt.title("adap_hist_equa")
plt.imshow(adap_equ, 'gray')
plt.subplot(2, 3, 5)
plt.title("hist of \nadap_hist_equa ")
plt.hist(adap_equ.ravel(), 256, [0, 256])
plt.subplot(2, 3, 3)
plt.title("simple hist_equa")
plt.imshow(equ_img, 'gray')
plt.subplot(2, 3, 6)
plt.title("hist of \nsimple hist_equa ")
plt.hist(equ_img.ravel(), 256, [0, 256])
plt.tight_layout()
plt.show()
In [6]:
# ::::::::::::::::::::::::::::::::::
# Requantize image and compute normalized and symmetric glcm
# by using get_glcm function norm and symm default true,
# isotropic default false, distance/offset default 1
levels = 16
req_img = requantize(adap_equ,level_num=levels)
scl_img = scale_image(req_img, 0, levels)
glcm_0 = get_glcm(scl_img, offsets=[1], angles=[0], levels=levels+1)
glcm_45 = get_glcm(scl_img, offsets=[1], angles=[np.pi/4], levels=levels+1)
glcm_90 = get_glcm(scl_img, offsets=[1], angles=[np.pi/2], levels=levels+1)
glcm_135 = get_glcm(scl_img, offsets=[1], angles=[3*np.pi/4], levels=levels+1)
glcm_iso = get_glcm(scl_img, offsets=[1], isotropic=True, levels=levels+1)
glcm_45_135 = get_glcm(scl_img, offsets=[1],
angles=[np.pi/4, 3*np.pi/4], levels=levels+1)
glcm_0_90 = get_glcm(scl_img, offsets=[1],
angles=[0, np.pi/2], levels=levels+1)
In [7]:
fig,(ax0, ax1) = plt.subplots(nrows=2)
im1 = ax0.imshow(scl_img, 'gray')
ax0.set_title("reqantized image ({0})".format(get_filename(image_file)))
fig.colorbar(im1, ax=ax0)
im2 = ax1.imshow(glcm_0,cmap=cmap)
ax1.set_title("glcm of angles[0]")
fig.colorbar(im2, ax=ax1)
fig.tight_layout()
plt.show()
In [8]:
fig,(ax0, ax1) = plt.subplots(nrows=2)
im1 = ax0.imshow(glcm_45,cmap=cmap)
ax0.set_title("glcm of angles[45]")
fig.colorbar(im1, ax=ax0)
im2 = ax1.imshow(glcm_90,cmap=cmap)
ax1.set_title("glcm of angles[90]")
fig.colorbar(im2, ax=ax1)
fig.tight_layout()
plt.show()
In [9]:
fig,(ax0, ax1) = plt.subplots(nrows=2)
im1 = ax0.imshow(glcm_135,cmap=cmap)
ax0.set_title("glcm of angles[135]")
fig.colorbar(im1, ax=ax0)
im2 = ax1.imshow(glcm_iso,cmap=cmap)
ax1.set_title("isotropic glcm")
fig.colorbar(im2, ax=ax1)
fig.tight_layout()
plt.show()
In [10]:
fig, ax = plt.subplots()
im = ax.imshow(glcm_45_135, cmap=cmap)
ax.set_title("glcm of \nangles[45,135]")
fig.colorbar(im, ax=ax)
fig, ax = plt.subplots()
im = ax.imshow(glcm_0_90, cmap=cmap)
ax.set_title("glcm of \nangles[0,90]")
fig.colorbar(im, ax=ax)
plt.show()
In [11]:
# Weights visualization
# weights_list = ['contrast','dissimilarity','homogeneity',
# 'correlation','cluster_shade']
cont_w = glcm_weights(glcm_iso, name='contrast')
diss_w = glcm_weights(glcm_iso, name='dissimilarity')
homo_w = glcm_weights(glcm_iso, name='homogeneity')
corr_w = glcm_weights(glcm_iso, name='correlation')
clus_w = glcm_weights(glcm_iso, name='cluster_shade')
In [12]:
fig, ax = plt.subplots()
im = ax.imshow(cont_w, cmap=cmap)
ax.set_title("contrast weights of \nisotropic glcm")
fig.colorbar(im, ax=ax)
fig, ax = plt.subplots()
im = ax.imshow(diss_w, cmap=cmap)
ax.set_title("dissimilarity weights of \nisotropic glcm")
fig.colorbar(im, ax=ax)
fig, ax = plt.subplots()
im = ax.imshow(homo_w, cmap=cmap)
ax.set_title("homogeneity weights of \nisotropic glcm")
fig.colorbar(im, ax=ax)
fig, ax = plt.subplots()
im = ax.imshow(corr_w, cmap=cmap)
ax.set_title("correlation weights of \nisotropic glcm")
fig.colorbar(im, ax=ax)
fig, ax = plt.subplots()
im = ax.imshow(clus_w, cmap=cmap)
ax.set_title("cluster_shade weights of \nisotropic glcm")
fig.colorbar(im, ax=ax)
plt.show()
In [13]:
# computer feature images with sliding windows
# image file
image_file = './data/sub-mosaic1-1.png'
gray_img = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE)
# apply adaptive histogram equalization to original image
clahe = cv2.createCLAHE(clipLimit=4.5, tileGridSize=(9, 9))
adap_equ = clahe.apply(gray_img)
start_time = time.time()
contrast_img1 = construct_texture_image(gray_img=adap_equ,
win_order=5,
feature='contrast',
offsets=[1],
angles=[0],
fill_type='mirror',
norm=True,
symm=True,
levels=17,
isotropic=True,
weight=None,
rescale=True
)
print('Time cost of isotropic contrast: ', time.time() - start_time)
In [15]:
fig, ax = plt.subplots()
im = ax.imshow(adap_equ, cmap='gray')
ax.set_title("orig equalized image\n ({0})".format(get_filename(image_file)))
fig.colorbar(im, ax=ax)
fig, ax = plt.subplots()
im = ax.imshow(contrast_img1, cmap=cmap)
ax.set_title("contrast img by sliding w(11x11) \nisotropic glcm 1-offset")
fig.colorbar(im, ax=ax)
plt.show()
In [16]:
start_time = time.time()
homogen_img1 = construct_texture_image(gray_img=adap_equ,
win_order=5,
feature='homogeneity',
offsets=[1],
angles=[0],
fill_type='mirror',
norm=True,
symm=True,
levels=17,
isotropic=True,
weight=None,
rescale=True
)
print('Time cost of isotropic homogeneity: ', time.time() - start_time)
start_time = time.time()
cluster_img1 = construct_texture_image(gray_img=adap_equ,
win_order=5,
feature='cluster_shade',
offsets=[1],
angles=[0],
fill_type='mirror',
norm=True,
symm=True,
levels=17,
isotropic=True,
weight=None,
rescale=True
)
print('Time cost of isotropic cluster_shade: ', time.time() - start_time)
start_time = time.time()
dissi_img1 = construct_texture_image(gray_img=adap_equ,
win_order=5,
feature='dissimilarity',
offsets=[1],
angles=[0],
fill_type='mirror',
norm=True,
symm=True,
levels=17,
isotropic=True,
weight=None,
rescale=True
)
print('Time cost of isotropic dissimilarity: ', time.time() - start_time)
start_time = time.time()
correl_img1 = construct_texture_image(gray_img=adap_equ,
win_order=5,
feature='correlation',
offsets=[1],
angles=[0],
fill_type='mirror',
norm=True,
symm=True,
levels=17,
isotropic=True,
weight=None,
rescale=True
)
print('Time cost of isotropic correlation: ', time.time() - start_time)
In [17]:
fig, ax = plt.subplots()
im = ax.imshow(homogen_img1, cmap=cmap)
ax.set_title("homogeneity img by sliding w(11x11) \nisotropic glcm 1-offset")
fig.colorbar(im, ax=ax)
fig, ax = plt.subplots()
im = ax.imshow(cluster_img1, cmap=cmap)
ax.set_title("cluster shade img by sliding w(11x11) \nisotropic glcm 1-offset")
fig.colorbar(im, ax=ax)
fig, ax = plt.subplots()
im = ax.imshow(dissi_img1, cmap=cmap)
ax.set_title("dissimilarity img by sliding w(11x11) \nisotropic glcm 1-offset")
fig.colorbar(im, ax=ax)
fig, ax = plt.subplots()
im = ax.imshow(correl_img1, cmap=cmap)
ax.set_title("correlation img by sliding w(11x11) \nisotropic glcm 1-offset")
fig.colorbar(im, ax=ax)
plt.show()
In [ ]: