how to convert categorical data (76*1) of labels into target data of 3 classes (76*3) for nrptool?

2 ビュー (過去 30 日間)
santhosh kumar buddepu
santhosh kumar buddepu 2021 年 12 月 8 日
I have assigned labels to my database like "traindatabase.Labels" which gives 76*1 categorical data (metal pipe-19, plastic box-28, steel box-29). I need to classify into 3 classes and my target data should be 76*3 which is in the form of
1 1 1....1(19) 0 0 0... (remaining)
0 0 0..0(19) 1 1 1...1(28) 0 0...0(remaining)
0 0 0......0(47) 1 1 1...1(29)
how to make target like this? please help me
path1='D:\matprog\matfiles2\trainfiles'; % training path
path2='D:\matprog\matfiles2\testfiles'; % testing path
traindb=imageDatastore(path1,'IncludeSubfolders',true,'LabelSource','foldernames');
testdb=imageDatastore(path2,'IncludeSubfolders',true,'LabelSource','foldernames');
%% Training
img=readimage(traindb,1);
[pixelCounts GLs] = imhist(img); % GL-gray levels
% Get the number of pixels in the histogram.
numberOfPixels = sum(pixelCounts);
% Get the mean gray lavel.
meanGL = sum(GLs .* pixelCounts) / numberOfPixels
% Get the variance, which is the second central moment.
varianceGL = sum((GLs - meanGL) .^ 2 .* pixelCounts) / (numberOfPixels-1)
% Get the standard deviation.
sd = sqrt(varianceGL);
% Get the skew.
skew = sum((GLs - meanGL) .^ 3 .* pixelCounts) / ((numberOfPixels - 1) * sd^3)
% Get the kurtosis.
kurtosis = sum((GLs - meanGL) .^ 4 .* pixelCounts) / ((numberOfPixels - 1) * sd^4)
% Get the entropy.
ent = entropy(img)
FeatureVector = [meanGL varianceGL sd skew kurtosis ent];
featuresize=length(FeatureVector)
totaltrainimages=numel(traindb.Files);
trainingfeatures=zeros(totaltrainimages,featuresize,'single');
for i=1:totaltrainimages % train all the images in training folder
img=readimage(traindb,i);
[pixelCounts GLs] = imhist(img); % GL-gray levels
% Get the number of pixels in the histogram.
numberOfPixels = sum(pixelCounts);
% Get the mean gray lavel.
meanGL = sum(GLs .* pixelCounts) / numberOfPixels;
% Get the variance, which is the second central moment.
varianceGL = sum((GLs - meanGL) .^ 2 .* pixelCounts) / (numberOfPixels-1);
% Get the standard deviation.
sd = sqrt(varianceGL);
% Get the skew.
skew = sum((GLs - meanGL) .^ 3 .* pixelCounts) / ((numberOfPixels - 1) * sd^3);
% Get the kurtosis.
kurtosis = sum((GLs - meanGL) .^ 4 .* pixelCounts) / ((numberOfPixels - 1) * sd^4);
% Get the entropy.
ent = entropy(img);
FeatureVector = [meanGL varianceGL sd skew kurtosis ent];
featuresize=length(FeatureVector);
trainingfeatures(i, 1 : featuresize) = FeatureVector;
end
traininglabels=traindb.Labels; % assign labels for training

回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by