Object labeling using 8 connectivity
10 ビュー (過去 30 日間)
古いコメントを表示
I'm trying to write a function that takes in a binary image and assign each object (connected component with 8-connectivity) a unique label. For example, the left image would be turned into the right one.
I was able to identify the labels uniquely but can't get them to increase in size as shown. Here's my code so far:
L = [0 0 0 0 0 0 0 0;
0 1 1 0 0 0 0 0;
0 1 0 0 0 1 1 0;
0 0 0 0 0 0 1 0;
0 0 0 0 0 0 0 0;
0 0 0 0 1 1 1 0;
0 0 1 1 0 1 1 0
0 0 0 0 0 0 0 0];
labelImage= zeros(size(L,1), size(L,2));
Current_Label = 1;
for i = 1:size(L,1) %Rastering over the rows
for j = 1:size(L,2) %Rastering over the columns
if L(i,j) == 1 %Finding where pixel value = 1 resides in our image
if L(i-1,j-1) == 1 || L(i-1,j) == 1 || L(i-1,j+1) == 1 || L(i,j-1) == 1 || L(i,j+1) == 1 ...
|| L(i+1,j-1) == 1 || L(i+1,j) == 1 || L(i+1,j+1) == 1
labelImage(i,j) = Current_Label;
if labelImage(i-1,j-1) == Current_Label || labelImage(i-1,j) == Current_Label || ...
labelImage(i-1,j+1) == Current_Label || labelImage(i,j-1) == Current_Label ...
|| labelImage(i,j+1) == Current_Label || labelImage(i+1,j-1) == Current_Label || ...
labelImage(i+1,j) == Current_Label || labelImage(i+1,j-1) == Current_Label
labelImage(i,j) = Current_Label;
disp('Im here')
else
Current_Label = Current_Label + 1;
disp('Im here now')
end
end
if labelImage(i,j) ~= (labelImage(i-1,j-1) == Current_Label || labelImage(i-1,j) == Current_Label || ...
labelImage(i-1,j+1) == Current_Label || labelImage(i,j-1) == Current_Label ...
|| labelImage(i,j+1) == Current_Label || labelImage(i+1,j-1) == Current_Label || ...
labelImage(i+1,j) == Current_Label || labelImage(i+1,j-1) == Current_Label)
labelImage(i,j) = Current_Label;
end
end
end
end
Any help would be greatly appreciated, thanks!
0 件のコメント
回答 (1 件)
Image Analyst
2021 年 6 月 28 日
Don't try to rewrite the built-in functions with your own. Simply use the built in functions bwlabel() or bwconncomp().
2 件のコメント
michael bowen
2021 年 12 月 12 日
alot of people are asking help because we are required to make code with out the built in funtions for classes but teachers who dont teach nor do they help us so we ask on the mathlab forums and get unhelpful answers from you. This is the 10 post of someone asking for help that could have helped me with the same problem and you were unhelpful and kinda rude
Image Analyst
2021 年 12 月 12 日
@michael bowen the person did not say it was homework, and for good reason (if it was). If I had done the assignment for them, or you, and they/you were to turn in my code as your own, then they/you would get into serious trouble for cheating and could face expulsion or serious disciplinary action. I think it's rude of you to ask me to do people's homework for them. In general I only give them hints, not full solutions (IF they say it's homework which this post did not) so as to not get them in trouble for cheating or refer them to our FAQ:
Also sometimes people ask how to do something and give their own code not realizing that there is a built-in function that already does that, so that's why I referred @John Doe to the built in functions.
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!