Pytorch binary tensor

Mickey_Han: <p>Thank you very much for reading my question . A newbie here sorry if my question isnt asked quite right<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/3/a3ebbc9cb545c266585426d45e1a3d5673546efe.png" data-download-href="https://discuss.pytorch.org/uploads/default/a3ebbc9cb545c266585426d45e1a3d5673546efe" title="circle"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/3/a3ebbc9cb545c266585426d45e1a3d5673546efe_2_345x169.png" alt="circle" data-base62-sha1="no6TgrIoaHupn0UkiyQCpgYpF0y" width="345" height="169" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/3/a3ebbc9cb545c266585426d45e1a3d5673546efe_2_345x169.png, https://discuss.pytorch.org/uploads/default/optimized/3X/a/3/a3ebbc9cb545c266585426d45e1a3d5673546efe_2_517x253.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/3/a3ebbc9cb545c266585426d45e1a3d5673546efe_2_690x338.png 2x" data-dominant-color="F0F0F0"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">circle</span><span class="informations">1746×856 1.64 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>for example I have this image and want to convert it to binary tensor that only has 1s and 0s, I was just using</p>
<pre><code class="lang-auto">import torch
import numpy as np 
from PIL import Image
from torchvision import transforms
import torchvision.transforms.functional as fn

cc = Image.open("C:/Users/T/Desktop/circle.png")
cc = fn.resize(cc, size=[300,200])

tt = transforms.ToTensor()
gg = transforms.Grayscale()
circle_tensor = gg(tt(cc))
circle_np = circle_tensor[0].numpy()
pd.DataFrame(circle_np).to_csv("C:/Users/T/Desktop/circle.csv")
</code></pre>
<p>and it becomes a tensor that has only 1 representing white and 0 representing black.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/5/358e8c671d7659f632face98cc88115e55e645e7.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/358e8c671d7659f632face98cc88115e55e645e7" title="circle tensor.PNG"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/5/358e8c671d7659f632face98cc88115e55e645e7_2_345x171.jpeg" alt="circle tensor.PNG" data-base62-sha1="7DMIiPsWRXdHtXT49gThGAyVtYj" width="345" height="171" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/5/358e8c671d7659f632face98cc88115e55e645e7_2_345x171.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/3/5/358e8c671d7659f632face98cc88115e55e645e7_2_517x256.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/5/358e8c671d7659f632face98cc88115e55e645e7_2_690x342.jpeg 2x" data-dominant-color="BCBCBC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">circle tensor.PNG</span><span class="informations">1920×957 556 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>here is another example of a line ,</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/6/e61bb442083f0b4b45c3aa12c8dc9589173975c8.png" data-download-href="https://discuss.pytorch.org/uploads/default/e61bb442083f0b4b45c3aa12c8dc9589173975c8" title="line"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/6/e61bb442083f0b4b45c3aa12c8dc9589173975c8_2_345x169.png" alt="line" data-base62-sha1="wPDaMZ4vWedtu1piuICUbaK25Xq" width="345" height="169" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/6/e61bb442083f0b4b45c3aa12c8dc9589173975c8_2_345x169.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/6/e61bb442083f0b4b45c3aa12c8dc9589173975c8_2_517x253.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/e/6/e61bb442083f0b4b45c3aa12c8dc9589173975c8_2_690x338.png 2x" data-dominant-color="FEFEFE"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">line</span><span class="informations">1746×856 2.71 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/d/2d60e9e75569e9bf9b62e4d07f9c1bd4f664249a.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/2d60e9e75569e9bf9b62e4d07f9c1bd4f664249a" title="line tensor.PNG"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/d/2d60e9e75569e9bf9b62e4d07f9c1bd4f664249a_2_345x171.jpeg" alt="line tensor.PNG" data-base62-sha1="6tr7BZ84S4ZLDEIwoqdND9I94VA" width="345" height="171" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/d/2d60e9e75569e9bf9b62e4d07f9c1bd4f664249a_2_345x171.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/2/d/2d60e9e75569e9bf9b62e4d07f9c1bd4f664249a_2_517x256.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/2/d/2d60e9e75569e9bf9b62e4d07f9c1bd4f664249a_2_690x342.jpeg 2x" data-dominant-color="B9B9B9"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">line tensor.PNG</span><span class="informations">1920×954 577 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p><code>I was just wondering , instead of converting an image into a tensor ,</code></p>
<p><code>if I knew the geometric coordinates of the shape in the image . ie , the center of the circle and its radius , and for the line , the two ends points [x,y], [x1,y1] coordinates . can I create this binary tensor directly within pytorch. </code></p>
<p>ie through something like a for loop or more efficient method</p>
<pre><code class="lang-auto">zz = torch.zeros(300,200)

for x in range(zz.shape[0]):
    ...

and some for loop ? at the moment Im using drawing it as image first and convert in this way which doesnt feel quite right in my specific situation where I know the coordinates, 
</code></pre>
<p>Thank you very much for your time in reading and helping out.!</p>

ptrblck: <p>You could certainly reimplement these drawing functions, but if you don’t need to differentiate through them I would recommend to use already available and tested libraries such as OpenCV.</p>
<p>Here is a small example of drawing a circle and a line into a numpy array using OpenCV and then transforming this image to a tensor:</p>
<pre data-code-wrap="python"><code class="lang-python">import cv2
import torch
import matplotlib.pyplot as plt

# circle
# empty image
img = np.ones((100, 100))
# draw circle
c = cv2.circle(img=img, center=[50, 50], radius=10, color=0, thickness=-1, lineType=cv2.FILLED)

# line
c = cv2.line(img=c, pt1=[10, 10], pt2=[10, 80], color=0)

# visualize
plt.imshow(c, cmap="gray")

# transform to tensor
tensor = torch.from_numpy(c)
</code></pre>
<p>Output:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/c/2/c28d193553c30367aef1ef9f62b74811108ee04d.png" alt="image" data-base62-sha1="rL4VK5xH7Y30lMkQiMy3NOrTC9f" width="251" height="251"></p>

Mickey_Han: <p>hi, thank you very much for your time and effort in replying my question ! may I please also just shout out a thank you to the community and the pros with enough kindness to be willing to spend time n help out. you may have literally just saved few hours or even days of time of a newbie who doesnt even know what keyword to search on google. Thank you very much ! ps seen ur other answers in other posts b4, truly appreciate your kindness !!!</p>