Converting Cartesian Coordinates to Binary image

조회 수: 41 (최근 30일)
Mohanad Alkhodari
Mohanad Alkhodari 2020년 6월 14일
댓글: Ameer Hamza 2020년 6월 21일
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일
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월 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일
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)

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by