Trump

    技术2022-07-10  102

    import cv2 import numpy as np import matplotlib.pyplot as plt %matplotlib inline def get_LBP(gray_image, ratio=3): img_lbp = np.zeros_like(gray_image) img_h, img_w = gray_image.shape center_p = ratio // 2 for ih in range(0,img_h - ratio): for iw in range(0,img_w - ratio): img = gray_image[ih:ih+ratio,iw:iw+ratio] center = img[center_p,center_p] img_binary = (img >= center)*1.0 binary_vector = img_binary.flatten() binary_vector = np.delete(binary_vector, 4) #去除中心点 where_binary_one = np.where(binary_vector)[0] if len(where_binary_one) >= 1: num = np.sum(2**where_binary_one) #二进制转十进制 else: num = 0 img_lbp[ih+1,iw+1] = num return img_lbp img = cv2.imread('trump.jpg') img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img_lbp = get_LBP(img_gray) ```python vecimg_lbp = img_lbp.flatten() fig = plt.figure(figsize=(20,8)) ax = fig.add_subplot(1,3,1) ax.imshow(img_gray, 'gray') ax.set_title('gray') ax = fig.add_subplot(1,3,2) ax.imshow(img_lbp, 'gray') ax.set_title('LBP') ax = fig.add_subplot(1,3,3) freq,lbp, _ = ax.hist(vecimg_lbp,bins=255) ax.set_title('LBP histogram') def binary_min(binary): #为了让LBP具有旋转不变性,将二进制串进行最小化处理 length = len(binary) min_b = [] for i in range(length)[::-1]: # [::-1]代表翻转 if binary[i] == 0: binary = binary[:i] # [:i]代表切片,i为停止位置 min_b = np.append(min_b, 0) else: min_b = np.append(min_b, binary) return np.array(min_b, dtype=np.uint8) demo = np.array([0, 1, 1, 1, 0, 1, 0, 0]) binary_min(demo)
    Processed: 0.030, SQL: 9