如何修改MATLAB连通域标记代码,清除超尺寸亮像素簇?
How to Filter Out Large Connected Clusters in Your MATLAB Labeling Code
Got it, let's break down how to modify your code to remove over-sized bright pixel clusters. Your current code already does solid 8-connected component labeling—we just need to add a few steps to count cluster sizes and zero out the ones that exceed your threshold.
Key Modification Steps
After your existing labeling loop finishes assigning IDs to all clusters, we'll:
- Count how many pixels belong to each unique cluster ID
- Identify which clusters are larger than your specified size (e.g., 12 pixels)
- Set all pixels in those large clusters back to 0 in your output matrix
B
Modified Full Code
visited = false(size(binary_image)); % Track which pixels have been processed [rows, cols] = size(binary_image); B = zeros(rows, cols); % Output matrix to store cluster IDs ID_counter = 1; % Keep track of unique cluster IDs % Original connected component labeling loop for row = 1:rows for col = 1:cols if binary_image(row, col) == 0 visited(row, col) = true; % Mark dark pixels as visited immediately elseif visited(row, col) continue; % Skip pixels we've already processed else stack = [row col]; % Initialize stack with the current bright pixel while ~isempty(stack) loc = stack(1,:); stack(1,:) = []; % Remove the top element from the stack if visited(loc(1),loc(2)) continue; end visited(loc(1),loc(2)) = true; B(loc(1),loc(2)) = ID_counter; % Assign current ID to this pixel % Check all 8 neighboring pixels [locs_y, locs_x] = meshgrid(loc(2)-1:loc(2)+1, loc(1)-1:loc(1)+1); locs_y = locs_y(:); locs_x = locs_x(:); % Filter out pixels that are outside the image bounds out_of_bounds = locs_x < 1 | locs_x > rows | locs_y < 1 | locs_y > cols; locs_y(out_of_bounds) = []; locs_x(out_of_bounds) = []; % Filter out already visited pixels is_visited = visited(sub2ind([rows cols], locs_x, locs_y)); locs_y(is_visited) = []; locs_x(is_visited) = []; % Filter out dark pixels (0s) is_1 = binary_image(sub2ind([rows cols], locs_x, locs_y)); locs_y(~is_1) = []; locs_x(~is_1) = []; % Add valid bright neighbors to the stack stack = [stack; [locs_x locs_y]]; end ID_counter = ID_counter + 1; % Increment ID for the next cluster end end end % -------------------- New code to filter large clusters -------------------- max_cluster_size = 12; % Set your desired size threshold here % Count the number of pixels in each cluster cluster_sizes = accumarray(B(:), 1); % cluster_sizes(n) will hold the pixel count for cluster ID = n % Find all cluster IDs that exceed the size threshold large_cluster_ids = find(cluster_sizes > max_cluster_size); % Zero out all pixels belonging to over-sized clusters for id = large_cluster_ids B(B == id) = 0; end
Optional: Re-label Clusters for Continuous IDs
If you want the remaining clusters to have consecutive IDs (no gaps from removed clusters), you can add this final step:
% Reassign IDs to be sequential (e.g., IDs 1,3 become 1,2) [B, ~] = labelmatrix(labelmatrix(B));
This uses MATLAB's built-in labelmatrix to clean up the ID numbering.
Quick Explanation
accumarrayis a efficient way to count pixel counts per ID: it groups all pixels by their label and sums the 1s we pass in (each 1 represents one pixel).- The loop over
large_cluster_idsdirectly targets pixels in over-sized clusters and sets them back to 0, which exactly matches your requirement of converting those clusters to dark pixels.
内容的提问来源于stack exchange,提问作者user11459362
相关产品推荐
相关产品推荐

