Skip to content

Instantly share code, notes, and snippets.

@clin045
Created February 19, 2021 02:38
Show Gist options
  • Save clin045/63f5bfb34f67e058f1395d93333a2aac to your computer and use it in GitHub Desktop.
Save clin045/63f5bfb34f67e058f1395d93333a2aac to your computer and use it in GitHub Desktop.
Remove dust from an image with IR information in the alpha channel
import cv2
import numpy as np
import argparse
from pathlib import Path
import sys
parser = argparse.ArgumentParser(description="Remove dust from an image with IR information in the alpha channel")
parser.add_argument('image',
metavar='img',
type=str,
help='Path to image')
parser.add_argument('outdir',
metavar='out',
type=str,
default=str,
help='Output directory')
parser.add_argument('-thr',
metavar='thr',
type=float,
default=0.95,
help='Percentage of maximum value to threshold the IR mask')
parser.add_argument('-rad',
metavar='rad',
type=float,
default=0.95,
help='Inpainting radius')
parser.add_argument("--masksweep", help="Outputs a series of masks at different thresholds starting from .85", default=False, action='store_true')
args = parser.parse_args()
img = cv2.normalize(cv2.imread(args.image,cv2.IMREAD_UNCHANGED),None, 255,0, cv2.NORM_MINMAX, cv2.CV_8UC1)
rgb_img = img[:,:,:3].astype(np.uint8)
alpha_img = img[:,:,3].astype(np.uint8)
if args.masksweep:
for threshold in range(85, 100):
thresh_mask = ((alpha_img < alpha_img.max() * threshold*0.01) * 255).astype(np.uint8)
cv2.imwrite(args.outdir + "/" + Path(args.image).stem + "_mask-" + str(threshold) + ".tif", thresh_mask)
else:
thresh_mask = ((alpha_img < alpha_img.max() * args.thr) * 255).astype(np.uint8)
cleaned = cv2.inpaint(rgb_img, thresh_mask, args.rad, cv2.INPAINT_NS)
cv2.imwrite(args.outdir + "/" + Path(args.image).stem + "_clean.tif", cleaned)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment