How to solve "brace indexing is not supported for variables of this type" for this code?
269 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Tawsif Mostafiz
le 25 Mai 2021
Modifié(e) : Image Analyst
le 25 Mai 2021
Excerpt of my code is as follows:
s=0;
for k = 1 : length(theFiles)
for q=0.1:0.01:0.3
histgrm=im2bw(maskedRgbImage,q);
num{k}=sum(histgrm(:) == 0);
if(num{k}>400)
number{s}=num{k};
s=s+1;
end
end
end
for s=0:19
fprintf("%d\n",number{s})
end
length(theFiles), histgrm are defined in the original code. But it shows error as:
Unable to perform assignment because brace indexing is not supported for variables of this type.
How can I fix this?
3 commentaires
Réponse acceptée
Image Analyst
le 25 Mai 2021
No need for cell arrays, which use braces. Use just regular double arrays with parentheses:
num(k) = sum(histgrm(:) == 0);
if(num(k)>400)
number(s) = num(k);
I think it's also deceptive to name a binary image "histgrm" which leads the reader to think it's a histogram instead of a binary image outputted from im2bw().
Next you need to initialize s to 1 instead of zero because there is no zeroth element in MATLAB.
2 commentaires
Image Analyst
le 25 Mai 2021
Modifié(e) : Image Analyst
le 25 Mai 2021
Nowhere in my code do I convert a cell to a double. The output of im2bw() is a logical -- a binary image though you deceptively called it a histogram. Nowhere did I even use braces to address it as a double. See the very first item on the FAQ to get an appreciation of what cell arrays are and when to use braces, parentheses, and brackets:
There is just so much wrong with your code I don't want to explain every thing I fixed, just see this fixed code below:
theFiles = dir('*.png');
allQs = 0.1 : 0.01 : 0.3;
numqs = length(allQs);
numFiles = length(theFiles);
number = zeros(numqs, numFiles);
for k = 1 : numFiles
fullFileName = fullfile(theFiles(k).folder, theFiles(k).name);
grayImage = imread(fullFileName);
subplot(2, 1, 1);
imshow(grayImage);
caption = sprintf('#%d of %d : "%s"', k, numFiles, theFiles(k).name);
title(caption, 'Interpreter', 'none');
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% Convert to gray scale.
grayImage = rgb2gray(grayImage);
end
for k2 = 1 : numqs
q = allQs(k2);
binaryImage = imbinarize(grayImage, q);
subplot(2, 1, 2);
imshow(binaryImage);
caption = sprintf('q = %.2f', q);
title(caption, 'Interpreter', 'none');
drawnow;
numZeros = nnz(~binaryImage);
% If there are more than 400 pixels above the threshold, log it.
if(numZeros > 400)
number(k2, k) = numZeros;
end
end
% Print out the values for this image.
fprintf('For %s, number = \n ', theFiles(k).name);
for k2 = 1 : numqs
fprintf("%d ", number(:, k))
end
fprintf('\n');
end
figure
imshow(number, []);
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Startup and Shutdown dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!