Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Get Bounding Box" node #1884

Merged
merged 8 commits into from
Jun 23, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from __future__ import annotations

from typing import Tuple

import numpy as np

from nodes.properties.inputs import ImageInput, SliderInput
from nodes.properties.outputs import NumberOutput
from nodes.utils.utils import get_h_w_c

from .. import utility_group


@utility_group.register(
schema_id="chainner:image:get_bbox",
name="Get Bounding Box",
description="Gets a bounding box (X, Y, Height, and Width) of the white area of a mask.",
icon="BsRulers",
zeptofine marked this conversation as resolved.
Show resolved Hide resolved
inputs=[
ImageInput(channels=1),
SliderInput(
"Threshold",
precision=1,
minimum=1,
zeptofine marked this conversation as resolved.
Show resolved Hide resolved
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
controls_step=1,
slider_step=1,
default=1,
zeptofine marked this conversation as resolved.
Show resolved Hide resolved
),
],
outputs=[
NumberOutput("X"),
NumberOutput("Y"),
NumberOutput("Width"),
NumberOutput("Height"),
zeptofine marked this conversation as resolved.
Show resolved Hide resolved
],
)
def get_dimensions_node(
zeptofine marked this conversation as resolved.
Show resolved Hide resolved
img: np.ndarray,
thresh_val: float,
) -> Tuple[int, int, int, int]:
# Threshold value 100 guarantees an empty image, so make sure the max
# is just below that.
thresh = min(thresh_val / 100, 0.99999)
h, w, _ = get_h_w_c(img)

r = np.any(img > thresh, 1)
if r.any():
c = np.any(img > thresh, 0)
x, y = c.argmax(), r.argmax()
return (
int(x),
int(y),
int(w - x - c[::-1].argmax()),
int(h - y - r[::-1].argmax()),
)
else:
return 0, 0, w, h