Converting Cartesian Coordinates to Binary image

Hello
I have two vectors of X and Y cartesian values in +/-, how to convert them to a binary image of 0's and 1's
Thank you.
I attached my plot.

2 件のコメント

Aditya Verma
Aditya Verma 2020 年 6 月 14 日
Hi, Could you specify how you want to map X and Y coordinates to the binary image. If I understand it correctly you want to create a 2-D matrix with binary values (Binary Image).
Mohanad Alkhodari
Mohanad Alkhodari 2020 年 6 月 14 日
Yes, I have the values as X and Y cartesian coordinates.
I want to convert them to pixel values. Like a 2D binary image where a 1 is located on the each location (X,Y).

サインインしてコメントする。

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 6 月 14 日

2 投票

Something like this
x = randn(1, 5000); % generate random points
y = randn(1, 5000);
img_dimension = [500 500]; % [width height]
img = zeros(img_dimension);
xt = floor(rescale(x, 1, img_dimension(2))); % convert x-y in range of image coordinates
yt = floor(rescale(y, 1, img_dimension(1)));
idx = sub2ind(img_dimension, yt, xt);
img(idx) = 1;
imshow(img)

10 件のコメント

Mohanad Alkhodari
Mohanad Alkhodari 2020 年 6 月 14 日
The result of this is points, is there a way to connect those points to form the shape?
Ameer Hamza
Ameer Hamza 2020 年 6 月 14 日
If you have computer vision toolbox, you can use insertShape()
x = randn(1, 100); % generate random points
y = randn(1, 100);
img_dimension = [500 500]; % [width height]
img = zeros(img_dimension);
xt = floor(rescale(x, 1, img_dimension(2))); % convert x-y in range of image coordinates
yt = floor(rescale(y, 1, img_dimension(1)));
X = zeros(1, numel(xt)*2);
X(1:2:end) = xt;
X(2:2:end) = yt;
img = insertShape(img, 'Line', X, 'Color', 'w');
imshow(img)
Aditya Verma
Aditya Verma 2020 年 6 月 14 日
If you're just interested in getting the final image, you can use the plot() function itself:
x = randn(1, 100);
y = randn(1, 100);
plot(x, y, '-w');
set(gca,'Color','k');
set(gca, 'YTickLabel', []);
set(gca, 'XTickLabel', []);
Mohanad Alkhodari
Mohanad Alkhodari 2020 年 6 月 14 日
I added the lines by adding more points to my X_edit and Y_edit vectors that correspond to the X-Y cartesian points. This is the code now:
%%% More points in between
X_new = double.empty;
for i = 2:length(X_edit)
x=linspace(X_edit(i-1),X_edit(i),500);
X_new = [X_new;x'];
end
Y_new = double.empty;
for i = 2:length(Y_edit)
y=linspace(Y_edit(i-1),Y_edit(i),500);
Y_new = [Y_new;y'];
end
%%% Transforming it to binary
img_dimension = [227 227]; % [width height]
img = zeros(img_dimension);
xt = floor(rescale(X_new, 1, img_dimension(2))); % convert x-y in range of image coordinates
yt = floor(rescale(Y_new, 1, img_dimension(1)));
idx = sub2ind(img_dimension, yt, xt);
img(idx) = 1;
Mohanad Alkhodari
Mohanad Alkhodari 2020 年 6 月 14 日
Many Thanks. Accepted as Answer!
Ameer Hamza
Ameer Hamza 2020 年 6 月 14 日
I am glad to be of help!
Following code shows how to add more points without using for-loops and dynamic memory allocation (will be faster then the above code if x and y vectors are very large)
x = randn(1, 100); % generate random points
y = randn(1, 100);
xv = interp1(linspace(0,1,numel(x)), x, linspace(0,1,numel(x)*1000));
yv = interp1(linspace(0,1,numel(y)), y, linspace(0,1,numel(y)*1000));
img_dimension = [500 500]; % [width height]
img = zeros(img_dimension);
xt = floor(rescale(xv, 1, img_dimension(2))); % convert x-y in range of image coordinates
yt = floor(rescale(yv, 1, img_dimension(1)));
idx = sub2ind(img_dimension, yt, xt);
img(idx) = 1;
imshow(img)
Mohanad Alkhodari
Mohanad Alkhodari 2020 年 6 月 21 日
Hello
I have a small question.
I have two figures of circle plots (1 is half size of the other) that I want to convert each into binary images, but I want to keep the scale the same.
I mean, the code generate each image between for example 500x500. Do both binary images will look the same at the end right? how to fix this to keep the scale when interpolating between cartesian and pixels?
Could you help please?
Ameer Hamza
Ameer Hamza 2020 年 6 月 21 日
I don't clearly understand what the required output is. Can you explain it with an example? Also, you can use imresize() to change the size of an image.
Mohanad Alkhodari
Mohanad Alkhodari 2020 年 6 月 21 日
編集済み: Mohanad Alkhodari 2020 年 6 月 21 日
A circle of radius 0.8 covers more space than a circle of radius 0.2 when converting them from cartesian to binary.
With the above code, both circles are converted for a 500x500 for example and have the same size.
How can I convert to binary while preserving the scaling differences?
Ameer Hamza
Ameer Hamza 2020 年 6 月 21 日
You can make the circle small and large by specifying the range of rescale() function
xt = floor(rescale(x, 100, img_dimension(2)-100)); % convert x-y in range of image coordinates
yt = floor(rescale(y, 100, img_dimension(1)-100));
create a circle between 100 to 400 pixels, i.e., leave 100 pixels on all sides unoccupied.

サインインしてコメントする。

その他の回答 (1 件)

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020 年 6 月 14 日

0 投票

This is an general example you could use:
Cartesian = [ 10,1;
15,10];
imSize=20;
binaryImage= false(imSize);
Cartesian(:,2) = imSize-(Cartesian(:,2)-1); % Comment if you want zero at the top-left corner
for idx=1:size(Cartesian,1)
binaryImage(Cartesian(idx,2),Cartesian(idx,1)) = true; % Note that X-Y index are inverted in indexing
end
figure,imshow(binaryImage)

Community Treasure Hunt

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

Start Hunting!

Translated by