如何为许多图像保存单个.mat文件(循环)?

时间:2018-01-09 09:28:50

标签: matlab for-loop matrix

我训练了两个分类(svmtrain),其中包含15个18个图像的特征,并且我在一个图像中有很多对象。我想保存这些图像的单个.mat文件。我的问题是,当我运行此代码时,我只有最后一个图像的变量。

这是我的代码:

NbIm = size(names1,1);
n1 = 1;
n2 = NbIm;
for n = n1:n2
    %1- Read the original image
    %2- Processing :  Segmentation
    %3- characterization :
    [B3,L3,N3] = bwboundaries(ICellules); (see picture)
    CC = bwconncomp(L3);
    BW = bwlabel(L3);
    stats1 = regionprops(CC,'Area','Centroid','Eccentricity','Perimeter','ConvexArea','ConvexHull','ConvexImage','MajorAxisLength','MinorAxisLength','Orientation','Solidity','BoundingBox');
    for k = 1:length(B3),
        V = [];    glcm = [];
        V = Im_originale(BW==k);
        glcm = graycomatrix(V,'Offset',[2 0],'Symmetric', true);
        stats= graycoprops(glcm);
        Contrast_Cellule = stats.Contrast;
        Correlation_Cellule = stats.Correlation;
        Energy_Cellule = stats.Energy;
        Homogeneity_Cellule = stats.Homogeneity;
        Area_cellule = stats1(k).Area;
        Perimeter_cellule = stats1(k).Perimeter;
        Circularity_cellule = (4*pi*Area_cellule)/Perimeter_cellule^2;
        Centroid_cellule = stats1(k).Centroid;
        Compactness_cellule = Perimeter_cellule^2/(4*pi*Area_cellule);
        MajorAxis_cellule = stats1(k). MajorAxisLength;
        MinorAxis_cellule = stats1(k). MinorAxisLength;
        Orientation_cellule = stats1(k).Orientation;
        Eccentricity_cellule = stats1(k).Eccentricity;
        Solidity_cellule = stats1(k).Solidity;
        boundary3 = B3{k};
        [cc] = chaincode(boundary3);
        ai = cc.code;
        ai = ai.';
        output = calc_harmonic_coefficients(ai,30);
        Ampl = 0.5*sqrt((output(1)^2)+(output(2)^2)+(output(3)^2)+(output(4)^2));
        Feat(k,:) = [Area_cellule,Perimeter_cellule,Circularity_cellule,Compactness_cellule, Solidity_cellule,Eccentricity_cellule,MajorAxis_cellule,MinorAxis_cellule, Centroid_cellule,Ampl,Contrast_Cellule,Correlation_Cellule,Energy_Cellule,Homogeneity_Cellule];
    end
end
save('FeatTrain.mat', 'Feat');

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

每次运行内循环(k=...)时,都会覆盖Feat,而不会保存它。

如果您想为Feat设置一个变量,而不是保存多个.mat文件,则有两种方法:

  1. 如果每次运行内循环时Feat的大小相同,则可以将行改为Feat(n-n1+1,k,:)=[...,使其成为3D矩阵。这将生成一个3D矩阵,其中每个Feat(n,:,:)将成为每个图像的特征。
  2. 相反,如果每次运行内部循环Feat都可以更改大小,则使用单元格数组,如Feat{n-n1+1}(k,:)=[...中所示。