Extract pixels inside the area between 2 edge lines
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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!
2 Commenti
Risposta accettata
Matt J
il 5 Giu 2023
Modificato: Matt J
il 5 Giu 2023
[m,n]=size(I);
mask=poly2mask(POLYOUT.Vertices(:,2),POLYOUT.Vertices(:,1), m,n);
pixels=I(mask)
Più risposte (1)
Diwakar Diwakar
il 5 Giu 2023
% 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
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!