how to delate some sparse pixels in a binary image

13 次查看(过去 30 天)
Hi everyone,
I have a binary image in which I have to detect or extract some pixels. I searched and I found some documentation that maybe help me like imclose or imfill but my image has very sparse pixels and the part that I should identify has not a regular shape or type. I am so confused about what can I do for that so I wanted to share with you to know your comments.
the orginal image and the image (rill erosion) in that I draw a red border to specify the wanted pixels is attached.
many thanks

回答(1 个)

Rahul
Rahul 2024-11-19,11:16
To identify and extract the desired pixels (regions outlined in red in the "rill erosion" image), here are some steps that you can follow:
Preprocessing
Sparse pixels in the given binary image can make it challenging to identify specific regions, hence we can improve the connectivity of pixels using the following operations:
  • Morphological operations like ‘imclose’ and ‘imopen’ are helpful in removing noise and closing contrast gaps.
  • Further, ‘imfill’ can be used to fill holes in darker regions of the resulting image.
Region Segmentation
  • After preprocessing, identify connected regions using ‘bwlabel’ or ‘bwconncomp’.
  • Extract specific regions based on size or shape.
Matching Desired Shapes
  • Since the approximate location and shapes of the regions are already known (from the red boundaries), you can use ‘regionprops’ to filter regions based on geometric properties like area, eccentricity, or bounding box.
Here’s how you can structure your code in MATLAB:
% Load images
original_img = imread('orginal.png'); % Load original image
target_img = imread('rill erosion.png'); % Load reference image with red borders
% Ensure the original image is grayscale
if size(original_img, 3) > 1
original_img = rgb2gray(original_img); % Convert RGB to grayscale
end
% Convert to binary
binary_img = imbinarize(original_img); % Ensure binary image
% Preprocessing (morphological operations to close gaps and fill holes)
se = strel('disk', 3); % Structuring element for morphological operations
processed_img = imclose(binary_img, se); % Close small gaps
processed_img = imfill(processed_img, 'holes'); % Fill holes in regions
% Identify connected components
CC = bwconncomp(processed_img); % Find connected components
stats = regionprops(CC, 'Area', 'BoundingBox', 'Eccentricity'); % Get properties
% Filter regions based on properties
min_area = 500; % Define minimum area threshold (adjust as needed)
desired_regions = find([stats.Area] > min_area); % Find regions meeting criteria
% Create a mask for the desired regions
mask = false(size(binary_img)); % Initialize an empty mask
for i = 1:length(desired_regions)
mask(CC.PixelIdxList{desired_regions(i)}) = true; % Add regions to mask
end
% Display results
figure;
subplot(1, 3, 1); imshow(binary_img); title('Original Binary Image');
subplot(1, 3, 2); imshow(processed_img); title('Processed Image');
subplot(1, 3, 3); imshow(mask); title('Detected Regions');
Adjust the size and shape of the ‘strel’ disk to suit the structure of the image, since a larger size can connect sparsely scattered pixels, but it may merge unwanted regions.
Using ‘regionprops’ can filter regions based on properties like area, eccentricity, or bounding box dimensions, while adjusting thresholds to include only the desired shapes.
If the reference (red-bordered image) is critical, extracted regions can be compared to the red regions using pixel-wise comparison or template matching.
To know more about the usage of various functions and parameters used in the above code, refer to the documentation links mentioned below:
Best!

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by