MATLAB: How can I access to pixels of objects produced by connected componenets function

connected components

when I use built-in function "bwconncomp" in matlab, I don't know which objects black and white it is produced as an target objects of this function? Also, I need to access pixels of these objects to remove some of them.

Best Answer

bwconncomp only returns the indices of the non-zero pixels.
bwconncomp returns a structure. The PixelIdxList field of the structure is a cell array. Each element of the cell array is a vector of linear indices. You can use ind2sub() if you want to turn those linear indices into row and column subscripts.
Example:
bw = rand(20,30) > 0.3; %sample data
CC = bwconncomp(bw);
idxlists = CC.PixelIDxList;
first_component_idx = idxlists{1};
%now some manipulation
%notice we did not use bw(R(1:2), C(1:2)) as that would usually access 2 x 2 pixels
[R, C] = ind2sub(size(bw), first_components_idx);
bw(R(1), C(1)) = 0;
bw(R(2), C(2)) = 0;
%alternate manipulation equivalent to the above
bw(first_components_idx(1:2)) = 0;