def getmultiLBP(mat,step=50,block=100): N=mat.shape[0] idx=np.triu_indices(N) lbp = [] for i,j in itertools.product(idx[0],idx[1]): lbp.append(feature.multiblock_lbp(mat,i,j,block,block)) colNames = [ 'lbp_rad_%d' %(r) for r in range(len(lbp))] lbpSeries = pd.Series(data = preprocessing.scale(lbp), index=colNames) return lbpSeries
def test_single_mblbp(self): # Create dummy matrix where first and fifth rectangles have greater # value than the central one. Therefore, the following bits # should be 1. test_img = np.zeros((9, 9), dtype='uint8') test_img[3:6, 3:6] = 1 test_img[:3, :3] = 255 test_img[6:, 6:] = 255 # MB-LBP is filled in reverse order. So the first and fifth bits from # the end should be filled. correct_answer = 0b10001000 int_img = integral_image(test_img) lbp_code = multiblock_lbp(int_img, 0, 0, 3, 3) np.testing.assert_equal(lbp_code, correct_answer)
def histogram_feature(real_img, img, p_blocks, n_features, h_size, slider): xsize, ysize = img.shape histogram = np.zeros(h_size, dtype=float) y = 0 x = 0 while ((y + 3 * slider) <= ysize): while ((x + 3 * slider) <= xsize): lbp_code = multiblock_lbp(img, y, x, p_blocks, p_blocks) histogram[lbp_code] += 1 x += slider #region_print(real_img, lbp_code, y+20, x+15, p_blocks, p_blocks) y += slider x = 0 histogram = np.sort(histogram, axis=None) features = np.zeros(n_features, dtype=float) histogram[:h_size - n_features] features[0:n_features - 1] = histogram[h_size - (n_features - 1):] features[n_features - 1] = np.sum(histogram[:h_size - (n_features - 1)]) max = np.amax(features) return features / max
def calculate_lbp(img_path): # load img in grayscale img = load(img_path, as_gray=True) return multiblock_lbp(img, 0, 0, WIDTH, HEIGHT)
from __future__ import print_function from skimage.feature import multiblock_lbp, draw_multiblock_lbp import numpy as np from numpy.testing import assert_equal from skimage.transform import integral_image from matplotlib import pyplot as plt # Create test matrix where first and fifth rectangles starting # from top left clockwise have greater value than the central one. test_img = np.zeros((9, 9), dtype='uint8') test_img[3:6, 3:6] = 1 test_img[:3, :3] = 50 test_img[6:, 6:] = 50 print(test_img) # First and fifth bits should be filled. This correct value will # be compared to the computed one. correct_answer = 0b10001000 int_img = integral_image(test_img) print(int_img) lbp_code = multiblock_lbp(int_img, 0, 0, 3, 3) print(lbp_code) img = draw_multiblock_lbp(test_img, 0, 0, 90, 90, lbp_code=lbp_code, alpha=0.5) plt.imshow(img, interpolation='nearest') plt.show()
def getFeatures_old(I, Isegmented): Ilab = rgb2lab(I) Ilab[:, :, 0] *= 2.55 Ilab[:, :, 1] += 127 Ilab[:, :, 2] += 128 I05 = rescale(I, 0.5) I025 = rescale(I, 0.25) I0125 = rescale(I, 0.125) Ilab05 = rescale(Ilab, 0.5) Ilab025 = rescale(Ilab, 0.25) Ilab0125 = rescale(Ilab, 0.125) Isegmented05 = rescale(Isegmented, 0.5) Isegmented025 = rescale(Isegmented, 0.25) Isegmented0125 = rescale(Isegmented, 0.125) Xstack = [] Xnstack = [] options = {'b': [ {'name': 'basicgeo', 'options': {'show': False}}, # basic geometric features {'name': 'hugeo', 'options': {'show': False}}, # Hu moments {'name': 'flusser', 'options': {'show': False}}, # Flusser moments {'name': 'fourierdes', 'options': {'show': False, 'Nfourierdes': 12}}, # Fourier descriptors ]} Xtmp, Xntmp = Bfx_geo(Isegmented, options) Xstack.extend(Xtmp[0]) Xnstack.extend(Xntmp) options = {'dharalick': [3, 6, 12]} # # pixels distance for coocurrence # options = {'dharalick': 3} # 3 pixels distance for coocurrence J = rgb2gray(I.astype(float)) Xtmp, Xntmp = Bfx_haralick(J, Isegmented, options) # Haralick features Xntmp = [name + '_gray' for name in Xntmp] Xstack.extend(Xtmp[0]) Xnstack.extend(Xntmp) Xtmp, Xntmp = Bfx_haralick(Ilab[:, :, 0], Isegmented, options) # Haralick features Xntmp = [name + '_L*' for name in Xntmp] Xstack.extend(Xtmp[0]) Xnstack.extend(Xntmp) Xtmp, Xntmp = Bfx_haralick(Ilab[:, :, 1], Isegmented, options) # Haralick features Xntmp = [name + '_A*' for name in Xntmp] Xstack.extend(Xtmp[0]) Xnstack.extend(Xntmp) Xtmp, Xntmp = Bfx_haralick(Ilab[:, :, 2], Isegmented, options) # Haralick features Xntmp = [name + '_B*' for name in Xntmp] Xstack.extend(Xtmp[0]) Xnstack.extend(Xntmp) a, _ = histogram(np.round(rgb2gray(I.astype(float)))) a = a / a.sum() Xtmp = [entr(a).sum(axis=0)] Xntmp = ['Entropy'] Xstack.extend(Xtmp) Xnstack.extend(Xntmp) count = float(I[:, :, 0].size) mean_red = np.sum(I[:, :, 0]) mean_green = np.sum(I[:, :, 1]) mean_blue = np.sum(I[:, :, 2]) Xtmp = [mean_red / count, mean_green / count, mean_blue / count] Xntmp = ['mean_red', 'mean_green', 'mean_blue'] Xstack.extend(Xtmp) Xnstack.extend(Xntmp) Xstack.extend(Xtmp) Xnstack.extend(Xntmp) mean_l = np.sum(Ilab[:, :, 0]) mean_a = np.sum(Ilab[:, :, 1]) mean_b = np.sum(Ilab[:, :, 2]) Xtmp = [mean_l / count, mean_a / count, mean_b / count] Xntmp = ['mean_l', 'mean_a', 'mean_b'] Xstack.extend(Xtmp) Xnstack.extend(Xntmp) options = { 'weight': 0, 'vdiv': 3, 'hdiv': 3, 'samples': 8, 'mappingtype': 'nri_uniform' } Xtmp, Xntmp = Bfx_lbp(I[:, :, 0], Isegmented, options) Xntmp = [name + '_red_normal' for name in Xntmp] Xstack.extend(Xtmp[0]) Xnstack.extend(Xntmp) Xtmp, Xntmp = Bfx_lbp(I[:, :, 1], Isegmented, options) Xntmp = [name + '_green_normal' for name in Xntmp] Xstack.extend(Xtmp[0]) Xnstack.extend(Xntmp) Xtmp, Xntmp = Bfx_lbp(I[:, :, 2], Isegmented, options) Xntmp = [name + '_blue_normal' for name in Xntmp] Xstack.extend(Xtmp[0]) Xnstack.extend(Xntmp) Xtmp, Xntmp = Bfx_lbp(rgb2gray(I), Isegmented, options) Xntmp = [name + '_gray_normal' for name in Xntmp] Xstack.extend(Xtmp[0]) Xnstack.extend(Xntmp) Xtmp, Xntmp = Bfx_lbp(Ilab[:, :, 0], Isegmented, options) Xntmp = [name + '_L*_normal' for name in Xntmp] Xstack.extend(Xtmp[0]) Xnstack.extend(Xntmp) Xtmp, Xntmp = Bfx_lbp(Ilab[:, :, 1], Isegmented, options) Xntmp = [name + '_A*_normal' for name in Xntmp] Xstack.extend(Xtmp[0]) Xnstack.extend(Xntmp) Xtmp, Xntmp = Bfx_lbp(Ilab[:, :, 2], Isegmented, options) Xntmp = [name + '_B*_normal' for name in Xntmp] Xstack.extend(Xtmp[0]) Xnstack.extend(Xntmp) # Multiblock_LBP - RGB - normal ILabel = label(Isegmented) for region in regionprops(ILabel): minr, minc, maxr, maxc = region.bbox Xtmp = multiblock_lbp(I[:, :, 0], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_red_normal' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) Xtmp = multiblock_lbp(I[:, :, 1], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_green_normal' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) Xtmp = multiblock_lbp(I[:, :, 2], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_blue_normal' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) # Multiblock_LBP - Gray - normal Xtmp = multiblock_lbp(rgb2gray(I), minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_gray_normal' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) # Multiblock_LBP - LBP - normal Xtmp = multiblock_lbp(Ilab[:, :, 0], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_L*_normal' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) Xtmp = multiblock_lbp(Ilab[:, :, 1], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_A*_normal' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) Xtmp = multiblock_lbp(Ilab[:, :, 2], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_B*_normal' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) # Multiblock_LBP - RGB - 0.5 ILabel = label(Isegmented05) for region in regionprops(ILabel): minr, minc, maxr, maxc = region.bbox Xtmp = multiblock_lbp(I05[:, :, 0], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_red_0.5' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) Xtmp = multiblock_lbp(I05[:, :, 1], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_green_0.5' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) Xtmp = multiblock_lbp(I05[:, :, 2], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_blue_0.5' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) # Multiblock_LBP - Gray - normal Xtmp = multiblock_lbp(rgb2gray(I05), minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_gray_0.5' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) # Multiblock_LBP - LBP - normal Xtmp = multiblock_lbp(Ilab05[:, :, 0], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_L*_0.5' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) Xtmp = multiblock_lbp(Ilab05[:, :, 1], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_A*_0.5' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) Xtmp = multiblock_lbp(Ilab05[:, :, 2], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_B*_0.5' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) # Multiblock_LBP - RGB - 0.25 ILabel = label(Isegmented025) for region in regionprops(ILabel): minr, minc, maxr, maxc = region.bbox Xtmp = multiblock_lbp(I025[:, :, 0], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_red_0.25' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) Xtmp = multiblock_lbp(I025[:, :, 1], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_green_0.25' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) Xtmp = multiblock_lbp(I025[:, :, 2], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_blue_0.25' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) # Multiblock_LBP - Gray - normal Xtmp = multiblock_lbp(rgb2gray(I025), minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_gray_0.25' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) # Multiblock_LBP - LBP - normal Xtmp = multiblock_lbp(Ilab025[:, :, 0], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_L*_0.25' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) Xtmp = multiblock_lbp(Ilab025[:, :, 1], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_A*_0.25' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) Xtmp = multiblock_lbp(Ilab025[:, :, 2], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_B*_0.25' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) # Multiblock_LBP - RGB - 0.125 ILabel = label(Isegmented0125) for region in regionprops(ILabel): minr, minc, maxr, maxc = region.bbox Xtmp = multiblock_lbp(I0125[:, :, 0], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_red_0.125' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) Xtmp = multiblock_lbp(I0125[:, :, 1], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_green_0.125' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) Xtmp = multiblock_lbp(I0125[:, :, 2], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_blue_0.125' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) # Multiblock_LBP - Gray - normal Xtmp = multiblock_lbp(rgb2gray(I0125), minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_gray_0.125' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) # Multiblock_LBP - LBP - normal Xtmp = multiblock_lbp(Ilab0125[:, :, 0], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_L*_0.125' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) Xtmp = multiblock_lbp(Ilab0125[:, :, 1], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_A*_0.125' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) Xtmp = multiblock_lbp(Ilab0125[:, :, 2], minr, minc, int((maxc - minc) / 3), int((maxr - minr) / 3)) Xntmp = 'multiblock_lbp_B*_0.125' Xstack.extend([Xtmp]) Xnstack.extend([Xntmp]) return Xstack, Xnstack
from skimage.transform import integral_image # Create test matrix where first and fifth rectangles starting # from top left clockwise have greater value than the central one. test_img = np.zeros((9, 9), dtype='uint8') test_img[3:6, 3:6] = 1 test_img[:3, :3] = 50 test_img[6:, 6:] = 50 # First and fifth bits should be filled. This correct value will # be compared to the computed one. correct_answer = 0b10001000 int_img = integral_image(test_img) lbp_code = multiblock_lbp(int_img, 0, 0, 3, 3) assert_equal(correct_answer, lbp_code) ###################################################################### # Now let's apply the operator to a real image and see how the visualization # works. from skimage import data from matplotlib import pyplot as plt from skimage.feature import draw_multiblock_lbp test_img = data.coins() int_img = integral_image(test_img)
def compareScikit(): test_img = np.round(np.random.rand(36, 36) * 256) int_img = integral_image(test_img) matriks = genAverageMat(test_img, 2, 3) mat_lbp = lbpCompare(matriks) lbp_2 = multiblock_lbp(int_img, 0, 9, 3, 2)
from skimage import data from matplotlib import pyplot as plt from skimage.transform import integral_image from skimage.feature import multiblock_lbp from skimage.feature import draw_multiblock_lbp test_img = data.coins() plt.imshow(test_img, interpolation='nearest') plt.show() int_img = integral_image(test_img) lbp_code = multiblock_lbp(int_img, 0, 0, 190, 90) img = draw_multiblock_lbp(test_img, 0, 0, 190, 90, lbp_code=lbp_code, alpha=0.5) plt.imshow(img, interpolation='nearest') plt.show()