How to solve "brace indexing is not supported for variables of this type" for this code?

258 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2021-5-25
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 个评论
Image Analyst
Image Analyst 2021-5-25
编辑:Image Analyst 2021-5-25
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, []);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by