-
-
Save sponnusa/6d86f90de75867917e25e3335f5ad567 to your computer and use it in GitHub Desktop.
An OpenCV script that you can use to transform multiple images into one single image containing the minimum pixel value for each pixel of the input images.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Written on 2013-03-18 by Philipp Klaus <philipp.l.klaus →AT→ web.de>. | |
Check <https://gist.github.com/5188638> for newer versions. | |
An OpenCV script that you can use to transform multiple images into one | |
single image containing the minimum pixel value for each pixel of the | |
input images. The input images have to be of the same dimensions! | |
""" | |
import cv2 | |
import numpy as np | |
import argparse | |
import sys | |
DEBUG = False | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Take a couple of images and determine the minimum values for each pixel.') | |
parser.add_argument('-o', '--outfile', metavar='output-image.png', | |
help='The resulting image is being written to this file.', required=True) | |
parser.add_argument('-g', '--gray', action='store_true', | |
help='Use this option if your input images ar grayscale!') | |
parser.add_argument('-v', '--verbose', action='store_true', | |
help='Give more detailed output.') | |
parser.add_argument('infiles', metavar='INFILE', nargs='+', | |
help='The image files you want to be analyzed.') | |
args = parser.parse_args() | |
if len(args.infiles) == 1: | |
sys.stderr.write("Using this tool to calculate the minimum with only one input image doesn't make sense.\n") | |
sys.stderr.write("Continuing anyway.\n") | |
outimg = None | |
for infile in args.infiles: | |
if args.verbose: print "Analyzing %s now." % infile | |
inimg = cv2.imread(infile, 0 if args.gray else 1) | |
if outimg == None: | |
outimg = np.copy(inimg) | |
if DEBUG: | |
cv2.imshow('current input image', inimg) | |
cv2.waitKey() | |
outimg = np.minimum(outimg, inimg) | |
# Here is an alternative to numpy.minimum() but it's much slower, | |
# I left it here because it shows how to work with np.select(): | |
#condlist = [outimg<inimg, outimg>=inimg] | |
#choicelist = [outimg, inimg] | |
#outimg = np.select(condlist, choicelist) | |
if DEBUG: | |
cv2.imshow('current output image', outimg) | |
cv2.waitKey() | |
if args.verbose: print "Writing result to %s." % args.outfile | |
cv2.imwrite(args.outfile, outimg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment