Extract pixels inside the area between 2 edge lines
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
Matt J
2023년 6월 5일
편집: Matt J
2023년 6월 5일
[m,n]=size(I);
mask=poly2mask(POLYOUT.Vertices(:,2),POLYOUT.Vertices(:,1), m,n);
pixels=I(mask)
추가 답변 (1개)
Diwakar Diwakar
2023년 6월 5일
% 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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!