Skip to content

Commit 0147bfb

Browse files
committed
Added two bin and updated lbp
1 parent f2f78fe commit 0147bfb

File tree

5 files changed

+114
-23
lines changed

5 files changed

+114
-23
lines changed

lbp.py

+4-21
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import cv2
22
import numpy as np
3-
import matplotlib
43
from matplotlib import pyplot as plt
54

6-
def version_info():
7-
print("OpenCV Version = " + cv2.__version__)
8-
print("Numpy Version = " + np.__version__)
9-
print("Matplotlib Version = " + matplotlib.__version__)
10-
115
def get_pixel(img, center, x, y):
126
new_value = 0
137
try:
@@ -56,31 +50,22 @@ def show_output(output_list):
5650
current_ytick = current_dict["ytick"]
5751
current_title = current_dict["title"]
5852
current_type = current_dict["type"]
53+
current_plot = figure.add_subplot(1, output_list_len, i+1)
5954
if current_type == "gray":
60-
current_plot = figure.add_subplot(1, output_list_len, i+1)
6155
current_plot.imshow(current_img, cmap = plt.get_cmap('gray'))
6256
current_plot.set_title(current_title)
6357
current_plot.set_xticks(current_xtick)
6458
current_plot.set_yticks(current_ytick)
6559
current_plot.set_xlabel(current_xlabel)
6660
current_plot.set_ylabel(current_ylabel)
67-
elif current_type == "color":
68-
current_plot = figure.add_subplot(1, output_list_len, i+1)
69-
current_plot.imshow(current_img)
70-
current_plot.set_title(current_title)
71-
current_plot.set_xticks(current_xtick)
72-
current_plot.set_yticks(current_ytick)
73-
current_plot.set_xlabel(current_xlabel)
74-
current_plot.set_ylabel(current_ylabel)
7561
elif current_type == "histogram":
76-
current_plot = figure.add_subplot(1, output_list_len, i+1)
77-
current_plot.hist(current_img.flatten(),256,[0,256], color = 'black')
62+
current_plot.plot(current_img, color = "black")
7863
current_plot.set_xlim([0,260])
7964
current_plot.set_title(current_title)
8065
current_plot.set_xlabel(current_xlabel)
8166
current_plot.set_ylabel(current_ylabel)
8267
ytick_list = [int(i) for i in current_plot.get_yticks()]
83-
current_plot.set_yticklabels(ytick_list,rotation = 90)
68+
current_plot.set_yticklabels(ytick_list,rotation = 90)
8469

8570
plt.show()
8671

@@ -94,7 +79,6 @@ def main():
9479
for i in range(0, height):
9580
for j in range(0, width):
9681
img_lbp[i, j] = lbp_calculated_pixel(img_gray, i, j)
97-
hist_gray = cv2.calcHist([img_gray], [0], None, [256], [0, 256])
9882
hist_lbp = cv2.calcHist([img_lbp], [0], None, [256], [0, 256])
9983
output_list = []
10084
output_list.append({
@@ -116,7 +100,7 @@ def main():
116100
"type": "gray"
117101
})
118102
output_list.append({
119-
"img": img_lbp,
103+
"img": hist_lbp,
120104
"xlabel": "Bins",
121105
"ylabel": "Number of pixels",
122106
"xtick": None,
@@ -132,5 +116,4 @@ def main():
132116
print("LBP Program is finished")
133117

134118
if __name__ == '__main__':
135-
version_info()
136119
main()

readme.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
`Local Binary Pattern (LBP)` is a simple yet very efficient texture operator which labels the pixels of an image by thresholding the neighborhood of each pixel and considers the result as a binary number.
44
[Click here to learn more.](http://www.scholarpedia.org/article/Local_Binary_Patterns "Local Binary Pattern (LBP)")
55

6+
Moreover, I also added a pattern code for two bins only to compare the result of LBP and two bin pattern.
7+
68
## Packages Used
79
The program is coded using Windows 10 (64 bit) operating system with Python version 3.5.2.
810

@@ -15,12 +17,17 @@ Package | Version | Installation
1517
**Matplotlib** | 1.5.3 | `pip install matplotlib`
1618

1719
## Image Used
18-
* [Lena Söderberg](https://raw.githubusercontent.com/arsho/local_binary_patterns/master/lenna.jpg "Lena Söderberg")
20+
![alt Lena Söderberg](https://raw.githubusercontent.com/arsho/local_binary_patterns/master/lenna.jpg)
21+
* [Click to download original image of Lena Söderberg](https://raw.githubusercontent.com/arsho/local_binary_patterns/master/lenna.jpg "Lena Söderberg")
1922

20-
## Output of `lbp.py`
23+
### Output of `lbp.py`
2124
The output consists of the gray scale image, LBP representation image and LBP histogram.
2225
![alt output of lbp](https://raw.githubusercontent.com/arsho/local_binary_patterns/master/screenshot/lbp_output.png)
2326

27+
### Output of `two_bin.py`
28+
The output consists of the gray scale image, Two Bin representation image and Two Bin histogram.
29+
![alt output of lbp](https://raw.githubusercontent.com/arsho/local_binary_patterns/master/screenshot/two_bin_output.png)
30+
2431
### References
2532
* [Local Binary Patterns with Python & OpenCV](http://www.pyimagesearch.com/2015/12/07/local-binary-patterns-with-python-opencv/ "Local Binary Patterns with Python & OpenCV")
2633
* [Local Binary Patterns Wiki](https://en.wikipedia.org/wiki/Local_binary_patterns "Local Binary Patterns Wiki")

screenshot/lbp_output.png

-2.29 MB
Loading

screenshot/two_bin_output.png

391 KB
Loading

two_bin.py

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import cv2
2+
import numpy as np
3+
from matplotlib import pyplot as plt
4+
5+
def get_pixel(img, center, x, y):
6+
new_value = 0
7+
try:
8+
if img[x][y] >= center:
9+
new_value = 1
10+
except:
11+
pass
12+
return new_value
13+
14+
def calculated_pixel(img, x, y):
15+
center = img[x][y]
16+
left = get_pixel(img, center, x, y-1)
17+
right = get_pixel(img, center, x, y+1)
18+
if left>right:
19+
return 100
20+
else:
21+
return 200
22+
23+
def show_output(output_list):
24+
output_list_len = len(output_list)
25+
figure = plt.figure()
26+
for i in range(output_list_len):
27+
current_dict = output_list[i]
28+
current_img = current_dict["img"]
29+
current_xlabel = current_dict["xlabel"]
30+
current_ylabel = current_dict["ylabel"]
31+
current_xtick = current_dict["xtick"]
32+
current_ytick = current_dict["ytick"]
33+
current_title = current_dict["title"]
34+
current_type = current_dict["type"]
35+
current_plot = figure.add_subplot(1, output_list_len, i+1)
36+
if current_type == "gray":
37+
current_plot.imshow(current_img, cmap = plt.get_cmap('gray'))
38+
current_plot.set_title(current_title)
39+
current_plot.set_xticks(current_xtick)
40+
current_plot.set_yticks(current_ytick)
41+
current_plot.set_xlabel(current_xlabel)
42+
current_plot.set_ylabel(current_ylabel)
43+
elif current_type == "histogram":
44+
current_plot.plot(current_img, color = "black")
45+
current_plot.set_xlim([0,260])
46+
current_plot.set_title(current_title)
47+
current_plot.set_xlabel(current_xlabel)
48+
current_plot.set_ylabel(current_ylabel)
49+
ytick_list = [int(i) for i in current_plot.get_yticks()]
50+
current_plot.set_yticklabels(ytick_list,rotation = 90)
51+
52+
plt.show()
53+
54+
def main():
55+
image_file = 'lenna.jpg'
56+
img_bgr = cv2.imread(image_file)
57+
height, width, channel = img_bgr.shape
58+
img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
59+
60+
img_two_bin = np.zeros((height, width,3), np.uint8)
61+
for i in range(0, height):
62+
for j in range(0, width):
63+
img_two_bin[i, j] = calculated_pixel(img_gray, i, j)
64+
hist_two_bin = cv2.calcHist([img_two_bin], [0], None, [256], [0, 256])
65+
output_list = []
66+
output_list.append({
67+
"img": img_gray,
68+
"xlabel": "",
69+
"ylabel": "",
70+
"xtick": [],
71+
"ytick": [],
72+
"title": "Gray Image",
73+
"type": "gray"
74+
})
75+
output_list.append({
76+
"img": img_two_bin,
77+
"xlabel": "",
78+
"ylabel": "",
79+
"xtick": [],
80+
"ytick": [],
81+
"title": "Two Bin Image",
82+
"type": "gray"
83+
})
84+
output_list.append({
85+
"img": hist_two_bin,
86+
"xlabel": "Bins",
87+
"ylabel": "Number of pixels",
88+
"xtick": None,
89+
"ytick": None,
90+
"title": "Histogram(Two Bin)",
91+
"type": "histogram"
92+
})
93+
94+
show_output(output_list)
95+
96+
cv2.waitKey(0)
97+
cv2.destroyAllWindows()
98+
print("Two bin Program is finished")
99+
100+
if __name__ == '__main__':
101+
main()

0 commit comments

Comments
 (0)