0

I am trying to implement code for the light speed labeling technique described in this article (I cannot use the Image Processing Toolbox): https://pdfs.semanticscholar.org/ef31/7c257603004d818ca1e2a2aa67d36d40147e.pdf (see section 2, page 7).

Here is my Matlab code for LSL equivalence construction (algorithm 14, step 2).

function [EQ,ERAi,nea] = LSL_equivalence(EQ,ERim1,RLCi,ERAim1,ERAi,NERi,nea,lImg)
    % LSL_EQUIVALENCE build the associative table between er and ea
    % GOAL: to create a Look Up Table to be applied to ERi to create EAi.

    for er = 1:2:NERi % check segments one by one
        % read the boundaries of each segment to obtain de relative labels of every agacent segment in the prev line
        j0 = RLCi(er); 
        j1 = RLCi(er+1);

        er0 = ERim1(j0+1); % label of the first segment
        er1 = ERim1(j1+1); % the label of the last segment

        % check label parity: segments are odd, background is even
        % bitand([1 2 3 4 5],1) == [1 0 1 0 1]
        if bitand(er0,1) == 0 % if er0 is even
            er0 = er0 + 1;
        end
        if bitand(er1,1) == 0 % if er1 is even
            er1 = er1 -1;
        end
        if er1 >= er0 % if there is an adjacency
            ea = ERAim1(er0+1); % absolute label of the first segment 
            a = EQ(ea+1);     % a is the ancestor (smallest label of the equivalence class) 
                for erk = (er0+2):2:er1
                    eak = ERAim1(erk+1);
                    ak = EQ(eak+1);
                    % min extraction and propagation
                    if a < ak
                        EQ(eak+1) = a;
                    else
                        a = ak;
                        EQ(ea+1) = a;
                        ea = eak;
                    end
                end
            ERAi(er+1) = a; % the global min of all ak ancestors 
        else % if there are no adjacent labels make a new label
            nea = nea + 1;
            ERAi(er+1) = nea;
        end
    end
    end

I am having some trouble with indexes, as the pseudo code described in the article has indexes starting with 0 and Matlab works with 1. I have already found some C++ code in this Stack Overflow post Implementing LSL for Connected Component Labeling/Blob Extraction (I applied suggested changes) and also in this git repo https://github.com/prittt/YACCLAB/blob/master/include/labeling_lacassagne_2016_code.inc. I fail to see the differences.

Also, I'm having some trouble understanding what an equivalence class is (which is what goes in matrix EQ). Thanks ahead of time!

Fennec
  • 13
  • 4
  • You're better off implementing a simple and straight-forward union-find labeling algorithm. It's much simpler to write and also likely to run faster in MATLAB (it's not a compiled language). Here is a good paper describing labeling algorithms: http://ieeexplore.ieee.org/document/7900112/ -- the code that comes with the paper is here: https://github.com/prittt/YACCLAB (but you already found that repository, I see) – Cris Luengo Mar 11 '18 at 14:21
  • Thanks, I'll look into it! My problem is using this code in both Simulink and Matlab. For others interested, this code runs about two times faster than this https://en.wikipedia.org/wiki/Connected-component_labeling#One-pass_version single pass algorithm in Matlab. – Fennec Mar 11 '18 at 17:40
  • Union-find is a two-pass algorithm, similar to what you implemented but simpler and probably a little faster. Don't expect huge time gains, though. If you need performance you'll have to implement in a MEX-file. One option then could be to get the newest version of DIPimage (https://github.com/DIPlib/diplib/), if you're not afraid of compiling stuff yourself (the released version of DIPimage at http://www.diplib.org implements a less efficient algorithm). – Cris Luengo Mar 11 '18 at 18:56

1 Answers1

-2

I realize this is coming a bit late, but I just put up a piece of code that is similar to what the light speed labeling algorithm does. I'll give a brief description of how I solved the index problem.

The first step of LSL is take each column of pixels and finds the start and stop positions of consecutively labeled pixels. You can do that in Matlab like this:

I = imread('text.png');
[rows,cols] = find(xor(I(2:end,:),I(1:end-1,:)));

What this gives you is the row and column of the start and stop position of each run of pixels in a column, except its non-inclusive indexing. By non-inclusive indexing I mean the indices of pixels runs from I(r(2*n-1),c(2*n-1)) to I(r(2*n)-1,c(2*n)) for each pixel run (n). You should note that the paper operates along rows where the above code operates along columns, but the same principle applies. You should also note that the above code does not cover the circumstance of labeled pixels on the edge of the image.

If you want to see a complete implementation, I posted my code on the Matlab File Exchange. I don't claim that it copies LSL exactly, but it works on many of the same principles.

https://www.mathworks.com/matlabcentral/fileexchange/70323-ftllabel?s_tid=prof_contriblnk

Nick
  • 21
  • 3
  • Not sure why there are down votes. The question is about how to do the indexing in Matlab, and my answer explains one way it is done. If this does not answer the question, please clarify what exactly is being asked. – Nick Mar 26 '19 at 00:35