Main Content

Texture Classification with Wavelet Image Scattering

This example shows how to classify textures using wavelet image scattering. In addition to Wavelet Toolbox™, this example also requires Parallel Computing Toolbox™ and Image Processing Toolbox™.

在a digital image, texture provides information about the spatial arrangement of color or pixel intensities. Particular spatial arrangements of color or pixel intensities correspond to different appearances and consistencies of the physical material being imaged. Texture classification and segmentation of images has a number of important application areas. A particularly important example is biomedical image analysis where normal and pathologic states are often characterized by morphological and histological characteristics which manifest as differences in texture [4].

Wavelet Image Scattering

For classification problems, it is often useful to map the data into some alternative representation which discards irrelevant information while retaining the discriminative properties of each class. Wavelet image scattering constructs low-variance representations of images which are insensitive to translations and small deformations. Because translations and small deformations in the image do not affect class membership, scattering transform coefficients provide features from which you can build robust classification models.

小波散射通过通过一系列小波变换,非线性和平均[1] [3] [5]来级联图像来起作用。结果的结果deepfeature extraction is that images in the same class are moved closer to each other in the scattering transform representation, while images belonging to different classes are moved farther apart.

kth-tips

This example uses a publicly available texture database, the KTH-TIPS (Textures under varying Illumination, Pose, and Scale) image database [6]. The KTH-TIPS dataset used in this example is the grayscale version. There are 810 images in total with 10 textures and 81 images per texture. The majority of images are 200-by-200 in size. This example assumes you have downloaded the KTH-TIPS grayscale dataset and untarred it so that the 10 texture classes are contained in separate subfolders of a common folder. Each subfolder is named for the class of textures it contains. Untarring the downloadedkth_tips_grey_200x200.tarfile is sufficient to provide a top-level folder KTH_TIPS and the required subfolder structure.

使用imageDatastoreto read the data. Set thelocationproperty of theimageDatastoreto the folder containing the KTH-TIPS database that you have access to.

位置= fullfile(tempdir,'kth_tips_grey_200x200',,,,'KTH_TIPS'); Imds = imageDatastore(location,'IncludeSubFolders',真的,'FileExtensions',,,,'.png',,,,'LabelSource',,,,“折叠式”);

从数据集中随机选择和可视化20个图像。

数字= 810;perm = randperm(numimages,20);fornp = 1:20 subplot(4,5,np) im = imread(Imds.Files{perm(np)}); imagesc(im); colormap灰色的;axisoff;end

Texture Classification

This example uses MATLAB™'s parallel processing capability through the高的数组接口。如果当前不运行,则启动并行池。

ifIsempty(GCP)Parpool;end
Starting parallel pool (parpool) using the 'local' profile ... Connected to the parallel pool (number of workers: 6).

For reproducibility, set the random number generator. Shuffle the files of the KTH-TIPS dataset and split the 810 images into two randomly selected sets, one for training and one held-out set for testing. Use approximately 80% of the images for building a predictive model from the scattering transform and use the remainder for testing the model.

rng(100) Imds = imageDatastore(location,'IncludeSubFolders',真的,'FileExtensions',,,,'.png',,,,'LabelSource',,,,“折叠式”); Imds = shuffle(Imds); [trainImds,testImds] = splitEachLabel(Imds,0.8);

We now have two datasets. The training set consists of 650 images, with 65 images per texture. The testing set consists of 160 images, with 16 images per texture. To verify, count the labels in each dataset.

countEachLabel(trainImds)
ans=10×2 table标签计数______________ _____铝_FOIL 65 BROWN_BREAD 65 CORDUROY 65棉65饼干65亚麻65 Orange_peel 65砂纸65 Sponge 65 Sponge 65 Styrofoam 65
CounteachLabel(testimds)
ans=10×2 table标签计数______________ _____铝_foil 16布朗_BREAD16 CORDUROY 16棉16饼干16亚麻16 Orange_peel 16砂纸16 Sponge 16 Sponge 16 Styrofoam 16

Create高的arrays for the resized images.

Ttrain = tall(trainImds); Ttest = tall(testImds);

Create a scattering framework for an image input size of 200-by-200 with an不这样刻150。不变性量表超参数是我们在此示例中设置的唯一一个。对于散射转换的其他超参数,请使用默认值。

sn= waveletScattering2('ImageSize',[200 200],“不传闻”,,,,150);

要为每个培训和测试集提取分类的功能,请使用helperScatImages_mean功能。代码helperScatImages_meanis at the end of this example.helperScatImages_meanresizes the images to a common 200-by-200 size and uses the scattering framework,sn,获得特征矩阵。在这种情况下,每个功能矩阵为391 x-7 x-7。有391个散射路径,每个散射系数图像为7 x-7。最后,helperScatImages_mean沿二维和第三维获得平均值,以获得每个图像的391元素特征向量。这是从40,000个元素降低到391的数据的显着降低。

trainfeatures = cellfun(@(x)helperScatImages_mean(sn,x),Ttrain,'Uni',,,,0); testfeatures = cellfun(@(x)helperScatImages_mean(sn,x),Ttest,'Uni',,,,0);

使用高的'sgather能力,收集所有训练和测试功能向量,然后将它们连接到矩阵中。

Trainf = gather(trainfeatures);
Evaluating tall expression using the Parallel Pool 'local': - Pass 1 of 1: Completed in 1 min 39 sec Evaluation completed in 1 min 39 sec
trainfeatures = cat(2,trainf {:});testf = chater(testfeatures);
Evaluating tall expression using the Parallel Pool 'local': - Pass 1 of 1: Completed in 23 sec Evaluation completed in 23 sec
testFeatures = cat(2,testf {:});

先前的代码会导致两个具有行尺寸391的矩阵和列尺寸分别等于训练和测试集中的图像数量。因此,每列都是特征向量。

PCA Model and Prediction

This example constructs a simple classifier based on the principal components of the scattering feature vectors for each class. The classifier is implemented in the functionshelperPCAModelandhelperPCAClassifier。功能helperPCAModel确定每个数字的主成分class based on the scattering features. The code forhelperPCAModelis at the end of this example. The functionhelperPCAClassifier通过找到每个测试功能向量的主要组件之间的最接近的匹配(最佳投影),并相应地分配了类,从而对固定测试数据进行了分类。代码helperPCAClassifieris at the end of this example.

型号= helperpcamodel(TrainFeatures,30,Trainimds.Labels);predlabels = helperpcaccacifier(TestFeatures,Model);

After constructing the model and classifying the test set, determine the accuracy of the test set classification.

accuracy = sum(testImds.Labels == predlabels)./numel(testImds.Labels)*100
精度= 99.3750

We have achieved 99.375% correct classification, or a 0.625% error rate for the 160 images in the test set. A plot of the confusion matrix shows that our simple model misclassified one texture.

Figusy ConfusionChart(testimds.Labels,Preewlabels)

Summary

在此示例中,我们使用小波图像散射来创建用于分类的纹理的低变差表示。使用散射变换和简单的主组件分类器,我们在固定测试集​​上实现了99.375%的正确分类。该结果与KTH-TIPS数据库中的最新性能相当。[2]

References

[1] Bruna, J., and S. Mallat. "Invariant Scattering Convolution Networks."IEEE Transactions on Pattern Analysis and Machine Intelligence。Vol. 35, Number 8, 2013, pp. 1872–1886.

[2] Hayman,E.,B。Caputo,M。Fritz和J. O. Eklundh。“关于现实世界条件对材料分类的重要性。”在计算机视觉-ECCV 2004,,,,edited by Tomás Pajdla and Jiří Matas, 3024:253–66. Berlin, Heidelberg: Springer Berlin Heidelberg, 2004. https://doi.org/10.1007/978-3-540-24673-2_21.

[3] Mallat, S. "Group Invariant Scattering."Communications in Pure and Applied Mathematics。Vol. 65, Number 10, 2012, pp. 1331–1398.

[4] Pujol,O。和P. Radeva。“血管内组织表征的监督质地分类。”在Handbook of Biomedical Image Analysis,,,,edited by Jasjit S. Suri, David L. Wilson, and Swamy Laxminarayan, 57–109. Boston, MA: Springer US, 2005. https://doi.org/10.1007/0-306-48606-7_2.

[5] Sifre,L。和S. Mallat。“纹理歧视的旋转,缩放和变形不变散射。”2013 IEEE计算机视觉和模式识别会议。2013, pp 1233–1240. 10.1109/CVPR.2013.163.

[6]kth-tips图像数据库主页https://www.csc.kth.se/cvap/databases/kth-tips/

Appendix — Supporting Functions

helperScatImages_mean

functionfeatures = helperScatImages_mean(sf,x) x = imresize(x,[200 200]); smat = featureMatrix(sf,x); features = mean(mean(smat,2),3);end

helperPCAModel

functionmodel = helperPCAModel(features,M,Labels)%此功能仅支持小波图像散射示例金宝app%小波工具箱。它可能会在以后的版本中更改或删除。%model = helperPCAModel(features,M,Labels)%Copyright 2018 MathWorks%在itialize structure array to hold the affine model模型= struct('Dim',,,,[],'mu',,,,[],'U',,,,[],'Labels',,,,categorical([]),',,,,[]); model.Dim = M;%Obtain the number of classesLabelCategories = categories(Labels); Nclasses = numel(categories(Labels));forkk = 1:nclasses class = labelCategories {kk};%Find indices corresponding to each classidxClass =标签== class;%Extract feature vectors for each classtmpFeatures = features(:,idxClass);%Determine the mean for each classmodel.mu{kk} = mean(tmpFeatures,2); [model.U{kk},model.S{kk}] = scatPCA(tmpFeatures);ifsize(model.U{kk},2) > M model.U{kk} = model.U{kk}(:,1:M); model.S{kk} = model.S{kk}(1:M);endmodel.Labels(kk) = Class;endfunction[u,s,v] = scatPCA(x,M)%Calculate the principal components of x along the second dimension.ifnargin > 1 && M > 0%If M is non-zero, calculate the first M principal components.[u,s,v] = svds(x-sig_mean(x),M); s = abs(diag(s)/sqrt(size(x,2)-1)).^2;else%否则,计算所有主要组件。%Each row is an observation, i.e. the number of scattering paths%每列是一类观察(u, d) = eig (x (x '));位于[s] =排序(诊断接头(d),'descend'); u = u(:,ind);endendend

helperPCAClassifier

function标签= helperpCacacifier(功能,模型)%此功能仅支持小波图像散射示例金宝app%小波工具箱。它可能会在以后的版本中更改或删除。%模型是带有字段,M,MU,V和标签的结构阵列%features is the matrix of test data which is Ns-by-L, Ns is the number of%散射路径和L是测试示例的数量。每一列%features is a test example.%Copyright 2018 MathWorkslabelIdx = determineClass(features,model); labels = model.Labels(labelIdx);%返回作为列矢量的百分比以同意Imagedatastore标签标签=标签(:);%--------------------------------------------------------------------------functionlabelIdx = determineClass(features,model)%Determine number of classesNclasses = numel(model.Labels);%初始化错误矩阵errMatrix = Inf(Nclasses,size(features,2));fornc = 1:Nclasses%class centroidmu = model.mu{nc}; u = model.U{nc};%1 x-lerrmatrix(nc,:) =投射eRventionError(功能,mu,u);end%确定沿班级维度的最小值[〜,labelidx] = min(errmatrix,[],1);%--------------------------------------------------------------------------functiontotalerr =投射器(功能,MU,U)Npc = size(u,2); L = size(features,2);%Subtract class mean: Ns-by-L minus Ns-by-1s = features-mu;%1 x-lnormSqX = sum(abs(s).^2,1)'; err = Inf(Npc+1,L); err(1,:) = normSqX; err(2:end,:) = -abs(u'*s).^2;%1 x-ltotalerr = sqrt(sum(err,1));endendend

See Also

相关示例

更多关于