How to save files with Canny edge detection and Feature extraction
2 次查看(过去 30 天)
显示 更早的评论
I'm trying to perform canny edge detection on images saved in a folder I have then save that data then run feature extraction and save it but I'm having an error when trying to save it. It looks like it doesn't like the file name I set for it but I don't know what to change it to work with the folder its calling from. I'll include the error message under the first function. The folder "training" has two sub folders called "Good" and "Bad" and within those folders are images. (I'm making a visual inspection system so those folders have good parts and bad parts to train a neural network)
Anyway here's the code and functions:
% Set the path to the main folder containing subfolders of images
disp('Setting a path to training data...'); % Output a message
dataPath = "C:\Users\jcfw0\Desktop\Matlab+Senior\VISData\Training";
% Create an image datastore
disp('Checking content number in folders...'); % Output a message
imds = imageDatastore(dataPath, ...
'IncludeSubfolders', true, ...
'LabelSource', 'foldernames');
% Display the number of images in each category
tbl = countEachLabel(imds)
% Canny Edge Detection and Save
disp('Performing Canny Edge Detection...'); % Output a message
outputPath = "C:\Users\jcfw0\Desktop\Matlab+Senior\VISData\CannyEdgeImages"; % Set the path to save Canny edge images
performCannyAndSave(imds, outputPath);
disp('Canny Edge Detection and Save complete.'); % Output a message
% Feature Extraction and Save
disp('Performing Feature Extraction...'); % Output a message
featureOutputPath = "C:\Users\jcfw0\Desktop\Matlab+Senior\VISData\Features"; % Set the path to save feature data
performFeatureExtractionAndSave(imds, featureOutputPath);
disp('Feature Extraction and Save complete.'); % Output a message
Function 1:
% Function for Canny Edge Detection and Save
function performCannyAndSave(imds, outputPath)
while hasdata(imds)
img = read(imds);
% Perform Canny edge detection on 'img'
edges = edge(rgb2gray(img), 'canny');
% Display original and Canny edge-detected images
figure;
subplot(1, 2, 1);
imshow(img);
title('Original Image');
subplot(1, 2, 2);
imshow(edges);
title('Canny Edge Detection');
% Get the current file name without path
[~, filename, ext] = fileparts(imds.Files{imds.FilesIndex});
% Save the result to the 'outputPath' with a new filename
imwrite(edges, fullfile(outputPath, [filename '_canny' ext]));
end
end
Error:
"Unrecognized method, property, or field 'FilesIndex' for class 'matlab.io.datastore.ImageDatastore'.
[~, filename, ext] = fileparts(imds.Files{imds.FilesIndex});"
Function 2:
% Function for Feature Extraction and Save
function performFeatureExtractionAndSave(imds, featureOutputPath)
while hasdata(imds)
img = read(imds);
% Perform feature extraction on 'img'
features = extractFeatures(img);
% Display the original image and extracted features
figure;
subplot(1, 2, 1);
imshow(img);
title('Original Image');
subplot(1, 2, 2);
bar(features);
title('Extracted Features');
% Save the features to the 'featureOutputPath' with a new filename
[~, filename, ~] = fileparts(imds.Files{imds.FilesIndex});
save(fullfile(featureOutputPath, [filename '_features.mat']), 'features');
end
end
0 个评论
回答(1 个)
Image Analyst
2023-12-5
You need to look over all files in a for loop instead of a while loop in performCannyAndSave
allFileNames = imds.Files
numFiles = numel(allFileNames)
for k = 1 : numFiles
inputFileName = allFileNames{k};
fprintf('Processing file %d of %d: "%s".\n', k, numFiles, inputFileName)
img = imread(inputFileName);
% Perform feature extraction on 'img'
features = extractFeatures(img);
% Display the original image and extracted features
figure;
subplot(1, 2, 1);
imshow(img);
title('Original Image');
drawnow;
subplot(1, 2, 2);
bar(features);
title('Extracted Features');
drawnow;
% Get the current file name without path
[~, baseFileNameNoExt, ext] = fileparts(allFileNames{k});
% Save the result to the 'outputPath' with a new filename
fullFileName = fullfile(outputPath, [baseFileNameNoExt, '_canny', ext]);
fprintf('Saving file %d of %d: "%s".\n', k, numFiles, fullFileName)
imwrite(edges, fullFileName);
end
1 个评论
Image Analyst
2023-12-8
编辑:Image Analyst
2023-12-17
@Joshua Corona, are you still alive?
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!