regarding fake currency detection matlab code…it is showing “real” even if i upload fake image,please rectify my code
% Step 1: Load the Image
[FILENAME, PATHNAME] = uigetfile(‘*.jpg’, ‘Select an Image’);
if isequal(FILENAME, 0)
disp(‘User canceled the image selection.’);
return;
end
FilePath = strcat(PATHNAME, FILENAME);
disp(‘Selected Image Location:’);
disp(FilePath);
% Step 2: Read and Resize the Image
DataArray = imread(FilePath); % Read the image
DataArray = imresize(DataArray, [300, 650]); % Resize image to a standard size
figure, imshow(DataArray); % Display image
title(‘Original Image’);
% Step 3: Convert to Grayscale
Igray = rgb2gray(DataArray); % Convert to grayscale
figure, imshow(Igray); % Display grayscale image
title(‘Grayscale Image’);
% Step 4: Noise Removal using Median Filter
Igray = medfilt2(Igray); % Apply median filter to remove noise
figure, imshow(Igray);
title(‘Denoised Grayscale Image’);
% Step 5: Edge Detection (using Sobel operator)
edges = edge(Igray, ‘Sobel’); % Detect edges using Sobel method
figure, imshow(edges);
title(‘Edge Detection’);
% Step 6: Feature Extraction – Texture Features using GLCM
% Calculate the Gray Level Co-occurrence Matrix (GLCM)
glcm = graycomatrix(Igray, ‘Offset’, [0 1; -1 1; -1 0; -1 -1]);
stats = graycoprops(glcm, {‘Contrast’, ‘Correlation’, ‘Energy’, ‘Homogeneity’});
% Extract features from GLCM
Contrast = stats.Contrast;
Correlation = stats.Correlation;
Energy = stats.Energy;
Homogeneity = stats.Homogeneity;
% Step 7: Feature Extraction – Shape Features using Regionprops
props = regionprops(edges, ‘Area’, ‘Eccentricity’, ‘Solidity’);
Area = [props.Area]; % Extract Area
Eccentricity = [props.Eccentricity]; % Extract Eccentricity
Solidity = [props.Solidity]; % Extract Solidity
% Step 8: Create Feature Vector
Features = [mean(Contrast), mean(Correlation), mean(Energy), mean(Homogeneity), mean(Area), mean(Eccentricity), mean(Solidity)];
disp(‘Extracted Features:’);
disp(Features);
% Step 9: Prepare Training Data (for SVM)
% Example of real and fake currency training data (manually labeled)
% In real projects, you’d use a dataset with images of real and fake currency
Real_Data = [
% Example feature values for real currency
0.1, 0.9, 0.8, 0.7, 2500, 0.6, 0.95; % Example feature values for real currency
% Add more real currency data points
];
Fake_Data = [
% Example feature values for fake currency
0.5, 0.4, 0.2, 0.3, 2000, 0.8, 0.3; % Example feature values for fake currency
% Add more fake currency data points
];
% Step 10: Labels for Training Data
% 1 for real currency, 0 for fake currency
Real_Labels = ones(size(Real_Data, 1), 1);
Fake_Labels = zeros(size(Fake_Data, 1), 1);
% Combine real and fake data
Train_Data = [Real_Data; Fake_Data];
Train_Labels = [Real_Labels; Fake_Labels];
% Step 11: Train an SVM Classifier
svmModel = fitcsvm(Train_Data, Train_Labels, ‘KernelFunction’, ‘rbf’, ‘Standardize’, true);
% Step 12: Test the classifier with the current image features
[Label, Score] = predict(svmModel, Features); % Classify using the SVM model
% Step 13: Display the result
if Label == 1
msgbox(‘Currency Type: Real’);
else
msgbox(‘Currency Type: Fake’);
end
% Step 14: Display Final Image with Result
figure, imshow(DataArray);
if Label == 1
title(‘Detected: Real Currency’);
else
title(‘Detected: Fake Currency’);
end% Step 1: Load the Image
[FILENAME, PATHNAME] = uigetfile(‘*.jpg’, ‘Select an Image’);
if isequal(FILENAME, 0)
disp(‘User canceled the image selection.’);
return;
end
FilePath = strcat(PATHNAME, FILENAME);
disp(‘Selected Image Location:’);
disp(FilePath);
% Step 2: Read and Resize the Image
DataArray = imread(FilePath); % Read the image
DataArray = imresize(DataArray, [300, 650]); % Resize image to a standard size
figure, imshow(DataArray); % Display image
title(‘Original Image’);
% Step 3: Convert to Grayscale
Igray = rgb2gray(DataArray); % Convert to grayscale
figure, imshow(Igray); % Display grayscale image
title(‘Grayscale Image’);
% Step 4: Noise Removal using Median Filter
Igray = medfilt2(Igray); % Apply median filter to remove noise
figure, imshow(Igray);
title(‘Denoised Grayscale Image’);
% Step 5: Edge Detection (using Sobel operator)
edges = edge(Igray, ‘Sobel’); % Detect edges using Sobel method
figure, imshow(edges);
title(‘Edge Detection’);
% Step 6: Feature Extraction – Texture Features using GLCM
% Calculate the Gray Level Co-occurrence Matrix (GLCM)
glcm = graycomatrix(Igray, ‘Offset’, [0 1; -1 1; -1 0; -1 -1]);
stats = graycoprops(glcm, {‘Contrast’, ‘Correlation’, ‘Energy’, ‘Homogeneity’});
% Extract features from GLCM
Contrast = stats.Contrast;
Correlation = stats.Correlation;
Energy = stats.Energy;
Homogeneity = stats.Homogeneity;
% Step 7: Feature Extraction – Shape Features using Regionprops
props = regionprops(edges, ‘Area’, ‘Eccentricity’, ‘Solidity’);
Area = [props.Area]; % Extract Area
Eccentricity = [props.Eccentricity]; % Extract Eccentricity
Solidity = [props.Solidity]; % Extract Solidity
% Step 8: Create Feature Vector
Features = [mean(Contrast), mean(Correlation), mean(Energy), mean(Homogeneity), mean(Area), mean(Eccentricity), mean(Solidity)];
disp(‘Extracted Features:’);
disp(Features);
% Step 9: Prepare Training Data (for SVM)
% Example of real and fake currency training data (manually labeled)
% In real projects, you’d use a dataset with images of real and fake currency
Real_Data = [
% Example feature values for real currency
0.1, 0.9, 0.8, 0.7, 2500, 0.6, 0.95; % Example feature values for real currency
% Add more real currency data points
];
Fake_Data = [
% Example feature values for fake currency
0.5, 0.4, 0.2, 0.3, 2000, 0.8, 0.3; % Example feature values for fake currency
% Add more fake currency data points
];
% Step 10: Labels for Training Data
% 1 for real currency, 0 for fake currency
Real_Labels = ones(size(Real_Data, 1), 1);
Fake_Labels = zeros(size(Fake_Data, 1), 1);
% Combine real and fake data
Train_Data = [Real_Data; Fake_Data];
Train_Labels = [Real_Labels; Fake_Labels];
% Step 11: Train an SVM Classifier
svmModel = fitcsvm(Train_Data, Train_Labels, ‘KernelFunction’, ‘rbf’, ‘Standardize’, true);
% Step 12: Test the classifier with the current image features
[Label, Score] = predict(svmModel, Features); % Classify using the SVM model
% Step 13: Display the result
if Label == 1
msgbox(‘Currency Type: Real’);
else
msgbox(‘Currency Type: Fake’);
end
% Step 14: Display Final Image with Result
figure, imshow(DataArray);
if Label == 1
title(‘Detected: Real Currency’);
else
title(‘Detected: Fake Currency’);
end % Step 1: Load the Image
[FILENAME, PATHNAME] = uigetfile(‘*.jpg’, ‘Select an Image’);
if isequal(FILENAME, 0)
disp(‘User canceled the image selection.’);
return;
end
FilePath = strcat(PATHNAME, FILENAME);
disp(‘Selected Image Location:’);
disp(FilePath);
% Step 2: Read and Resize the Image
DataArray = imread(FilePath); % Read the image
DataArray = imresize(DataArray, [300, 650]); % Resize image to a standard size
figure, imshow(DataArray); % Display image
title(‘Original Image’);
% Step 3: Convert to Grayscale
Igray = rgb2gray(DataArray); % Convert to grayscale
figure, imshow(Igray); % Display grayscale image
title(‘Grayscale Image’);
% Step 4: Noise Removal using Median Filter
Igray = medfilt2(Igray); % Apply median filter to remove noise
figure, imshow(Igray);
title(‘Denoised Grayscale Image’);
% Step 5: Edge Detection (using Sobel operator)
edges = edge(Igray, ‘Sobel’); % Detect edges using Sobel method
figure, imshow(edges);
title(‘Edge Detection’);
% Step 6: Feature Extraction – Texture Features using GLCM
% Calculate the Gray Level Co-occurrence Matrix (GLCM)
glcm = graycomatrix(Igray, ‘Offset’, [0 1; -1 1; -1 0; -1 -1]);
stats = graycoprops(glcm, {‘Contrast’, ‘Correlation’, ‘Energy’, ‘Homogeneity’});
% Extract features from GLCM
Contrast = stats.Contrast;
Correlation = stats.Correlation;
Energy = stats.Energy;
Homogeneity = stats.Homogeneity;
% Step 7: Feature Extraction – Shape Features using Regionprops
props = regionprops(edges, ‘Area’, ‘Eccentricity’, ‘Solidity’);
Area = [props.Area]; % Extract Area
Eccentricity = [props.Eccentricity]; % Extract Eccentricity
Solidity = [props.Solidity]; % Extract Solidity
% Step 8: Create Feature Vector
Features = [mean(Contrast), mean(Correlation), mean(Energy), mean(Homogeneity), mean(Area), mean(Eccentricity), mean(Solidity)];
disp(‘Extracted Features:’);
disp(Features);
% Step 9: Prepare Training Data (for SVM)
% Example of real and fake currency training data (manually labeled)
% In real projects, you’d use a dataset with images of real and fake currency
Real_Data = [
% Example feature values for real currency
0.1, 0.9, 0.8, 0.7, 2500, 0.6, 0.95; % Example feature values for real currency
% Add more real currency data points
];
Fake_Data = [
% Example feature values for fake currency
0.5, 0.4, 0.2, 0.3, 2000, 0.8, 0.3; % Example feature values for fake currency
% Add more fake currency data points
];
% Step 10: Labels for Training Data
% 1 for real currency, 0 for fake currency
Real_Labels = ones(size(Real_Data, 1), 1);
Fake_Labels = zeros(size(Fake_Data, 1), 1);
% Combine real and fake data
Train_Data = [Real_Data; Fake_Data];
Train_Labels = [Real_Labels; Fake_Labels];
% Step 11: Train an SVM Classifier
svmModel = fitcsvm(Train_Data, Train_Labels, ‘KernelFunction’, ‘rbf’, ‘Standardize’, true);
% Step 12: Test the classifier with the current image features
[Label, Score] = predict(svmModel, Features); % Classify using the SVM model
% Step 13: Display the result
if Label == 1
msgbox(‘Currency Type: Real’);
else
msgbox(‘Currency Type: Fake’);
end
% Step 14: Display Final Image with Result
figure, imshow(DataArray);
if Label == 1
title(‘Detected: Real Currency’);
else
title(‘Detected: Fake Currency’);
end image processing MATLAB Answers — New Questions