Extract pixels inside the area between 2 edge lines

3 visualizaciones (últimos 30 días)
Riccardo Rossi
Riccardo Rossi el 5 de Jun. de 2023
Editada: Matt J el 6 de Jun. de 2023
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 comentarios
Matt J
Matt J el 5 de Jun. de 2023
Editada: Matt J el 5 de Jun. de 2023
What is a polybuffer? Please show us the image and the input variables that you have, so we can better demonstrate solutions..
Riccardo Rossi
Riccardo Rossi el 5 de Jun. de 2023
Editada: Matt J el 5 de Jun. de 2023
Hi Matt,
I have uploaded the image I and the POLYOUT (with x and y coordinates). Basically I would like to select all the pixels of I which are enclosed inside the area of polyout., as you can easily see by running the following code:
imshow(I)
hold on;
plot(POLYOUT.Vertices(:,2),POLYOUT.Vertices(:,1),'g','LineWidth',3);

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 5 de Jun. de 2023
Editada: Matt J el 5 de Jun. de 2023
[m,n]=size(I);
mask=poly2mask(POLYOUT.Vertices(:,2),POLYOUT.Vertices(:,1), m,n);
pixels=I(mask)
  2 comentarios
Riccardo Rossi
Riccardo Rossi el 6 de Jun. de 2023
It is run, and how can i get the x y coordinates of "pixels" variable? Thank you so much again
Matt J
Matt J el 6 de Jun. de 2023
Editada: Matt J el 6 de Jun. de 2023
[y,x]=find(mask);
Once you have the mask, you can also get both together with,
stats = regionprops(mask,I, 'PixelList','PixelValues')

Iniciar sesión para comentar.

Más respuestas (1)

Diwakar Diwakar
Diwakar Diwakar el 5 de Jun. de 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

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by