Advertisement

python 图像特征提取_python实现LBP方法提取图像纹理特征实现分类的步骤

阅读量:

题目描述

这篇博文是数字图像处理的大作业.

包含40幅不同风格的纹理图像样本,每幅图像尺寸均为512×512像素,要求将每幅图像均匀划分为9个部分,选取其中5个部分构成训练样本,剩余4个部分作为测试样本,并设计并建立相应的模型用于对纹理图像进行分类

图片如下图所示:

blank.gif

分析部分指出,因数据样本数量有限,基于深度学习的神经网络模型在处理这类图像时表现出局限性.因此需要开发有效的算法来提取和描述图像中的纹理特征.在本研究中,我们采用了LBP算法来提取和表示图像的空间纹理特征.随后通过多类别分类器对这些特征进行识别和归类.

环境

python2.7,jupyter notebook,anaconda

数据集的地址

实现

读取数据

Numpy包数组操作API格式化数据

def loadPicture():

train_index = 0;

test_index = 0;

train_data = np.zeros( (200,171,171) );

test_data = np.zeros( (160,171,171) );

train_label = np.zeros( (200) );

test_label = np.zeros( (160) );

for i in np.arange(40):

image = mpimg.imread('picture/'+str(i)+'.tiff');

data = np.zeros( (513,513) );

data[0:image.shape[0],0:image.shape[1]] = image;

#切割后的图像位于数据的位置

index = 0;

#将图片分割成九块

for row in np.arange(3):

for col in np.arange(3):

if index<5:

train_data[train_index,:,:] = data[171row:171(row+1),171col:171(col+1)];

train_label[train_index] = i;

train_index+=1;

else:

test_data[test_index,:,:] = data[171row:171(row+1),171col:171(col+1)];

test_label[test_index] = i;

test_index+=1;

index+=1;

return train_data,test_data,train_label,test_label;

特征提取

LBP特征提取方法

radius = 1;

n_point = radius * 8;

def texture_detect():

train_hist = np.zeros( (200,256) );

test_hist = np.zeros( (160,256) );

for i in np.arange(200):

#使用LBP方法提取图像的纹理特征.

lbp=skft.local_binary_pattern(train_data[i],n_point,radius,'default');

#统计图像的直方图

max_bins = int(lbp.max() + 1);

#hist size:256

train_hist[i], _ = np.histogram(lbp, normed=True, bins=max_bins, range=(0, max_bins));

for i in np.arange(160):

lbp = skft.local_binary_pattern(test_data[i],n_point,radius,'default');

#统计图像的直方图

max_bins = int(lbp.max() + 1);

#hist size:256

test_hist[i], _ = np.histogram(lbp, normed=True, bins=max_bins, range=(0, max_bins));

return train_hist,test_hist;

训练分类器

SVM支持向量机分类.

import matplotlib.image as mpimg

import matplotlib.pyplot as plt

import numpy as np

from sklearn.multiclass import OneVsRestClassifier

from sklearn.svm import SVR

from skimage import feature as skft

train_data,test_data,train_label,test_label= loadPicture();

train_hist,test_hist = texture_detect();

svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1);

One vs. rest分类器(核函数rbf,最后一类索引号).拟合训练历史和标签.计算测试集的历史和标签的得分

实验测试集结果的正确率为:90.6%

blank.gif

初次接触Python的numpy库时会感到其API的学习存在困难。然而,在实际应用中发现该库仍有优化空间。与Matlab中的矩阵操作存在显著差异,但就机器学习领域而言,scikit-learn是一个极为优秀的工具库。

总结:分类器的效果尚有提升空间;同时建议从以下几个方面入手:一是优化分类器本身;二是探索更有效的特征提取方法。

这就是本文的核心内容。这篇短文旨在提供一些有用的学习资料。我们也衷心感谢大家的支持与鼓励!

时间: 2019-07-08

全部评论 (0)

还没有任何评论哟~