regarding fake currency detection matlab code...it is showing "real" even if i upload fake image,please rectify my code
3 views (last 30 days)
Show older comments
% 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
1 Comment
Image Analyst
on 23 Jan 2025
Yes, that's possible. Many counterfeit notes can look the same as real notes in photos. You can't tell from photos alone. If you could, it would have to be a really lousy fake.
Answers (1)
Hitesh
on 5 Mar 2025
Edited: Hitesh
on 5 Mar 2025
Hi Thanusha,
This happens when the model's features are insufficient to effectively differentiate between real and fake notes. You need to use "confusionmat" to compute the accuracy and precision of your model by creating confusion matrix.You can Consider the following suggestions to improve the accuracy of your currency detection model:
- Expand Training Data: The training data for both real and fake currency is currently minimal. Expand this dataset with more examples to better train the SVM model. More diverse and representative data will improve the model's ability to generalize.
- Feature Extraction: Ensure that the feature extraction process is correctly capturing distinguishing features between real and fake currencies. You need to refine the features or add additional ones to improve discrimination.
- Review SVM Parameters: Experiment with different SVM parameters such as kernel functions, box constraints, and other hyperparameters to improve classification performance. Cross-validation can help in selecting the best parameters.
For more information regarding the "Feature Extraction" and "confusionmat" , kindly refer to the following MATLAB Documentation:
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!