BadCafeCode / masquerade-nodes-comfyui

A powerful set of mask-related nodes for ComfyUI
MIT License
348 stars 34 forks source link

MaskToRegion is 1 pixel too large #6

Open Acly opened 1 year ago

Acly commented 1 year ago

Reproducer:

>>> mask = torch.zeros([1, 4, 4])
>>> mask[0, 0:2, 0:2] = torch.ones([2, 2])
>>> mask
tensor([[[1., 1., 0., 0.],
         [1., 1., 0., 0.],
         [0., 0., 0., 0.],
         [0., 0., 0., 0.]]])
>>> MaskNodes.MaskToRegion().get_region(mask, 0, 'multiple_of', 2, 2, 0, 0, 'match_ratio')
(tensor([[[1., 1., 1., 0.],
         [1., 1., 1., 0.],
         [1., 1., 1., 0.],
         [0., 0., 0., 0.]]]),)

I believe the width/height calculation is wrong? it should rather be:

width = max_x - min_x + 1
height = max_y - min_y + 1
Acly commented 1 year ago

Also this code should probably clamp max_x/max_y to the mask's max, ie. mask_width - 1 and mask_height - 1.