MATLAB Answers

Save Extracted Features (For Loop)

1 view (last 30 days)
Gigi N
Gigi N on 31 Jul 2021
Answered: Image Analyst on 31 Jul 2021
HI, I have the below code to perform a DWT and then perform feature extractions. However, I'm having difficulty saving the extraction values into .xlsx format because every new entry gets overwritten, and the 1st column values and 2nd column 1st row values are wrong (scroll down to the last part for the xlsx result). Please advise how I can go about doing this as I'm very new to MATLAB. Thank you.
srcFile = dir('filepath');
for i = 1:length(srcFile)
filename = strcat('filepath',srcFile(i).name);
j = imread(filename);
[xar,xhr,xvr,xdr] = dwt2(j(:,:,1),'bior5.5');
[xag,xhg,xvg,xdg] = dwt2(j(:,:,1),'bior5.5');
[xab,xhb,xvb,xdb] = dwt2(j(:,:,1),'bior5.5');
xa(:,:,1) = xar ; xa(:,:,2) = xag ; xa(:,:,3) = xab;
xh(:,:,1) = xhr ; xh(:,:,2) = xhg ; xh(:,:,3) = xhb;
xv(:,:,1) = xvr ; xv(:,:,2) = xvg ; xv(:,:,3) = xvb;
xd(:,:,1) = xdr ; xd(:,:,2) = xdg ; xd(:,:,3) = xdb;
[xaar,xhhr,xvvr,xddr] = dwt2(xa(:,:,1),'bior5.5');
[xaag,xhhg,xvvg,xddg] = dwt2(xa(:,:,1),'bior5.5');
[xaab,xhhb,xvvb,xddb] = dwt2(xa(:,:,1),'bior5.5');
xaa(:,:,1) = xaar ; xaa(:,:,2) = xaag ; xaa(:,:,3) = xaab;
xhh(:,:,1) = xhhr ; xhh(:,:,2) = xhhg ; xhh(:,:,3) = xhhb;
xvv(:,:,1) = xvvr ; xvv(:,:,2) = xvvg ; xvv(:,:,3) = xvvb;
xdd(:,:,1) = xddr ; xdd(:,:,2) = xddg ; xdd(:,:,3) = xddb;
DWT_feat = [xaa,xhh,xvv,xdd];
gray = rgb2gray(DWT_feat);
glcm = graycomatrix(gray);
stats = graycoprops(glcm,'Contrast Correlation Energy Homogeneity');
Contrast = stats.Contrast;
Correlation = stats.Correlation;
Energy = stats.Energy;
Homogeneity = stats.Homogeneity;
Mean = mean2(DWT_feat);
StdDev = std2(DWT_feat);
Entropy = entropy(DWT_feat);
RMS = mean2(rms(DWT_feat));
Variance = mean2(var(double(DWT_feat)));
a = sum(double(DWT_feat(:)));
Smoothness = 1-(1/(1+a));
Kurtosis = kurtosis(double(DWT_feat(:)));
Skewness = skewness(double(DWT_feat(:)));
VariableNames = {'Contrast','Correlation', 'Energy', 'Homogeneity', 'Mean', 'StdDev', 'Entropy', 'RMS', 'Variance', 'Smoothness', 'Kurtosis', 'Skewness'};
T = table(Contrast(:), Correlation(:), Energy(:), Homogeneity(:), Mean(:), StdDev(:), Entropy(:), RMS(:), Variance(:), Smoothness(:), Kurtosis(:), Skewness(:));
AT = array2table(T,'VariableNames', {'Contrast','Correlation', 'Energy', 'Homogeneity', 'Mean', 'StdDev', 'Entropy', 'RMS', 'Variance', 'Smoothness', 'Kurtosis', 'Skewness'});
disp(AT);
xlswrite('savepath',AT,'Sheet1','A1');
end
Result (which is wrong):
A T Energy Homogeneity Mean StdDev Entropy RMS Variance Smoothness Kurtosis Skewness
T 0.720361 0.3986 0.871775 24.72161 95.91721 1.395854 37.76407 4277.009 1 32.69805 5.19293

Answers (1)

Image Analyst
Image Analyst on 31 Jul 2021
You need to index the variables that you want to save a value for on every iteration. For example:
Entropy(i) = entropy(DWT_feat);
but it's better not to use i (the imaginary variable) as a loop counter, Use k instead.
Also, don't use built-in function names, like gray(), as a variable name. Use grayImage instead.
Next put in comments before you write the code, then write the code. Or at least put in comments AS you write the code. But to not have any comments at all would not fly in my company.
Finally, when you post code, you can highlight it and click the code icon. That will format it properly and allow us to copy the whole code with a single click so we can try it out on our computers.

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!