Undefined function 'extractLBP' for input arguments of type 'uint8'.

I get this errror in my model and want to Extract local patches from the testing images and make predictions using the predict function
Error:
Undefined function 'extractLBP' for input arguments of type 'uint8'.
Error in extractLBPFeatures (line 19)
lbpImg = extractLBP(img, 'NumNeighbors', numNeighbors, 'Radius', radius);
Error in face_recognition_model_PLS (line 14)
trainFeatures{i} = extractLBPFeatures(img, numNeighbors, radius, numBins);
Code:
%Face Recognition using LRR
%% Load a dataset of grayscale face images
Dataset = imageDatastore('ExtendedYaleB', 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
%% Split the data into training and testing sets
[trainImgs, testImgs] = splitEachLabel(Dataset, 0.7, 'randomized');
%% Extract local patches from the training images using the extractLBPFeatures function
numNeighbors = 8;
radius = 1;
numBins = numNeighbors*(numNeighbors-1)+3;
trainFeatures = cell(numel(trainImgs.Files),1);
for i = 1:numel(trainImgs.Files)
img = readimage(trainImgs,i);
trainFeatures{i} = LBPFeatures(img, numNeighbors, radius, numBins);
end
%% Train the local ridge regression model using the fitrlinear function and local ridge regression
lambda = 1;
span = 0.5;
for i = 1:numel(trainImgs.Files)
features = trainFeatures{i};
label = double(trainImgs.Labels(i));
idx = setdiff(1:numel(trainFeatures),i);
neighbors = vertcat(trainFeatures{idx});
neighborLabels = double(trainImgs.Labels(idx));
mdl = fitrlinear(neighbors,neighborLabels,'Learner','leastsquares','Lambda',lambda);
yhat = zeros(size(features));
for j = 1:size(features,1)
patch = features(j,:);
pred = predict(mdl,patch);
dist = pdist2(patch,neighbors);
w = exp(-dist.^2/(2*span^2));
yhat(j) = sum(w.*pred)/sum(w);
end
trainFeatures{i} = yhat;
end
trainFeatures = cell2mat(trainFeatures);
%% Train the linear regression model on the modified LBP features
mdl = fitrlinear(trainFeatures,double(trainImgs.Labels),'Learner','leastsquares','Lambda',lambda);
%% Save the model to a file
save('face_recognition_model.mat', 'mdl');
%% Extract local patches from the testing images and make predictions using the predict function
testFeatures = cell(numel(testImgs.Files),1);
for i = 1:numel(testImgs.Files)
img = readimage(testImgs,i);
testFeatures{i} = extractLBPFeatures(img, numNeighbors, radius, numBins);
end
testFeatures = cell2mat(testFeatures);
predictions = predict(mdl,testFeatures);
%% Evaluate the performance of the model using the confusionmat and classificationReport functions
confMat = confusionmat(testImgs.Labels,predictions);
classificationReport = classificationReport(testImgs.Labels,predictions);
%% Load the saved model from a file
load('face_recognition_model.mat');
%% Use the loaded model for prediction
testImg = imread('test_image.jpg');
testFeatures = extractLBPFeatures(testImg, numNeighbors, radius, numBins);
prediction = predict(mdl,testFeatures);

10 Kommentare

Do you have the Computer Vision toolbox installed?
Yes but I don't know why this error
What shows up for
which -all extractLBPFeatures
ver vision
Are you able to run the example
openExample('vision/VisualizeLBPFeatureHistogramsExample')
>> which -all extractLBPFeatures
C:\Users\AT\Desktop\Face Recognition\extractLBPFeatures.m
C:\Program Files\MATLAB\R2023a\toolbox\vision\vision\extractLBPFeatures.m % Shadowed
>> ver vision
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.14.0.2306882 (R2023a) Update 4
MATLAB License Number: DEMO
Operating System: Microsoft Windows 11 Pro Version 10.0 (Build 22621)
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
Computer Vision Toolbox Version 10.4 (R2023a)
C:\Users\AT\Desktop\Face Recognition\extractLBPFeatures.m
What is that file? It is not the Mathworks-supplied extractLBPFeatures code. It is interfering with invoking the Mathworks code.
In MATLAB, there are rules about which version of a function is used when there are multiple functions with the same name. Functions defined in the current directory have priority over library functions. Functions defined in the current file have priority over functions defined in the current directory. There are some additional complications, and there is provision for Object Oriented Programming, but most of the time, if you define a function yourself with the same name as a Mathworks-provided function, you might well end up interferring with calling the Mathworks version of the function.
Abdelrahman
Abdelrahman am 3 Aug. 2023
Bearbeitet: Abdelrahman am 3 Aug. 2023
I defined it manually after I have got the error that shown, but Now I deleted that and I will try again now but extractLBPFeatures not in my folder that contain the Dataset images and matlab file that I want to run is that normal?
That a result from:
>> which -all extractLBPFeatures
C:\Program Files\MATLAB\R2023a\toolbox\vision\vision\extractLBPFeatures.m
C:\Program Files\MATLAB\R2023a\toolbox\vision\vision\+vision\+internal\LBPImpl.m % vision.internal.LBPImpl method
Error using extractLBPFeatures>parseInputs
Expected a string scalar or character vector for the parameter name.
Error in extractLBPFeatures (line 10)
params = parseInputs(I,varargin{:});
Error in face_recognition_model (line 52)
testFeatures{i} = extractLBPFeatures(img, numNeighbors, radius, numBins);
I got this error again after rerun my code
You are calling the function incorrectly. It expects name/value pairs for everything except the image -- and there is no numBins parameter.
If you examine the example I directed you to, you will see that numBins is defined after the call to extractLBPFeatures and is used to reshape() the results of calling extractLBPFeatures with a CellSize parameter.
You mean that the correct to call extractLBPFeatures first can you replace my code?
to declare
testFeatures = extractLBPFeatures(testImg, 'NumNeighbors', numNeighbors, 'Radius', radius);

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

recent works
recent works am 4 Aug. 2023

9 Stimmen

The error you are encountering (Undefined function 'extractLBP' for input arguments of type 'uint8'.) indicates that the extractLBP function is not recognized. It seems that you might have intended to use extractLBPFeatures function instead of extractLBP.
In your code, you've defined LBPFeatures instead of extractLBPFeatures, so you should use LBPFeatures instead of extractLBP in the loop where you extract local features from training images. Modify the corresponding part of your code as follows:
% ... for i = 1:numel(trainImgs.Files)
img = readimage(trainImgs,i);
trainFeatures{i} = LBPFeatures(img, numNeighbors, radius, numBins);
end % ...
Make sure that you have properly implemented the LBPFeatures function or it is a valid function accessible in your MATLAB environment. If the function is not implemented yet, you need to define the LBPFeatures function with the desired functionality, or you can use the appropriate function for extracting LBP features that suits your application.
Additionally, double-check any other custom functions you are using and verify that they are defined and accessible in your code.
After making these changes, the error should be resolved, and your face recognition model should work as expected

5 Kommentare

Hello Dear,
You mean that?
%Face Recognition using LRR
%% Load a dataset of grayscale face images
Dataset = imageDatastore('ExtendedYaleB', 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
%% Split the data into training and testing sets
[trainImgs, testImgs] = splitEachLabel(Dataset, 0.7, 'randomized');
%% Extract local patches from the training images using the extractLBPFeatures function
numNeighbors = 8;
radius = 1;
numBins = numNeighbors*(numNeighbors-1)+3;
trainFeatures = cell(numel(trainImgs.Files),1);
for i = 1:numel(trainImgs.Files)
img = readimage(trainImgs,i);
trainFeatures{i} = LBPFeatures(img, numNeighbors, radius, numBins);
end
%% Train the local ridge regression model using the fitrlinear function and local ridge regression
lambda = 1;
span = 0.5;
for i = 1:numel(trainImgs.Files)
features = trainFeatures{i};
label = double(trainImgs.Labels(i));
idx = setdiff(1:numel(trainFeatures),i);
neighbors = vertcat(trainFeatures{idx});
neighborLabels = double(trainImgs.Labels(idx));
mdl = fitrlinear(neighbors,neighborLabels,'Learner','leastsquares','Lambda',lambda);
yhat = zeros(size(features));
for j = 1:size(features,1)
patch = features(j,:);
pred = predict(mdl,patch);
dist = pdist2(patch,neighbors);
w = exp(-dist.^2/(2*span^2));
yhat(j) = sum(w.*pred)/sum(w);
end
trainFeatures{i} = yhat;
end
trainFeatures = cell2mat(trainFeatures);
%% Train the linear regression model on the modified LBP features
mdl = fitrlinear(trainFeatures,double(trainImgs.Labels),'Learner','leastsquares','Lambda',lambda);
%% Save the model to a file
save('face_recognition_model.mat', 'mdl');
%% Extract local patches from the testing images and make predictions using the predict function
testFeatures = cell(numel(testImgs.Files),1);
for i = 1:numel(testImgs.Files)
img = readimage(testImgs,i);
testFeatures{i} = LBPFeatures(img, numNeighbors, radius, numBins);
end
testFeatures = cell2mat(testFeatures);
predictions = predict(mdl,testFeatures);
%% Evaluate the performance of the model using the confusionmat and classificationReport functions
confMat = confusionmat(testImgs.Labels,predictions);
classificationReport = classificationReport(testImgs.Labels,predictions);
%% Load the saved model from a file
load('face_recognition_model.mat');
%% Use the loaded model for prediction
testImg = imread('test_image.jpg');
testFeatures = extractLBPFeatures(testImg, numNeighbors, radius, numBins);
prediction = predict(mdl,testFeatures);
Error using confusionmat
G and GHAT need to be the same type.
Error in face_recognition_model (line 58)
confMat = confusionmat(testImgs.Labels,predictions);
fitrlinear returns a RegressionLinear object. predict() applied to such an object returns a numeric matrix. Your testImgs.Labels are probably text rather than numeric.
right.
Assumed that the Labels in testImgs are numeric, but they are likely text or categorical labels. Since fitrlinear returns a RegressionLinear object, its predict function will return numeric predictions, which might lead to issues when comparing them with text labels.
To handle the situation where the labels are text or categorical, you can follow these steps:
  1. Convert text labels to numeric values: Before training the model, convert the text or categorical labels in trainImgs.Labels to numeric values. You can use the grp2idx function to achieve this:
[trainImgs.Labels, labelIdx] = grp2idx(trainImgs.Labels);
Train the model with numeric labels: Now, use fitrlinear with the numeric labels:
mdl = fitrlinear(neighbors, labelIdx, 'Learner', 'leastsquares', 'Lambda', lambda);
Convert test labels to numeric for prediction: Similar to the training data, convert the test labels to numeric using grp2idx:
[testImgs.Labels, testLabelIdx] = grp2idx(testImgs.Labels);
Perform prediction and convert numeric predictions back to text labels: After obtaining numeric predictions, convert them back to their original text labels using the grp2idx reverse function, idx2grp:
predictionsIdx = predict(mdl, testFeatures);
predictions = idx2grp(predictionsIdx, labelIdx);
By converting the labels to numeric and then back to text, you ensure that the comparison between predictions and test labels is done correctly.
Remember that the success of this approach depends on the uniqueness and consistency of the labels. If there are any label mismatches or inconsistencies, the conversion might not be accurate. Therefore, ensure that your labels are properly formatted and unique.
Hello sir you mean Like that or there are any issue:
%Face Recognition using LRR
%% Load a dataset of grayscale face images
Dataset = imageDatastore('ExtendedYaleB', 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
%% Split the data into training and testing sets
[trainImgs, testImgs] = splitEachLabel(Dataset, 0.8, 'randomized');
%% Extract local patches from the training images using the extractLBPFeatures function
numNeighbors = 8;
radius = 1;
numBins = numNeighbors*(numNeighbors-1)+3;
trainFeatures = cell(numel(trainImgs.Files),1);
for i = 1:numel(trainImgs.Files)
img = readimage(trainImgs,i);
trainFeatures{i} = LBPFeatures(img, numNeighbors, radius, numBins);
end
%% Train the local ridge regression model using the fitrlinear function and local ridge regression
lambda = 1;
span = 0.5;
for i = 1:numel(trainImgs.Files)
features = trainFeatures{i};
label = double(trainImgs.Labels(i));
idx = setdiff(1:numel(trainFeatures),i);
neighbors = vertcat(trainFeatures{idx});
neighborLabels = double(trainImgs.Labels(idx));
mdl = fitrlinear(neighbors,neighborLabels,'Learner','leastsquares','Lambda',lambda);
yhat = zeros(size(features));
for j = 1:size(features,1)
patch = features(j,:);
pred = predict(mdl,patch);
dist = pdist2(patch,neighbors);
w = exp(-dist.^2/(2*span^2));
yhat(j) = sum(w.*pred)/sum(w);
end
trainFeatures{i} = yhat;
end
trainFeatures = cell2mat(trainFeatures);
%% Convert text labels to numeric values
[trainImgs.Labels, labelIdx] = grp2idx(trainImgs.Labels);
%% Train the linear regression model on the modified LBP features
mdl = fitrlinear(neighbors, labelIdx, 'Learner', 'leastsquares', 'Lambda', lambda);
%% Save the model to a file
save('face_recognition_model.mat', 'mdl');
%% Extract local patches from the testing images and make predictions using the predict function
testFeatures = cell(numel(testImgs.Files),1);
for i = 1:numel(testImgs.Files)
img = readimage(testImgs,i);
testFeatures{i} = LBPFeatures(img, numNeighbors, radius, numBins);
end
testFeatures = cell2mat(testFeatures);
%% Convert test labels to numeric for prediction
[testImgs.Labels, testLabelIdx] = grp2idx(testImgs.Labels);
%% Perform prediction and convert numeric predictions back to text labels
predictionsIdx = predict(mdl, testFeatures);
predictions = idx2grp(predictionsIdx, labelIdx);
%% Evaluate the performance of the model using the confusionmat and classificationReport functions
confMat = confusionmat(testImgs.Labels, predictions);
disp(confMat);
classification_report = classificationReport(testImgs.Labels,predictions);
disp(classification_report)
%% Load the saved model from a file
load('face_recognition_model.mat');
%% Use the loaded model for prediction
testImg = imread('ExtendedYaleB\yaleB11\yaleB11_P00A+000E+00_result.jpg');
testFeatures = LBPFeatures(testImg, numNeighbors, radius, numBins);
prediction = predict(mdl,testFeatures);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Deep Learning with Simulink finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by