Extract pixels inside the area between 2 edge lines
Afficher commentaires plus anciens
Hello,
I would like to detect all the pixels of an BW image which are enclosed into the area of a polybuffer. About the polybuffer, I have got the x and y coordinates of the 2 edge lines whcih constitute the polybuffer itself. How can I do it?
Thank you so much for your help!
Réponse acceptée
Plus de réponses (1)
% Create a black and white image
image = zeros(500, 500);
image(200:300, 200:300) = 1; % Add a square shape
% Display the original image
subplot(1, 2, 1);
imshow(image);
title('Original Image');
% Coordinates of the edge lines
edge_line1 = [100, 100; 200, 100; 200, 200; 100, 200];
edge_line2 = [150, 150; 250, 150; 250, 250; 150, 250];
% Detect enclosed pixels
enclosed_pixels = detect_enclosed_pixels(image, edge_line1, edge_line2);
% Display the enclosed pixels
subplot(1, 2, 2);
imshow(enclosed_pixels);
title('Enclosed Pixels');
function enclosed_pixels = detect_enclosed_pixels(image, edge_line1, edge_line2)
% Create binary mask
mask = false(size(image));
% Define the polygon points using edge lines
polygon_points = [edge_line1; flip(edge_line2, 1)];
% Get the minimum and maximum coordinates for the polygon
min_x = min(polygon_points(:, 1));
max_x = max(polygon_points(:, 1));
min_y = min(polygon_points(:, 2));
max_y = max(polygon_points(:, 2));
% Create a binary mask for the polygon region
polygon_mask = poly2mask(polygon_points(:, 1), polygon_points(:, 2), size(image, 1), size(image, 2));
% Extract the region of interest using the polygon mask
roi = image(min_y:max_y, min_x:max_x);
% Apply the polygon mask to the region of interest
roi_masked = roi & polygon_mask(min_y:max_y, min_x:max_x);
% Create a new binary mask with the enclosed region
enclosed_pixels = false(size(image));
enclosed_pixels(min_y:max_y, min_x:max_x) = roi_masked;
end
Catégories
En savoir plus sur Object Analysis dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
