Binary Image from 2D plot
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have wrote a code to generate an irregular shape. Now i want to convert this into a binary image in which all the points lying inside the shape will have a pixel value of 1 and outside will have 0 such that while using Imshow function the visual image show the shape in the form of black and white image. My ultimate aim is to generate random non overlapping irregular shapes, this is the first step towards that. The binary values will help me in overlap checking, as i will be scanning the pixel value and if they sum up to 2 then there is overlap i will discard that shape.
I want little bit of help for this approach.
% Your MATLAB code for generating the shape
N = 5 + round(rand() * 10);
a = sort(rand(N, 1)) * 2 * pi;
r = 8 + rand(N, 1) * 0.6;
% Generate points
x = cos(a) .* r;
y = sin(a) .* r;
t = linspace(1, N, 100000 * N);
xi = interp1(1:N, x, t, 'pchip');
yi = interp1(1:N, y, t, 'pchip');
% Close the shape by adding the first point to the end
xi = [xi, xi(1)];
yi = [yi, yi(1)];
% Plot the original shape
figure(1);
plot(xi, yi);
xlim([-15, 15])
ylim([-15, 15])
axis equal;
title('Irregular Shape');
0 comentarios
Respuestas (1)
DGM
el 8 de Dic. de 2023
Editada: DGM
el 8 de Dic. de 2023
One way would be to use poly2mask(), but bear in mind that your coordinate space changes. That may complicate the expense of testing points.
% Your MATLAB code for generating the shape
N = 5 + round(rand() * 10);
a = sort(rand(N, 1)) * 2 * pi;
r = 8 + rand(N, 1) * 0.6;
% Generate points
x = cos(a) .* r;
y = sin(a) .* r;
t = linspace(1, N, 100000 * N);
xi = interp1(1:N, x, t, 'pchip');
yi = interp1(1:N, y, t, 'pchip');
% Close the shape by adding the first point to the end
xi = [xi, xi(1)];
yi = [yi, yi(1)];
% Plot the original shape
figure(1);
plot(xi, yi);
xl = [-15, 15]; % need to know this later
yl = [-15, 15];
xlim(xl)
ylim(yl)
axis equal;
title('Irregular Shape');
% create an image of some size
% assume that the image edges correspond to the plot extents
% assume that the image should be rendered such that
% the object is not flipped
imsz = [501 501]; % image size [y x]
xmod = interp1(xl,[0.5 imsz(2)+0.5],xi); % rescale to image coordinates
ymod = interp1(yl,[0.5 imsz(1)+0.5],-yi);
mask = poly2mask(xmod,ymod,imsz(1),imsz(2));
% show the mask
figure
imshow(mask)
0 comentarios
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!