How to convert nearest pixel as same number of pixels value from gray image? like bwlabel?

6 次查看(过去 30 天)
How to convert nearest pixel as same number of pixels value from gray image? like bwlabel?

回答(1 个)

Charu
Charu 2025-5-5
编辑:Charu 2025-5-8
Hello Voxey,
According to my understanding of the question you want to label connected components in a grayscale image.
The function “bwlabel” works only for binary images, for grayscale images, you can use “bwlabeln” with logical conditions You can also use “bwconncomp” and “labelmatrix” functions in a loop for each unique value.
You can refer to the steps mentioned below to get label connected components in a grayscale image:
- Let I be the grayscale image.
I = [
1 1 2 2;
1 0 0 2;
3 3 0 2
];
- Find the unique values (excluding background e.g.:0)
vals = unique(I);
vals(vals == 0) = [];
% Remove background if needed
- Initialse label matrix
L = zeros(size(I));
label_count = 0;
- Loop through each value and label connected components.
L = zeros(size(I), 'double');
% Make sure L is double
label_count = 0;
for k = 1:length(vals)
mask = I == vals(k);
CC = bwconncomp(mask, 8);
% 8-connectivity for 2D images
temp_labels = double(labelmatrix(CC)); % Convert to double
temp_labels(temp_labels > 0) = temp_labels(temp_labels > 0) + label_count;
L(mask) = temp_labels(mask);
label_count = max(L(:));
end
L is the labelled image and each connected region of the same value has a unique label.
Here is the image generated on sample data, with unique label for grayscale:
For more information on the functions, kindly refer to the link of MathWorks documentation mentioned below:
-bwlabeln:
-bwconncomp:
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Modify Image Colors 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by