Urgent help with connected component labeling

2 views (last 30 days)
Steve
Steve on 21 May 2012
Hello Experts,
I need some urgent help with matlab code, I have an exam in app. 2 hours and I need to write a function that gets a binary picture (matrix) and returns a matrix with the same size in which each connected componnent is tagged with a different label.
Please be so kind and provide me the code, it's very urgent.
  2 Comments
Geoff
Geoff on 21 May 2012
You haven't been posting responses in your last question ever since you emailed me asking for private mentoring. If you have specific problems, whether they be syntactic, logical or conceptual, please describe them, and we may be able to help you. I do wonder how long you've known about this exam, and how much time you set aside to prepare, given that it appears you are having difficulty with this particular subject. Have you implemented a Union Find algorithm yet? Or did you have another method for relating the pixels?
Walter Roberson
Walter Roberson on 21 May 2012
http://www.mathworks.com/matlabcentral/answers/29922-why-your-question-is-not-urgent-or-an-emergency

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 21 May 2012
And what was wrong with the algorithm that kind Geoff described for you in your duplicate question? Did you want him to post his code so that you could turn his code in as your own for your test?
  2 Comments
Steve
Steve on 21 May 2012
Dear Image Analyst,
I really need your help, and yes I need the code because I am complete newbie in matlab and you can save me on this exam.
Here is what I have done so far, but without any success please help me with the code...
function [newImg] = tagConnectedComponents(img)
newImg = zeros(size(img));
label = 1;
eq =
for i = 1:size(img,1)
for j = 1:size(img,2)
if img(i,j) == 1
if i>size(img,1) && j>size(img,2)
neighbors = [img(i-1,j), img(i,j-1)];
if max(neighbors) == 0 % neighbors is empty
newImg(i,j) = label;
label = label + 1;
end
end
end
end
end
end
Geoff
Geoff on 21 May 2012
Hang on a minute.... An exam is designed to test YOUR ability, not ours!
I can point out a logic error right now though... "i" and j" will NEVER be greater than size(img,1) and size(img,2) respectively. You want to test if i > 1 or j > 1 here, but you need to read my answer to your other question again and/or think about exactly what that code does and when it does it.
But your concept is not going to work. All it does is individually labels all pixels that have a neighbour on the left or above. It doesn't actually connect the sets.
What you actually need to do here is implement Union Find and do this:
* if neighbour on left, union thispixel with leftpixel
* if neighbour above, union thispixel with abovepixel
It might be a little late to implement union find... But if you want to cheat, you can probably just google "union find code for matlab". In fact, Google solves about 90% of questions that are asked here.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!