commit 5ba397f6d674a4da044f1509725d37e13092de32 Author: Jens Ahrensfeld Date: Mon Oct 12 06:36:37 2020 +0000 added git-svn-id: http://moon:8086/svn/software/trunk/projects/chromakey@738 b431acfa-c32f-4a4a-93f1-934dc6c82436 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/chromakey.iml b/.idea/chromakey.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/chromakey.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..c66b234 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..dce4812 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..1ae450f --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,143 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1602408864067 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + file://$PROJECT_DIR$/main.py + 8 + + + + + \ No newline at end of file diff --git a/chromakey.py b/chromakey.py new file mode 100644 index 0000000..fe8c939 --- /dev/null +++ b/chromakey.py @@ -0,0 +1,129 @@ +#!/usr/bin/env python +''' +Chroma keying in OpenCV + +''' +# Python 2/3 compatibility +from __future__ import print_function + +import numpy as np +import sys +import argparse +import cv2 as cv + + +def colordistance(color1, color2): + d0 = (color1[0] - color2[0]) + d1 = (color1[1] - color2[1]) + d2 = (color1[2] - color2[2]) + return np.math.sqrt(d0 * d0 + d1 * d1 + d2 * d2) + +def blend(src, matte): + result = np.copy(src) + + result[:, :, 0] = src[:, :, 0] * matte + result[:, :, 1] = src[:, :, 1] * matte + result[:, :, 2] = src[:, :, 2] * matte + + return result + +def wait_key(): + while True: + ch = cv.waitKey() + if ch == 27: + break + +class ImageWindow(): + def __init__(self, name): + self.name = name + cv.namedWindow(self.name) + + def update(self, img): + cv.imshow(self.name, img) + +class Chromakey(): + def __init__(self): + self.matte = None + self.k_ref = 0.95 + self.k_var = 1 + self.k_gain = 1 + self.color_key = np.array([0.9, 0.2, 0.1]) + ap = argparse.ArgumentParser() + ap.add_argument("-f", "--filename", type=str, default=None, + help="Input image") + + args = vars(ap.parse_args()) + inputfilename = args["filename"] + + self.img_source = np.float32(cv.imread(inputfilename)) / 255 + if self.img_source is None: + print('Failed to load file:', inputfilename) + sys.exit(1) + + self.win_source = ImageWindow('source') + self.win_matte = ImageWindow('matte') + self.win_final = ImageWindow('final') + self.win_edges = ImageWindow('edges') + cv.createTrackbar('k_ref', 'matte', 0, 150, self.on_change_k_ref) + cv.createTrackbar('k_var', 'matte', 0, 1000, self.on_change_k_var) + cv.createTrackbar('k_gain', 'matte', 0, 2000, self.on_change_k_gain) + cv.setMouseCallback('source', self.onMouse) + self.update_controls() + self.update_source() + self.update_matte() + self.update_edges() + self.update_final() + wait_key() + print('Done') + + def onMouse(self, event, x, y, flags, param): + if (event == cv.EVENT_LBUTTONDOWN): + self.color_key = self.img_source[y,x] + print("color_key", self.color_key) + self.update_matte() + + def update_controls(self): + cv.setTrackbarPos('k_ref', 'matte', int(self.k_ref*100)) + cv.setTrackbarPos('k_var', 'matte', int(self.k_var*100)) + cv.setTrackbarPos('k_gain', 'matte', int(self.k_gain*100)) + + def on_change_k_ref(self, v=0): + self.k_ref = v/100 + self.update_matte() + + def on_change_k_var(self, v=0): + self.k_var = v/100 + self.update_matte() + + def on_change_k_gain(self, v=0): + self.k_gain = v/100 + self.update_matte() + + def update_source(self): + self.win_source.update(self.img_source) + + def update_matte(self): + img_filt = self.img_source - self.k_ref * self.color_key + img_norm = np.linalg.norm(img_filt, axis=2) + img_var = np.var(img_filt, axis=2) + img_exp = self.k_gain*(1-np.exp(-self.k_var*img_var)) + self.matte = np.clip(img_exp, 0, 1) + self.win_matte.update(self.matte) + self.update_edges() + + def update_edges(self): + img_filt = self.img_source - self.k_ref * self.color_key + img_norm = np.linalg.norm(img_filt, axis=2) + edges = np.clip(img_norm-self.matte, 0, 1) + self.win_edges.update(edges) + self.update_final() + + def update_final(self): + # Multiply the foreground with the alpha matte + self.img_final = blend(self.img_source, self.matte) + self.win_final.update(self.img_final) + +if __name__ == '__main__': + print(__doc__) + ck = Chromakey() + cv.destroyAllWindows() diff --git a/main.py b/main.py new file mode 100644 index 0000000..76d0e8c --- /dev/null +++ b/main.py @@ -0,0 +1,16 @@ +# This is a sample Python script. + +# Press Umschalt+F10 to execute it or replace it with your code. +# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. + + +def print_hi(name): + # Use a breakpoint in the code line below to debug your script. + print(f'Hi, {name}') # Press Strg+F8 to toggle the breakpoint. + + +# Press the green button in the gutter to run the script. +if __name__ == '__main__': + print_hi('PyCharm') + +# See PyCharm help at https://www.jetbrains.com/help/pycharm/ diff --git a/sample.png b/sample.png new file mode 100644 index 0000000..e11b249 Binary files /dev/null and b/sample.png differ