Friday, 27 May 2022

How do I use np.linspace() to change the pixels colors of a patch in an image

I drew a straight line by changing the color of 50 pixels in the image below. The line is the diagonal line shown on the image

enter image description here

Here is how I did it.

zeros = torch.zeros_like(img) #img is b,c,h,w

x1, y1, x2, y2, r, g,  b = 16, 50, 100, 100, 6, 2, 4
rgb = torch.tensor([r, g, b])
cx, cy = x2 - x1, y2 - y1

for t in np.linspace(0, 1, 50):
    px, py = x1 + t * cx, y1 + t * cy
    zeros[0, :, int(py), int(px)] = rgb

mask = (zeros == 0).float()
im = mask * img + (1 - mask) *zeros

It is obvious in np.linspace(0, 1, 50) that I am changing 50 pixels. I want to use the same method to change a specific portion of the image, like a 14 x 14 patch of my image if the image size is 224 x 224 or even more. The range of pixels to cover now will be 196 -> (since there are 196 pixels in a 14 x 14 patch) instead of 50. How do I go about it, please?



from How do I use np.linspace() to change the pixels colors of a patch in an image

No comments:

Post a Comment