Skip to content

Commit

Permalink
perf: more efficient ct crop-boarders --same_crop (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
yxlao authored Mar 13, 2024
1 parent 17936d6 commit bee988d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
53 changes: 35 additions & 18 deletions camtools/tools/crop_boarders.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import argparse
import numpy as np
import camtools as ct
from tqdm import tqdm


def instantiate_parser(parser):
Expand Down Expand Up @@ -105,7 +106,10 @@ def entry_point(parser, args):
]

# Read.
src_ims = [ct.io.imread(src_path, alpha_mode="white") for src_path in src_paths]
src_ims = [
ct.io.imread(src_path, alpha_mode="white")
for src_path in tqdm(src_paths, desc="Reading images")
]
for src_im in src_ims:
if not src_im.dtype == np.float32:
raise ValueError(f"Input image {src_path} must be of dtype float32.")
Expand All @@ -122,20 +126,25 @@ def entry_point(parser, args):
"All images must be of the same shape when --same_crop is " "specified."
)

# Stack images.
src_ims_stacked = np.concatenate(src_ims, axis=2)

# Compute cropping boarders.
crop_u, crop_d, crop_l, crop_r = ct.image.compute_cropping(src_ims_stacked)
croppings = [(crop_u, crop_d, crop_l, crop_r)] * num_ims

# Compute padding.
individual_croppings = [
ct.image.compute_cropping(im)
for im in tqdm(src_ims, desc="Computing croppings")
]
min_crop_u, min_crop_d, min_crop_l, min_crop_r = individual_croppings[0]
for crop_u, crop_d, crop_l, crop_r in individual_croppings[1:]:
min_crop_u = min(min_crop_u, crop_u)
min_crop_d = min(min_crop_d, crop_d)
min_crop_l = min(min_crop_l, crop_l)
min_crop_r = min(min_crop_r, crop_r)
croppings = [(min_crop_u, min_crop_d, min_crop_l, min_crop_r)] * len(src_ims)

# Compute padding (remains unchanged)
if args.pad_pixel != 0:
padding = args.pad_pixel
else:
h, w, _ = src_ims_stacked.shape
h, w, _ = src_ims[0].shape
padding = int(max(h, w) * args.pad_ratio)
paddings = [(padding, padding, padding, padding)] * num_ims
paddings = [(padding, padding, padding, padding)] * len(src_ims)
else:
# Compute cropping boarders.
croppings = [ct.image.compute_cropping(src_im) for src_im in src_ims]
Expand Down Expand Up @@ -186,20 +195,28 @@ def entry_point(parser, args):
)

# Save.
assert num_ims == len(src_paths) == len(dst_paths)
assert num_ims == len(src_ims) == len(dst_ims)
assert num_ims == len(croppings) == len(paddings)
for (
src_path,
dst_path,
src_im,
dst_im,
cropping,
padding,
) in zip(
src_paths,
dst_paths,
src_ims,
dst_ims,
croppings,
paddings,
) in tqdm(
zip(
src_paths,
dst_paths,
src_ims,
dst_ims,
croppings,
paddings,
),
desc="Saving images",
total=num_ims,
disable=True,
):
out_dir = dst_path.parent
if not out_dir.exists():
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies = [
"scikit-image>=0.16.2",
"torch>=1.8.0",
"lpips>=0.1.4",
"tqdm>=4.60.0",
]
description = "CamTools: Camera Tools for Computer Vision."
license = {text = "MIT"}
Expand Down

0 comments on commit bee988d

Please sign in to comment.