How do I export image features matrix data to Excel for multiple images in the same folder?

조회 수: 10 (최근 30일)
I have 100 image files which I need to process (Images are in the same folder). The idea is to take one image, perform pre-processing (noise removal, binarization etc.) followed by features extraction.I've used Wavelets (db1) to extract approximation & directional coefficients (horizontal, vertical and diagonal)-4 features per image.I need to store this features matrix in Excel format.How do I automate the process for 100 image files, so that I get an Excel sheet containing 100x4 entries?
if true
% clc;
%Code begins from here
clc;
clear all;
close all;
%Load current directory
myFolder = 'E:\BE Project\Devnagiri Character Recognition\test_images\trial';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
%imshow(imageArray); % Display image.
%drawnow; % Force display to update immediately.
end
a=imageArray;
%Scale to 512x512 since images are of higher resolution
a=imresize(a, [512 512]);
%RGB to gray
if size(a,3)==3
b=rgb2gray(a);
end
figure;
imshow(a);
title('Original Image')
%Noise removal by median filter
f=medfilt2(b);
figure;
imshow(f);
title('Noise removal by median filter')
%Determine threshold by Otsu's method
th=graythresh(f);
g=im2bw(f,th);
figure;
imshow(g);
title('Binarized Image (Otsu Method)')
%Resize image for Wavelet Transformation
g=imresize(g, [128 128]);
figure;
imshow(g);
title('Resized Image')
%Find wavelet coefficients using 2D Wavelet Transform
[cA,cH,cV,cD]=dwt2(g,'db1');
disp('---WAVELET FEATURES---');
%Calculate max value for approximation coefficient
disp('Approximation Coefficient (Max Value) :');
ca_max=max(mean(double(cA)));
disp((ca_max));
%Calculate max value for horizontal coefficient
disp('Horizontal Coefficient (Max Value) :');
ch_max=max(mean(double(cH)));
disp((ch_max));
%Calculate max value for vertical coefficient
disp('Vertical Coefficient (Max Value) :');
cv_max=max(mean(double(cV)));
disp((cv_max));
%Calculate max value for diagonal coefficient
disp('Diagonal Coefficient (Max Value) :');
cd_max=max(mean(double(cD)));
disp(mean(cd_max));
%Obtain features in a matrix form
ft=cat(2,ca_max,ch_max,cv_max,cd_max);
%Export features matrix to Excel sheet
xlswrite('testdata.xlsx',ft);
end
  댓글 수: 1
MARIA MALAKOPOULOU
MARIA MALAKOPOULOU 2020년 12월 20일
hello ! Can i ask you something ? Why you take the max value for all the coefficients?
Thanks in advance!

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2013년 1월 5일
Something like this:
numberOfResults = 42; % Whatever it is for a single image
numberOfImages = length(filenames); % Whatever...
allResults = zeros(numberOfImages, numberOfResults);
for k = 1 : numberOfImages
% Read in image into variable called thisImage.
% Analyze the image with function AnalyzeSingleImage and get results.
thisImagesResults = AnalyzeSingleImage(thisImage);
% Store results in allResults:
allResults(k, :) = thisImagesResults;
end
xlswrite(excelFullFileName, allResults);
  댓글 수: 9
Devashish Tiwari
Devashish Tiwari 2022년 7월 22일
Yeah Exactly sir, I have multiple images to do so. But, I actually wanted to know, from those multiple images, how do I call them, and how to save the tracked particles in each image. Also, How to save the excel sheets of each image?
But, to solve this problem, I needed the work of each image and then the for loop, so I started doing so.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Walter Roberson
Walter Roberson 2013년 1월 5일
  댓글 수: 3
Walter Roberson
Walter Roberson 2013년 1월 5일
Save the entries into a single common array. xlswrite() the array only after all 100 are done.
Ragavi Thiyagarajan
Ragavi Thiyagarajan 2020년 2월 27일
i am also have a same problem with exporting extracted features from matlab to excel.. can explain me to .how to save the entries in a single array?

댓글을 달려면 로그인하십시오.


Braiki Marwa
Braiki Marwa 2018년 1월 9일
편집: Image Analyst 2018년 1월 9일
i trained two-class-classification (svmtrain) with 15 features for 18 images and i have different number of objects in a single image. I want to save single .mat file of features of these images?my problem is when i run this code i have just the variables of last image!!This is my code :
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
  댓글 수: 3
Braiki Marwa
Braiki Marwa 2018년 1월 10일
Hi, thanks for your response...can you please explain more because i don't understand
Image Analyst
Image Analyst 2018년 1월 10일
Actually I thought B3 was an image, but it's not, so your for loop line should also work as it.
For the line
Area_cellule = stats1(k).Area;
you're just taking the k'th area variable and assigning it to Area_cellule. So for k=1 (first iteration) Area_cellule = the first area. For k=2, Area_cellule = the second area. For the last iteration Area_cellule = the last area. And when the loop finally exits, Area_cellule is just a single number equal to the last area, NOT an array of all areas like you said you wanted. To get an array, you either have to do
Area_cellule*k( = stats1(k).Area;
inside the loop, or better, don't assign Area_cellule at all inside the loop and have this outside the loop:
Area_cellule = [stats1.Area];
Same for all the other properties of stats1 you're trying to make into individual arrays, like Perimeter_cellule etc.

댓글을 달려면 로그인하십시오.


anuar ibrahim
anuar ibrahim 2019년 4월 6일
numberOfResults = 42; % Whatever it is for a single image
numberOfImages = length(filenames); % Whatever...
allResults = zeros(numberOfImages, numberOfResults);
for k = 1 : numberOfImages
% Read in image into variable called thisImage.
% Analyze the image with function AnalyzeSingleImage and get results.
thisImagesResults = AnalyzeSingleImage(thisImage);
% Store results in allResults:
allResults(k, :) = thisImagesResults;
end
xlswrite(excelFullFileName, allResults);
can u show with the code please? i dont understand

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by