【图像处理】图像特征提取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

  • 1
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
1. 提取图像特征 图像(Image Moment)是图像处理中一种基本的形状描述方法,它可以用来描述图像的几何特征和灰度特征。图像特征包括中心、标准化中心、Hu等。 下面是使用OpenCV库提取图像特征的代码示例: ```python import cv2 # 读取图像 img = cv2.imread('test.jpg') # 转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 计算图像 m = cv2.moments(gray) # 计算中心 cx = int(m['m10'] / m['m00']) cy = int(m['m01'] / m['m00']) # 计算标准化中心 nu20 = m['mu20'] / m['m00'] nu02 = m['mu02'] / m['m00'] nu11 = m['mu11'] / m['m00'] # 计算Hu hu = cv2.HuMoments(m) ``` 2. 提取HOG特征 HOG(Histogram of Oriented Gradients)特征是一种用于图像识别和检测的特征描述子,它可以通过计算图像中每个像素点的梯度方向和大小,进而得到图像的特征向量。 下面是使用OpenCV库提取HOG特征的代码示例: ```python import cv2 # 读取图像 img = cv2.imread('test.jpg') # 转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 计算HOG特征 winSize = (64, 64) blockSize = (16, 16) blockStride = (8, 8) cellSize = (8, 8) nbins = 9 hog = cv2.HOGDescriptor(winSize, blockSize, blockStride, cellSize, nbins) feat = hog.compute(gray) ``` 3. 提取LBP特征 LBP(Local Binary Pattern)特征是一种用于图像分类和识别的局部纹理特征描述子,它可以通过对图像中每个像素点的局部区域进行二值化处理,得到一个二进制序列,并将该序列转换为十进制数作为该像素点的LBP特征值。 下面是使用OpenCV库提取LBP特征的代码示例: ```python import cv2 # 读取图像 img = cv2.imread('test.jpg') # 转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 计算LBP特征 radius = 1 n_points = 8 * radius lbp = cv2.ORB_create(n_features=1000, scaleFactor=1.2, nlevels=8, edgeThreshold=15, firstLevel=0, WTA_K=2, patchSize=31, fastThreshold=20) feat = lbp.detectAndCompute(gray, None) ``` 以上是三种常用的图像特征提取方法的Python实现,可以根据需要选择并应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Evan Yi

老板行行好,打赏一下吧~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值