Conversion to logical from table is not possible
84 次查看(过去 30 天)
显示 更早的评论
Dear All,
I have the coding below, but got error. the data as attached.
%% I131 10:1 graycutoff 100000 latest GUNA FWHM
clc
clear all
close all
[spect map]=dicomread('I-131sphere10nisbah1.dcm');
info = dicominfo('I-131sphere10nisbah1.dcm');
%gp=info.SliceThickness;
spect=(squeeze(spect));%smooth3
aa=size(spect);aa=aa(3);
imshow3D(spect)
seedR1 = 58; seedC1 = 76; seedP1 = 45;%seedR1=row number, seedC1=column number, seedP1=slice number
W = graydiffweight(spect, seedC1, seedR1, seedP1, 'GrayDifferenceCutoff', 1000000000);
for thresh =0.0001:0.0001:0.9999
[BW, D] = imsegfmm (W, seedC1, seedR1, seedP1, thresh);
T = regionprops3( BW,'Volume');
if T==26.67
then gray_weight = thresh;
volume = T;
break
end
end
ERROR
Conversion to logical from table is not possible.
0 个评论
回答(2 个)
Walter Roberson
2024-12-18,6:07
T = regionprops3( BW,'Volume');
if T==26.67
You are attempting to compare the table() object to the floating point constant 26.67 . Comparing a complete table to a floating point object is not a defined operation.
You have made the mistake of assuming that when only a single property is explicitly requested from regionprops3(), that the result is numeric. regionprops3() always returns a table() object.
You need something more like
if T.Volume == 26.67
However: it is nearly always a mistake to compare two floating point values for exact equality. Two values that are calculated by slightly different methods can end up being different in their last couple of bits.
26.67 looks suspiciously like 26 + (2/3) . A value that was calculated as 26 + (2/3) would work out to be approximately 26.666666666666667850904559600166976451873779296875 . That will not compare equal to 26.67
then gray_weight = thresh;
MATLAB does not have a "then" keyword. Attempting to execute "then" will result in toolbox/matlab/connector2/common/+connector/+internal/Future.p being executed, if anything.
I suspect that your entire approach is wrong. I think you should be using something like fminsearch or fzero searching over thresh
thresh = fzero(@(thesh)find_volume(W, seedC1, seedR1, seedP1, thresh)-26.67, [0.0001,0.9999] )
%...
function volume = find_volume(W, seedC1, seedR1, seedP1, thresh)
[BW, D] = imsegfmm (W, seedC1, seedR1, seedP1, thresh);
T = regionprops3( BW,'Volume');
volume = T.Volume;
end
3 个评论
Steven Lord
2024-12-18,14:41
In this case, don't call fzero with a 2-element vector as the second input argument. If you do that, fzero checks that the sign of your function evaluated at the locations in that vector are different (those two values bracketing at least one root) and throws an error if the signs are not different. Instead specify one number, a starting guess, and let fzero search for an interval where the endpoints differ in sign. Or, since the interval you've specified doesn't necessarily bracket a root, find an interval that does.
But before you do that, you might want to use the fplot function to help you find that interval bracketing the root or a good starting guess, one somewhat near the root.
Walter Roberson
2024-12-18,19:47
The two-element vector is needed to keep fzero from probing negative thresh values.
I am a bit puzzled as to why the segmented volume would be the same sign in both cases... unless, that is, the threshold used does not make any difference to the calculation.
Jayanti
2024-12-18,6:15
Hi,
The error you are encountering is due to "regionprops3" function. The "regionprops3" function returns a table. In the line
if T==26.67
the code attempts to compare an entire table "T", returned by the "regionprops3" function, to the number 26.67. To resolve this, access the specific property from the table returned by "regionprops3".
Please refer to the modified code below to resolve the issue.
for thresh = 0.0001:0.0001:0.9999
[BW, D] = imsegfmm(W, seedC1, seedR1, seedP1, thresh);
T = regionprops3(BW, 'Volume');
if ~isempty(T)
volume = T.Volume;
if any(volume == 26.67)
gray_weight = thresh;
break;
end
end
end
Also please refer to the documentation link of “regionprops3” for your reference.
3 个评论
Stephen23
2024-12-18,10:40
编辑:Stephen23
2024-12-18,11:09
"I used your syntax. but why the thresh value is 0.9990 in workspace?"
Because your code does not take into account binary floating point error.
In short, your testing of exact equivalence of two binary floating point numbers like this:
volume == 26.67
will (in general) not work. Your code is therefore buggy. The loop condition is never met and so BREAK is never called and so the loop will iterate until the final THRESH value. Which is 0.9990.
The solution is to get rid of EQ and then pick one of these:
- compare the absolute difference against a tolerance: abs(A-B)<tol
- use ISAPPROX (Walter Roberson already proposed this earlier)
- use ISMEMBERTOL
- ...etc
However, rather than using a loop like this you should use FZERO or similar.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!