I have a set of blobs in a circular pattern, that is, they all are lined up in a circular pattern and I need to label them according the circle starting from the bottom of the circle. The way bwlabel works makes it very difficult to see an easy way to do this, the far left blob on the circle is one and the blob on the far right of the circle is the last label 8. Anyone have any ideas or have done this before? I have an idea to find the center of the circle and use angles to determine each blobs relative position, but then I'm pretty lost. Thank you!!
So a full example:
% Synthetic image I = imread('coins.png'); BW = imfill(I > 100,'holes');
%% Make the circular matrix szBW = size(BW); C = zeros(szBW); for ii = 1:ceil(size(BW,1)./2) C(ii:(end-(ii-1)),ii:(end-(ii-1))) = circlemat(szBW-2*(ii-1))+max(C(:)); end
%% Connect components CC = bwconncomp(BW); [~,order] = sort(cellfun(@(x)min(C(x)),CC.PixelIdxList)); %find order in C CC.PixelIdxList = CC.PixelIdxList(order); %reorder LL = labelmatrix(CC); %make label matrix imshow(label2rgb(LL,jet(CC.NumObjects+1))) %view it
Where circlemat is:
function C = circlemat(szC)
%Default if ~nargin szC = [10 10]; end
%Initial conditions and sizes C = zeros(szC); szhalf = floor(szC./2);
%Seed the first position rr = 1; cc = 1; C(1) = 1;
%Quadrants QQ = [1 2;3 4];
for ii = 2:(sum(szC)*2-4) %number of elements %Determine quadrant Q = QQ((rr > szhalf(1))+1, (cc > szhalf(2))+1);
switch Q case 1 %Upper left if cc == 1 rr = rr+1; else cc = cc-1; end case 2 %Lower left if rr == 1 cc = cc-1'; else rr = rr-1; end case 3 %lower right if rr == szC(1) cc = cc+1; else rr = rr+1; end case 4 %upper right if cc == szC(2) rr = rr-1; else cc = cc+1; end end C(rr,cc) = ii;
Where did you post your image? I imagine you'd have to segment your blobs and get their centroids. Then renumber your blobs according to your own custom algorithm, perhaps based on the angle from the center of the image or something. Then you can do a relabeling really fast by using intlut and your original labeled image along with your renumbered list. Then, you can either sort your measurements from regionprops the same way, or simply start all over again by calling regionprops with your newly labeled image.