I am getting * error Conversion to uint8 from cell is not possible, in following code.
14 次查看(过去 30 天)
显示 更早的评论
Kindly help me to solve this error.
% Define a starting folder.
start_path = 'C:\Users\saeeda\Desktop\Training';
% Ask user to confirm or change.
topLevelFolder = start_path;
if topLevelFolder == 0
return;
end
% Get list of all subfolders.
allSubFolders = genpath(topLevelFolder);
% Parse into a cell array.
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, ';');%#ok (suppres m-lint warning)
if isempty(singleSubFolder)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];%#ok (suppres m-lint warning)
end
numberOfFolders = length(listOfFolderNames);
totalFileList={};%this will contain all file names of jpg files
% Process all image files in those folders.
for k = 1 : numberOfFolders
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
%fprintf('Processing folder %s\n', thisFolder);
% Get JPG files.
filePattern = sprintf('%s/*.jpg', thisFolder);
baseFileNames = dir(filePattern);
numberOfImageFiles = length(baseFileNames);
% Now we have a list of all files in this folder.
if numberOfImageFiles >= 1
% Go through all those image files.
for f = 1 : numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
totalFileList{end+1}=fullFileName;%#ok (suppres m-lint warning)
%fprintf(' Processing image file %s\n', fullFileName);
end
else
%fprintf(' Folder %s has no image files in it.\n', thisFolder);
end
end
% Initilizing the variable for fast loading
trainSet = uint8(zeros(48*48,length(totalFileList)));
for i = 1 :length(totalFileList)
img = fullfile(totalFileList, totalFileList{i});
trainSet(:,i) = img(:);
end
0 个评论
回答(1 个)
Walter Roberson
2018-1-18
You have
trainSet = uint8(zeros(48*48,length(totalFileList)));
so your output variable trainSet is an array of uint8.
Then you have
for i = 1 :length(totalFileList)
img = fullfile(totalFileList, totalFileList{i});
trainSet(:,i) = img(:);
end
where you assign img(:) to part of that array of uint8. For that to be possible, img() would have to be numeric or character or a symbolic expression with no free variables (character and sym would be converted to numeric before storing).
So let us go back a step to see how it was assigned. We see that img is the result of a fullfile() call. The result of a fullfile() call is either a character vector or a cell array of character vectors. If the result of the fullfile() were character then the assignment could potentially work, provided the sizes matched, because character can be converted to numeric. No warning or error would be used if character were being converted to numeric, but an error would be created if the lengths did not match in doing so.
But what about the possibility that fullfile() has returned a cell array of character vectors? In that case you would be trying to assign a cell array into a numeric location, which would give exactly the error you mentioned. We can be fairly sure that fullfile() is returning a cell array of character vectors.
fullfile() returns a cell array of character vectors if at least one of its inputs is a cell array instead of being a plain character vector. We can look at the second argument and see that totalFileList is being accessed with cell array indexing. Is the result of the {i} cell indexing a cell array itself or is it a character vector? We can postpone that question for a moment. Now look at the first argument, which is totalFileList without any cell indexing. As we just established that totalFileList is a cell array of something, we know that the first input is a cell array, so at least one of the two inputs is a cell array, which is enough to trigger the output being a cell array instead of being a character vector.
If you search back further in the code, totalFileList is a cell array of character vectors. It does not make sense to fullfile() using the entire cell array as the first argument with a part of the same cell array as the second argument. There are cases where you want to pass a cell array first argument to fullfile(), but this is not one of them.
I would suggest to you that you construct the file name in totalFileList to already include the appropriate directory name for each element -- possibly by calling fullfile() at that earlier stage. And then in the line I pointed out, the assignment to img, you would use totalFIleList{i}, which would get you a character vector. It would then be valid to assign that character vector to trainSet(:,i) provided that the lengths matched -- that is, that every file name happened to be exactly (48*48) = 2304 characters long.
You appear to be using MS Windows. In MS Windows, the maximum file name length under typical circumstances is 259 characters. In order to be working with file names that are exactly 2304 characters long, you would need to code in terms of "UNC paths"; https://support.microsoft.com/en-us/help/967030/what-is-unc-pathing . This is not the default for the information returned from dir() so you are going to need to create the UNC versions of the names yourself. UNC names are typically longer than regular names, which will help you fill out those 2304 characters, but you are still going to have a fair bit of struggle coming up with names that are exactly 2304 characters long.
I must admit that it seems strange to me to want to train upon file names...
4 个评论
Walter Roberson
2018-1-18
Notice that in that code, the [myDir dd(1).name] constructs a file name character vector, and then imread() is used on that character vector, and the image that is read in is stored in trainSet.
In the code you posted, after the corrections I wrote about through, including the recommendation to add the path at the time you are looking through the directories, then at that point in the code totalFileList{i} would be the character vector. You then try to store the character vector itself into trainSet: somehow you deleted the imread() step against that character vector. As in
img = imread(totalFileList{i});
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Name Construction 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!