RuntimeError: Subtraction, the `-` operator, with a bool tensor is not supported. If you are trying to invert a mask, use the `~` or `logical_not()` operator instead

I have implemented a function to thin an image using numpy package. Now, I tried to convert that implementation using pytorch.

Numpy Implementation:

# calculate dx and dy - this is correct as can be seen in a picture
dx = ((orien < ((3/8) * np.pi)).astype(np.float32) - (orien > ((5/8) * np.pi)).astype(np.float32)).astype(bool)
dy = (((orien > ((1/8) * np.pi)) & (orien <= ((1/2) * np.pi))).astype(np.float32) - ((orien > ((1/2) * np.pi)) & (orien < ((7/8) * np.pi))).astype(np.float32)).astype(bool)
                
# normally the same pixels would be checked if (dy and dx > 0) and (dy and dx < 0). because
# different pixels should be checked with a gabor-orientation of 45 and 135 this difference should
# be checked
                
if (dy < 0) & (dx < 0):
    result[i,j] = np.multiply(((matrixB[i,j] >= matrixB[i+dy, j+dx]) & (matrixB[i,j] >= matrixB[i-dy, j-dx])), matrixB[i,j])
else:
    result[i,j] = np.multiply(((matrixB[i,j] >= matrixB[i-dy, j+dx]) & (matrixB[i,j] >= matrixB[i+dy, j-dx])), matrixB[i,j])

Corresponding torch code:

dx = ((orien < ((3/8) * torch.pi)).to(torch.float32) - (orien > ((5/8) * torch.pi)).to(torch.float32)).to(torch.bool)
dy = (((orien > ((1/8) * torch.pi)) & (orien <= ((1/2) * torch.pi))).to(torch.float32) - ((orien > ((1/2) * torch.pi)) & (orien < ((7/8) * torch.pi))).to(torch.float32)).to(torch.bool)


if (dy < 0) & (dx < 0):
    result[i,j] = torch.mul(((matrixB[i,j] >= matrixB[i+dy, j+dx]) & (matrixB[i,j] >= matrixB[i-dy, j-dx])), matrixB[i,j])
else:
    result[i,j] = torch.mul(((matrixB[i,j] >= matrixB[i-dy, j+dx]) & (matrixB[i,j] >= matrixB[i+dy, j-dx])), matrixB[i,j])

I am getting the following error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[18], line 29
     26 plt.imshow(output_image, cmap='gray')
     27 plt.show()
---> 29 thinning_output = thinning(output_image, oriens_matrix, 1)
     30 plt.imshow(thinning_output, cmap='gray')
     31 plt.show()

File ~/Code/corf_dst/push_pull_corf/thin_image.py:43, in thinning(matrix, oriens_matrix, method)
     40                 result[i,j] = torch.mul(((matrixB[i,j] >= matrixB[i+dy, j+dx]) & ~(matrixB[i,j] >= matrixB[i-dy, j-dx])), matrixB[i,j])
     41                 # result[i,j] = torch.mul(((matrixB[i,j] >= matrixB[i+dy, j+dx]) & torch.logical_not((matrixB[i,j] >= matrixB[i-dy, j-dx]))), matrixB[i,j])
     42             else:
---> 43                 result[i,j] = torch.mul(((matrixB[i,j] >= matrixB[i-dy, j+dx]) & ~(matrixB[i,j] >= matrixB[i+dy, j-dx])), matrixB[i,j])
     44                 # result[i,j] = torch.mul(((matrixB[i,j] >= matrixB[i-dy, j+dx]) & torch.logical_not((matrixB[i,j] >= matrixB[i+dy, j-dx]))), matrixB[i,j])
     45 
     46 
     47 else: # linear thinning
     48     for i in range(1, h+1): # for rows

File /home/anaconda3/envs/neeraj/lib/python3.9/site-packages/torch/_tensor.py:39, in _handle_torch_function_and_wrap_type_error_to_not_implemented.<locals>.wrapped(*args, **kwargs)
     37     if has_torch_function(args):
     38         return handle_torch_function(wrapped, args, *args, **kwargs)
---> 39     return f(*args, **kwargs)
     40 except TypeError:
     41     return NotImplemented

File /home/anaconda3/envs/neeraj/lib/python3.9/site-packages/torch/_tensor.py:834, in Tensor.__rsub__(self, other)
    832 @_handle_torch_function_and_wrap_type_error_to_not_implemented
    833 def __rsub__(self, other):
--> 834     return _C._VariableFunctions.rsub(self, other)

RuntimeError: Subtraction, the `-` operator, with a bool tensor is not supported. If you are trying to invert a mask, use the `~` or `logical_not()` operator instead.

Can anyone please help me out?

Based on the error message and the code it seems you are trying to subtract a bool tensor from an index value i and j.
In this case, transform the bool to long and the subtraction as well as the indexing should work: i - dy.long().