| | import torch
|
| |
|
| | def bresenham_line(x0: int, y0: int, x1: int, y1: int):
|
| | """
|
| | Integer Bresenham line algorithm.
|
| | Returns two Python lists: xs, ys (same length).
|
| | """
|
| | xs = []
|
| | ys = []
|
| |
|
| | dx = abs(x1 - x0)
|
| | sx = 1 if x0 < x1 else -1
|
| | dy = -abs(y1 - y0)
|
| | sy = 1 if y0 < y1 else -1
|
| | err = dx + dy
|
| |
|
| | x, y = x0, y0
|
| | while True:
|
| | xs.append(x)
|
| | ys.append(y)
|
| | if x == x1 and y == y1:
|
| | break
|
| | e2 = 2 * err
|
| | if e2 >= dy:
|
| | err += dy
|
| | x += sx
|
| | if e2 <= dx:
|
| | err += dx
|
| | y += sy
|
| |
|
| | return xs, ys
|
| |
|
| |
|
| | class BatchRaycast_2D:
|
| | """
|
| | Returns the first image in the batch where the START->END line
|
| | is completely "white enough" according to the chosen mode/threshold.
|
| | """
|
| |
|
| | @classmethod
|
| | def INPUT_TYPES(cls):
|
| | return {
|
| | "required": {
|
| | "images": ("IMAGE",),
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | "white_mode": (["max_channel", "all_channels", "luminance", "green_only"], {"default": "max_channel"}),
|
| |
|
| |
|
| |
|
| | "threshold": ("FLOAT", {"default": 0.98, "min": 0.0, "max": 1.0, "step": 0.005}),
|
| |
|
| |
|
| | "fallback": (["return_first", "return_last"], {"default": "return_last"}),
|
| | },
|
| | "optional": {
|
| |
|
| | "start_x": ("INT", {"default": 0, "min": -999999, "max": 999999}),
|
| | "start_y": ("INT", {"default": 386, "min": -999999, "max": 999999}),
|
| | "end_x": ("INT", {"default": 330, "min": -999999, "max": 999999}),
|
| | "end_y": ("INT", {"default": 385, "min": -999999, "max": 999999}),
|
| | }
|
| | }
|
| |
|
| | RETURN_TYPES = ("IMAGE", "INT")
|
| | RETURN_NAMES = ("image", "index")
|
| | FUNCTION = "pick"
|
| | CATEGORY = "image/filter"
|
| |
|
| | def pick(
|
| | self,
|
| | images,
|
| | white_mode="max_channel",
|
| | threshold=0.98,
|
| | fallback="return_last",
|
| | start_x=0,
|
| | start_y=386,
|
| | end_x=330,
|
| | end_y=385,
|
| | ):
|
| |
|
| | if not isinstance(images, torch.Tensor):
|
| | raise TypeError("images must be a torch.Tensor (ComfyUI IMAGE type).")
|
| |
|
| | if images.ndim != 4 or images.shape[-1] < 3:
|
| | raise ValueError(f"Expected images shape [B,H,W,C>=3], got {tuple(images.shape)}")
|
| |
|
| | B, H, W, C = images.shape
|
| |
|
| |
|
| | xs_list, ys_list = bresenham_line(int(start_x), int(start_y), int(end_x), int(end_y))
|
| |
|
| |
|
| | device = images.device
|
| | xs = torch.tensor(xs_list, device=device, dtype=torch.long)
|
| | ys = torch.tensor(ys_list, device=device, dtype=torch.long)
|
| |
|
| |
|
| | if xs.min().item() < 0 or ys.min().item() < 0 or xs.max().item() >= W or ys.max().item() >= H:
|
| |
|
| | idx = 0 if fallback == "return_first" else max(B - 1, 0)
|
| | return (images[idx:idx+1], int(-1))
|
| |
|
| |
|
| | pixels = images[:, ys, xs, :3]
|
| |
|
| | if white_mode == "max_channel":
|
| |
|
| | white_mask = pixels.max(dim=-1).values >= threshold
|
| |
|
| | elif white_mode == "all_channels":
|
| |
|
| | white_mask = pixels.min(dim=-1).values >= threshold
|
| |
|
| | elif white_mode == "green_only":
|
| |
|
| | white_mask = pixels[..., 1] >= threshold
|
| |
|
| | elif white_mode == "luminance":
|
| |
|
| | weights = torch.tensor([0.2126, 0.7152, 0.0722], device=device, dtype=pixels.dtype)
|
| | lum = (pixels * weights).sum(dim=-1)
|
| | white_mask = lum >= threshold
|
| |
|
| | else:
|
| | raise ValueError(f"Unknown white_mode: {white_mode}")
|
| |
|
| |
|
| | line_is_white = white_mask.all(dim=1)
|
| |
|
| | found = torch.nonzero(line_is_white, as_tuple=False).flatten()
|
| | if found.numel() > 0:
|
| | idx = int(found[0].item())
|
| | return (images[idx:idx+1], idx)
|
| |
|
| |
|
| | idx = 0 if fallback == "return_first" else max(B - 1, 0)
|
| | return (images[idx:idx+1], int(-1))
|
| |
|
| |
|
| | NODE_CLASS_MAPPINGS = {
|
| | "BatchRaycast_2D": BatchRaycast_2D
|
| | }
|
| |
|
| | NODE_DISPLAY_NAME_MAPPINGS = {
|
| | "BatchRaycast_2D": "BatchRaycast_2D"
|
| | }
|
| |
|