Last active
May 8, 2017 14:13
-
-
Save grasses/bacbdfae0626353de12cedc4ceaed552 to your computer and use it in GitHub Desktop.
image lbp transform: http://homeway.me/2017/05/04/pattern-writer-identify/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
__author__ = 'homeway' | |
__email__ = '[email protected]' | |
__copyright__ = 'Copyright © 2017/05/08, homeway' | |
import numpy as np | |
import cv2 | |
from matplotlib import pyplot as plt | |
def thresholded(center, pixels): | |
out = [] | |
for a in pixels: | |
if a < center: | |
out.append(0) | |
else: | |
out.append(1) | |
return out | |
def get_pixel(pixel_list, idx, idy, default = 0): | |
try: | |
return pixel_list[idx, idy] | |
except IndexError: | |
return default | |
def show(img, lbp_img): | |
plt.figure(figsize = (8, 8)) | |
plt.subplot(221) | |
plt.title("original image") | |
plt.imshow(img, cmap=plt.cm.Greys_r) | |
plt.subplot(222) | |
plt.title("LBP transform image") | |
plt.imshow(lbp_img, cmap=plt.cm.Greys_r) | |
plt.subplot(223) | |
(hist, bins) = np.histogram(img.flatten(), 256, [0, 256]) | |
cdf = hist.cumsum() | |
cdf_normalized = cdf * hist.max() / cdf.max() | |
plt.plot(cdf_normalized, color = 'b') | |
plt.hist(img.flatten(), 256, [0, 256], color = 'r') | |
plt.xlim([0, 256]) | |
plt.legend(('cdf', 'histogram'), loc = 'upper left') | |
plt.show() | |
def main(fpath): | |
img = cv2.imread(fpath, cv2.IMREAD_GRAYSCALE) | |
lbp_img = cv2.imread(fpath, cv2.IMREAD_GRAYSCALE) | |
offset = [(-1, -1), (0, -1), (1, -1), (1, 0), (-1, 0), (-1, 1), (1, 1), (0, 1)] | |
for x in range(len(img)): | |
for y in range(len(img[x])): | |
matrix = [] | |
for z in range(len(offset)): | |
matrix.append(get_pixel(img, x + offset[z][0], y + offset[z][1])) | |
center = img[x, y] | |
# get thresholded 0101 value | |
values = thresholded(center, matrix) | |
weights = [1, 2, 4, 8, 16, 32, 64, 128] | |
# add thresholded weight | |
res = 0 | |
for a in range(len(values)): | |
res += weights[a] * values[a] | |
lbp_img.itemset((x,y), res) | |
show(img, lbp_img) | |
if __name__ == '__main__': | |
main(r'/path/to/img') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment