基于局部二值模式的 LBP 算法 python代码
时间: 2023-09-12 15:08:52 浏览: 150
很高兴能为您解答问题。以下是基于局部二值模式的 LBP 算法的 Python 代码:
```
import numpy as np
import cv2
def LBP(image):
rows,cols = image.shape
dst = np.zeros((rows-2,cols-2),dtype=image.dtype)
for i in range(1,rows-1):
for j in range(1,cols-1):
binary = ''
center = image[i,j]
if image[i-1,j-1]>center:
binary += '1'
else:
binary += '0'
if image[i-1,j]>center:
binary += '1'
else:
binary += '0'
if image[i-1,j+1]>center:
binary += '1'
else:
binary += '0'
if image[i,j+1]>center:
binary += '1'
else:
binary += '0'
if image[i+1,j+1]>center:
binary += '1'
else:
binary += '0'
if image[i+1,j]>center:
binary += '1'
else:
binary += '0'
if image[i+1,j-1]>center:
binary += '1'
else:
binary += '0'
if image[i,j-1]>center:
binary += '1'
else:
binary += '0'
dst[i-1,j-1] = int(binary,2)
return dst
if __name__ == '__main__':
image = cv2.imread('image.jpg',cv2.IMREAD_GRAYSCALE)
lbp = LBP(image)
cv2.imshow('image',image)
cv2.imshow('lbp',lbp)
cv2.waitKey(0)
```
请放心,我会保守您的要求,并根据您提出的问题提供最佳帮助。
阅读全文