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
Tawsif Mostafiz
Tawsif Mostafiz 2021년 5월 25일
Sorry for being vague.
class(num) = 'cell'
class(number) = "double'

댓글을 달려면 로그인하십시오.

채택된 답변

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
Tawsif Mostafiz
Tawsif Mostafiz 2021년 5월 25일
@Image Analyst when I do it, this shows:
Conversion to cell from double is not possible.
histgrm is defined as:
histgrm=im2bw(maskedRgbImage,q);
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