Skip to content

Commit

Permalink
Fix issues with the apply node
Browse files Browse the repository at this point in the history
  • Loading branch information
Kidev committed Jan 21, 2025
1 parent 3546210 commit 3737c8b
Show file tree
Hide file tree
Showing 3 changed files with 290 additions and 16 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Provides tools for applying and removing fisheye lens effects from images.
- Format options: fullframe and circular
- Adjustable FOV and PFOV parameters

### Tips
- The `orthographic` mapping keeps the entirety of the image data, so going back and forth works well, see `examples/BackAndForth.json`

## Installation

1. Clone this repository into your ComfyUI custom_nodes folder:
Expand Down
269 changes: 269 additions & 0 deletions examples/BackAndForth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
{
"last_node_id": 8,
"last_link_id": 6,
"nodes": [
{
"id": 4,
"type": "PreviewImage",
"pos": [
140,
-70
],
"size": [
210,
246
],
"flags": {},
"order": 3,
"mode": 0,
"inputs": [
{
"name": "images",
"type": "IMAGE",
"link": 4
}
],
"outputs": [],
"properties": {
"Node name for S&R": "PreviewImage",
"enableTabs": false,
"tabWidth": 65,
"tabXOffset": 10,
"hasSecondTab": false,
"secondTabText": "Send Back",
"secondTabOffset": 80,
"secondTabWidth": 65
},
"widgets_values": []
},
{
"id": 5,
"type": "PreviewImage",
"pos": [
620,
-490
],
"size": [
430,
430
],
"flags": {},
"order": 4,
"mode": 0,
"inputs": [
{
"name": "images",
"type": "IMAGE",
"link": 3
}
],
"outputs": [],
"properties": {
"Node name for S&R": "PreviewImage",
"enableTabs": false,
"tabWidth": 65,
"tabXOffset": 10,
"hasSecondTab": false,
"secondTabText": "Send Back",
"secondTabOffset": 80,
"secondTabWidth": 65
},
"widgets_values": []
},
{
"id": 3,
"type": "LoadImage",
"pos": [
-700,
-580
],
"size": [
470,
450
],
"flags": {},
"order": 0,
"mode": 0,
"inputs": [],
"outputs": [
{
"name": "IMAGE",
"type": "IMAGE",
"links": [
1
],
"slot_index": 0
},
{
"name": "MASK",
"type": "MASK",
"links": null
}
],
"properties": {
"Node name for S&R": "LoadImage",
"enableTabs": false,
"tabWidth": 65,
"tabXOffset": 10,
"hasSecondTab": false,
"secondTabText": "Send Back",
"secondTabOffset": 80,
"secondTabWidth": 65
},
"widgets_values": [
"fisheye-image.jpg",
"image"
]
},
{
"id": 2,
"type": "Defisheye",
"pos": [
-170,
-280
],
"size": [
315,
130
],
"flags": {},
"order": 1,
"mode": 0,
"inputs": [
{
"name": "image",
"type": "IMAGE",
"link": 1
}
],
"outputs": [
{
"name": "IMAGE",
"type": "IMAGE",
"links": [
2,
4
],
"slot_index": 0
}
],
"properties": {
"Node name for S&R": "Defisheye",
"enableTabs": false,
"tabWidth": 65,
"tabXOffset": 10,
"hasSecondTab": false,
"secondTabText": "Send Back",
"secondTabOffset": 80,
"secondTabWidth": 65
},
"widgets_values": [
"orthographic",
"fullframe",
180,
120
]
},
{
"id": 1,
"type": "Fisheye",
"pos": [
240,
-340
],
"size": [
315,
130
],
"flags": {},
"order": 2,
"mode": 0,
"inputs": [
{
"name": "image",
"type": "IMAGE",
"link": 2
}
],
"outputs": [
{
"name": "IMAGE",
"type": "IMAGE",
"links": [
3
],
"slot_index": 0
}
],
"properties": {
"Node name for S&R": "Fisheye",
"enableTabs": false,
"tabWidth": 65,
"tabXOffset": 10,
"hasSecondTab": false,
"secondTabText": "Send Back",
"secondTabOffset": 80,
"secondTabWidth": 65
},
"widgets_values": [
"orthographic",
"fullframe",
180,
120
]
}
],
"links": [
[
1,
3,
0,
2,
0,
"IMAGE"
],
[
2,
2,
0,
1,
0,
"IMAGE"
],
[
3,
1,
0,
5,
0,
"IMAGE"
],
[
4,
2,
0,
4,
0,
"IMAGE"
]
],
"groups": [],
"config": {},
"extra": {
"ds": {
"scale": 0.9229599817706413,
"offset": [
1037.145963590657,
949.7047748071493
]
},
"node_versions": {
"comfy-core": "0.3.12",
"ComfyUI-Fisheye-effects": "0.0.1"
},
"ue_links": [],
"VHS_latentpreview": true,
"VHS_latentpreviewrate": 1
},
"version": 0.4
}
34 changes: 18 additions & 16 deletions nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def tensor_to_image(self, tensor):
img_np = np.array(tensor).astype(np.float32) / 255.0
return torch.from_numpy(img_np).unsqueeze(0)


class FisheyeNode(FisheyeBase):
@classmethod
def INPUT_TYPES(cls):
Expand All @@ -33,8 +34,8 @@ def INPUT_TYPES(cls):
"image": ("IMAGE",),
"mapping": (["equidistant", "equisolid", "orthographic", "stereographic"],),
"format": (["fullframe", "circular"],),
"fov": ("FLOAT", {"default": 180.0, "min": 0.0, "max": 360.0, "step": 0.1}),
"pfov": ("FLOAT", {"default": 120.0, "min": 0.0, "max": 360.0, "step": 0.1}),
"fov": ("FLOAT", {"default": 180.0, "min": 0.0, "max": 360.0, "step": 30.0}),
"pfov": ("FLOAT", {"default": 120.0, "min": 0.0, "max": 360.0, "step": 30.0}),
},
}

Expand All @@ -47,29 +48,30 @@ def map_fisheye(self, i, j, width, height, dim, xcenter, ycenter):
yd = j - ycenter
rd = hypot(xd, yd)

rn = rd / (dim / 2)
rmax = dim / 2 if self.format == "circular" else hypot(width / 2, height / 2)

theta = rn * (self.fov * pi / 360)
phiang = arctan(rd / rmax)
theta = phiang * self.pfov / (self.fov / 2)

# See https://en.wikipedia.org/wiki/Fisheye_lens
if self.mapping == "equidistant":
r = theta
r = rmax * theta / (pi / 2)
elif self.mapping == "equisolid":
r = 2 * sin(theta / 2)
r = 2 * rmax * sin(theta / 2)
elif self.mapping == "orthographic":
r = sin(theta)
r = rmax * sin(theta)
elif self.mapping == "stereographic":
r = 2 * tan(theta / 2)

r = r * (dim / 2)
r = 2 * rmax * tan(theta / 2)

rdmask = rd != 0

xs = xd.astype(np.float32).copy()
ys = yd.astype(np.float32).copy()

xs[rdmask] = (r[rdmask] / rd[rdmask]) * xd[rdmask] + xcenter
ys[rdmask] = (r[rdmask] / rd[rdmask]) * yd[rdmask] + ycenter
scale = 1.0
if self.mapping == "equidistant":
scale = 1.5

xs[rdmask] = scale * (rd[rdmask] / r[rdmask]) * xd[rdmask] + xcenter
ys[rdmask] = scale * (rd[rdmask] / r[rdmask]) * yd[rdmask] + ycenter

xs[~rdmask] = xcenter
ys[~rdmask] = ycenter
Expand Down Expand Up @@ -108,8 +110,8 @@ def INPUT_TYPES(cls):
"image": ("IMAGE",),
"mapping": (["equidistant", "equisolid", "orthographic", "stereographic"],),
"format": (["fullframe", "circular"],),
"fov": ("FLOAT", {"default": 180.0, "min": 0.0, "max": 360.0, "step": 5.0}),
"pfov": ("FLOAT", {"default": 120.0, "min": 0.0, "max": 360.0, "step": 5.0}),
"fov": ("FLOAT", {"default": 180.0, "min": 0.0, "max": 360.0, "step": 30.0}),
"pfov": ("FLOAT", {"default": 120.0, "min": 0.0, "max": 360.0, "step": 30.0}),
},
}

Expand Down

0 comments on commit 3737c8b

Please sign in to comment.