How to solve "brace indexing is not supported for variables of this type" for this code?
조회 수: 257 (최근 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
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
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 Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!