图片特征HOG/LBP/ORB存为npy文件


前言

例如需要分类器KNN、SVM等对图片特征进行分类,在这之前把图片特征转为npy文件。


一、HOG特征存npy文件

可以实现文件夹内的所有图片获取hog特征后存npy文件。

from skimage import feature as ft
import cv2
import matplotlib.pyplot as plt
import numpy as np

savepath = r"/PIC_F/A"#存到哪个位置
rootpath = r"PIC_F/hello/" #从哪个文件夹找到图片
imgfiles = os.listdir(rootpath)#形成文件途径

pic_weidu=400
npdata=np.empty([0,pic_weidu])#先固定400维度 和图片大小相关

def  Tiqu(imgpath):
    frame=cv2.imread(imgpath)    
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    normalised_blocks,features = ft.hog(gray,orientations=6,pixels_per_cell=[8,8],cells_per_block=[2,2],visualize=True)
    features=np.array(features,dtype=np.float32)
    # print(features.shape)
    return features

for i in range(0, len(imgfiles)):
    path = os.path.join(rootpath, imgfiles[i])
    if os.path.isfile(path):
        if (imgfiles[i].endswith("jpg") or imgfiles[i].endswith("JPG")):
            picpath =os.path.join(rootpath, imgfiles[i])#找到每一张图片
            dst=Tiqu(picpath)
            npdata= np.append(npdata, dst, axis=0)
print("数据总的维度是%d %d" % npdata.shape)
np.save(r".\PIC_F\a\A.npy",npdata)

二、LBP特征存npy文件

from skimage.transform import rotate
from skimage.feature import local_binary_pattern
from skimage import data, io
from skimage.color import label2rgb
import skimage
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import cv2

# settings for LBP
radius = 3
n_points = 8 * radius


savepath = r"/PIC_F/A"#存到哪个位置
rootpath = r"PIC_F/hello/" #从哪个文件夹找到图片
imgfiles = os.listdir(rootpath)#形成文件途径

pic_weidu=400
npdata=np.empty([0,pic_weidu])#先固定400维度

def  Tiqu(imgpath):
    frame=cv2.imread(imgpath)    
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    lbp = local_binary_pattern(image, n_points, radius)
    # print(lbp.shape)
    features=np.array(lbp,dtype=np.float32)
    # print(features.shape)
    return features

for i in range(0, len(imgfiles)):
    path = os.path.join(rootpath, imgfiles[i])
    if os.path.isfile(path):
        if (imgfiles[i].endswith("jpg") or imgfiles[i].endswith("JPG")):
            picpath =os.path.join(rootpath, imgfiles[i])#找到每一张图片
            dst=Tiqu(picpath)
            npdata= np.append(npdata, dst, axis=0)
print("数据总的维度是%d %d" % npdata.shape)
np.save(r".\PIC_F\a\B.npy",npdata)

三、ORB特征存npy文件

SIFT的特征也可以参考这个处理

import os
import cv2
import numpy as np

savepath = r"/PIC_F/A"#存到哪个位置
rootpath = r"PIC_F/hello/" #从哪个文件夹找到图片
imgfiles = os.listdir(rootpath)#形成文件途径

pic_weidu=32
npdata=np.empty([0,pic_weidu])#先固定32维度

Ex_weidu=315  #特征点统一一个维度 这个获取的每个图片特征点数量不一致,我们在分类器中不方便使用,所以统一一个维度
#不满足这个数量的特征就填充为0

def  Tiqu(imgpath):
    frame=cv2.imread(imgpath)    
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    orb=cv2.ORB_create()
    kp,des=orb.detectAndCompute(gray,None)
    features=np.array(des,dtype=np.float32)
    print("图片特征获取之前的维度%d" % len(kp))    
    fillnpdata=np.zeros([Ex_weidu-len(kp),pic_weidu])
    features=np.append(features, fillnpdata, axis=0)
    return features

for i in range(0, len(imgfiles)):
    path = os.path.join(rootpath, imgfiles[i])
    if os.path.isfile(path):
        if (imgfiles[i].endswith("jpg") or imgfiles[i].endswith("JPG")):
            picpath =os.path.join(rootpath, imgfiles[i])#找到每一张图片
            dst=Tiqu(picpath)
            npdata= np.append(npdata, dst, axis=0)
print("数据总的维度是%d %d" % npdata.shape)
np.save(r".\PIC_F\a\C.npy",npdata)

总结

备忘。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

粒子白

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值