cc=bwconncomp return empty PixelIdxList
1 view (last 30 days)
Show older comments
I'm working on a alpha matting project that reqires to calculating connectivity error of predicted alpha matte and ground truth alpha matte. However after input the image my code get error indicating the cc.cc.PixelIdxList has a row of empty cell array, which alwasy stop the code from runing? how can i fix it? i did conve
rt the image to grayscale first.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/395483/image.png)
Thanks
% pred: the predicted alpha matte
% target: the ground truth alpha matte
% trimap: the given trimap
% step = 0.1
function loss = compute_connectivity_error(pred,target,trimap,step)
pred = single(pred)/255;
target = single(target)/255;
%320,320
[dimy,dimx] = size(pred);
thresh_steps = 0:step:1;
%l_map is -1 matix with same shape as pred
l_map = ones(size(pred))*(-1);
dist_maps = zeros([dimy,dimx,numel(thresh_steps)]);
% ii=[0,0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0]
for ii = 2:numel(thresh_steps)
pred_alpha_thresh = pred>=thresh_steps(ii);
target_alpha_thresh = target>=thresh_steps(ii);
%%% error from here
cc = bwconncomp(pred_alpha_thresh & target_alpha_thresh,4);
cc.PixelIdxList
size_vec = cellfun(@numel,cc.PixelIdxList);
[biggest,max_id] = max(size_vec);
omega = zeros([dimy,dimx]);
omega(cc.PixelIdxList{max_id}) = 1;
flag = l_map==-1 & omega==0;
l_map(flag==1) = thresh_steps(ii-1);
dist_maps(:,:,ii) = bwdist(omega);
dist_maps(:,:,ii) = dist_maps(:,:,ii) / max(max(dist_maps(:,:,ii)));
end
l_map(l_map==-1) = 1;
pred_d = pred - l_map;
target_d = target - l_map;
pred_phi = 1 - pred_d .* single(pred_d>=0.15);
target_phi = 1 - target_d .* single(target_d>=0.15);
loss = sum(sum(abs(pred_phi - target_phi).*single(trimap==128)));
0 Comments
Answers (1)
Nagasai Bharat
on 25 Nov 2020
Hi,
The error arises from the cc.PixelIdxList which is here a empty cell array. cc.PicelIdxList is an empty cell array due to the reason that the binay image(pred_alpha_thresh & target_alpha_thresh) sent into the bwconncomp function is a completely black image(all zeros) which has no connected components.
Cross check the pred_alpha_thresh and target_alpha_thresh values and ensure the output is not all zeros.
The following code would show you the case.
img = zeros([256, 256]);
cc = bwconncomp(img,4);
cc.PixelIdxList
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!