图像特征提取python实现(颜色矩+HOG+LBP)

颜色矩

#计算图像的颜色一二三阶矩
def color_moments(img):
    n_images = img.shape[0]
    all_color_feature = np.zeros((n_images, 9))    
    for i in range(n_images):
    #img = cv2.imread(filename)
        if img is None:
            return
        # Convert BGR to HSV colorspace
        hsv = cv2.cvtColor(img[i], cv2.COLOR_BGR2HSV)
        # Split the channels - h,s,v
        h,s,v = cv2.split(hsv)
        # Initialize the color feature
        color_feature = []
        # N = h.shape[0] * h.shape[1]
        # The first central moment - average 
        h_mean = np.mean(h)  # np.sum(h)/float(N)
        s_mean = np.mean(s)  # np.sum(s)/float(N)
        v_mean = np.mean(v)  # np.sum(v)/float(N)
        color_feature.extend([h_mean, s_mean, v_mean])
        # The second central moment - standard deviation
        h_std = np.std(h)  # np.sqrt(np.mean(abs(h - h.mean())**2))
        s_std = np.std(s)  # np.sqrt(np.mean(abs(s - s.mean())**2))
        v_std = np.std(v)  # np.sqrt(np.mean(abs(v - v.mean())**2))
        color_feature.extend([h_std, s_std, v_std])
        # The third central moment - the third root of the skewness
        h_skewness = np.mean(abs(h - h.mean())**3)
        s_skewness = np.mean(abs(s - s.mean())**3)
        v_skewness = np.mean(abs(v - v.mean())**3)
        h_thirdMoment = h_skewness**(1./3)
        s_thirdMoment = s_skewness**(1./3)
        v_thirdMoment = v_skewness**(1./3)
        color_feature.extend([h_thirdMoment, s_thirdMoment, v_thirdMoment])
         
        all_color_feature[i]= color_feature
        
    all_color_feature=np.delete(all_color_feature,errorline,axis=0)
    return all_color_feature

HOG 

def get_hog(img):
    n_images = img.shape[0]
    all_hog_feature = np.zeros((n_images, 144))    
    for i in range(n_images):
        normalised_blocks, hog_image = hog(img[i], orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), block_norm='L2-Hys',visualize=True)
        all_hog_feature[i]= normalised_blocks
        
    all_hog_feature=np.delete(all_hog_feature,errorline,axis=0)
    return all_hog_feature

LBP 纹理特征

#使用LBP方法提取图像的纹理特征.
def get_lbp_data(images_data, hist_size, lbp_radius, lbp_point):
    n_images = images_data.shape[0]
    hist = np.zeros((n_images, 256))  
    global errorline
    errorline=[]
    for i in range(n_images):
       #rgb图像转换为灰度图像
        rgb_data=images_data[i]        
        #这个计算结果是float64
        #b, g, r = rgb_data[:,:,0], rgb_data[:,:,1], rgb_data[:,:,2]
        #gray_data = 0.2989 * r + 0.5870 * g + 0.1140 * b
        gray_data=cv2.cvtColor(rgb_data, cv2.COLOR_BGR2GRAY)        
        lbp = local_binary_pattern(gray_data, 8, 1, 'default')
        # 统计图像的直方图
        max_bins = int(lbp.max() + 1)     
        #print(max_bins)  
        #print('hhhhhhhh')
       #print(i)
        #删除不符合条件的行
        if max_bins!=256:
            print(i)
            errorline.append(i)
            continue
        hist[i], _ = np.histogram(lbp, normed=True, bins=max_bins, range=(0, max_bins))
    print(errorline)
    hist=np.delete(hist,errorline,axis=0)
    return hist

 

 

你可能感兴趣的:(图像处理,python,python,特征提取,图像处理)