ESF projekt Západočeské univerzity v Plzni reg. č. CZ.02.2.69/0.0/0.0/16 015/0002287
In [1]:
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
import skimage
In [2]:
# Image loading
img =  io.imread('./Lenna.png')
In [3]:
# LBP calculation - original image
from skimage.feature import local_binary_pattern

radius = 1
n_points = 8 * radius
METHOD = 'uniform'

lbp = local_binary_pattern(skimage.color.rgb2gray(img), n_points, radius, METHOD)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True)

ax1.axis('off')
ax1.imshow(img, cmap=plt.cm.gray)
ax1.set_title('Input image')


ax2.axis('off')
ax2.imshow(lbp, cmap=plt.cm.gray)
ax2.set_title('Local Binary Pattern')
plt.show()
In [4]:
# Original image - histogram
n_bins = int(lbp.max() + 1)
a,b,c = plt.hist(lbp.ravel(), density=True, bins=n_bins, range=(0, n_bins))
plt.show()
In [5]:
# Brightness change
from skimage import exposure

img_bright = exposure.adjust_gamma(img, gamma=0.5,gain=1)

lbp_bright = local_binary_pattern(skimage.color.rgb2gray(img_bright), n_points, radius, METHOD)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True)

ax1.axis('off')
ax1.imshow(img_bright)
ax1.set_title('Input image')


ax2.axis('off')
ax2.imshow(lbp_bright, cmap=plt.cm.gray)
ax2.set_title('Local Binary Pattern')
plt.show()
In [6]:
# Histogram for image with changed brightness
n_bins = int(lbp_bright.max() + 1)
a,b,c = plt.hist(lbp_bright.ravel(), density=True, bins=n_bins, range=(0, n_bins))
plt.show()
In [7]:
# Image rotation
img_rot = skimage.transform.rotate(img, angle = 90)
lbp_rot = local_binary_pattern(skimage.color.rgb2gray(img_rot), n_points, radius, METHOD)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True)

ax1.axis('off')
ax1.imshow(img_rot, cmap=plt.cm.gray)
ax1.set_title('Input image')


ax2.axis('off')
ax2.imshow(lbp_rot, cmap=plt.cm.gray)
ax2.set_title('Local Binary Pattern')
plt.show()
In [8]:
# Histogram for rotated image
n_bins = int(lbp_rot.max() + 1)
a,b,c = plt.hist(lbp_rot.ravel(), density=True, bins=n_bins, range=(0, n_bins))
plt.show()

Úkol - naprogramujte LBP reskriptor

In [12]:
import scipy
grayscale = skimage.color.rgb2gray(img)

hog_filter = np.expand_dims(np.array([-1,0,1]), axis = 1)

img_x = scipy.signal.convolve(grayscale, hog_filter)
img_y = scipy.signal.convolve(grayscale, np.transpose(hog_filter))

final_grad = np.sqrt(img_x[1:512,1:513]**2+img_y[1:513,1:512]**2)
In [13]:
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(16, 8), sharex=True, sharey=True)
ax1.axis('off')
ax1.imshow(img_x, cmap=plt.cm.gray)
ax1.set_title('Gradients X')

ax2.axis('off')
ax2.imshow(img_y, cmap=plt.cm.gray)
ax2.set_title('Gradients Y')

ax3.axis('off')
ax3.imshow(final_grad, cmap=plt.cm.gray)
ax3.set_title('Gradients')
plt.show()
In [9]:
# HoG calculation
from skimage.feature import hog

fd, hog_image = hog(img, orientations=8, pixels_per_cell=(16, 16),
                    cells_per_block=(1, 1), visualize=True, multichannel=True)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True)

ax1.axis('off')
ax1.imshow(img, cmap=plt.cm.gray)
ax1.set_title('Input image')

ax2.axis('off')
ax2.imshow(hog_image, cmap=plt.cm.gray)
ax2.set_title('Histogram of Oriented Gradients')
plt.show()