added
git-svn-id: http://moon:8086/svn/software/trunk/projects/chromakey@738 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
Generated
+3
@@ -0,0 +1,3 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
Generated
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (cv)" project-jdk-type="Python SDK" />
|
||||
<component name="PythonCompatibilityInspectionAdvertiser">
|
||||
<option name="version" value="3" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/chromakey.iml" filepath="$PROJECT_DIR$/.idea/chromakey.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+143
@@ -0,0 +1,143 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="03a3dd8f-6322-46a9-91ac-da33c526d3ed" name="Default Changelist" comment="" />
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||
</component>
|
||||
<component name="FileTemplateManagerImpl">
|
||||
<option name="RECENT_TEMPLATES">
|
||||
<list>
|
||||
<option value="Python Script" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectId" id="1ij6srjKxzA0W3XYK7oJbOmnwep" />
|
||||
<component name="ProjectViewState">
|
||||
<option name="hideEmptyMiddlePackages" value="true" />
|
||||
<option name="showLibraryContents" value="true" />
|
||||
</component>
|
||||
<component name="PropertiesComponent">
|
||||
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
|
||||
<property name="settings.editor.selected.configurable" value="configurable.group.appearance" />
|
||||
</component>
|
||||
<component name="RunManager" selected="Python.chromakey">
|
||||
<configuration name="chromakey" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
|
||||
<module name="chromakey" />
|
||||
<option name="INTERPRETER_OPTIONS" value="" />
|
||||
<option name="PARENT_ENVS" value="true" />
|
||||
<envs>
|
||||
<env name="PYTHONUNBUFFERED" value="1" />
|
||||
</envs>
|
||||
<option name="SDK_HOME" value="" />
|
||||
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
|
||||
<option name="IS_MODULE_SDK" value="true" />
|
||||
<option name="ADD_CONTENT_ROOTS" value="true" />
|
||||
<option name="ADD_SOURCE_ROOTS" value="true" />
|
||||
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/chromakey.py" />
|
||||
<option name="PARAMETERS" value="-f $PROJECT_DIR$/../../../matlab/trunk/chromakey/gallery_2.png" />
|
||||
<option name="SHOW_COMMAND_LINE" value="false" />
|
||||
<option name="EMULATE_TERMINAL" value="false" />
|
||||
<option name="MODULE_MODE" value="false" />
|
||||
<option name="REDIRECT_INPUT" value="false" />
|
||||
<option name="INPUT_FILE" value="" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="main" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
|
||||
<module name="chromakey" />
|
||||
<option name="INTERPRETER_OPTIONS" value="" />
|
||||
<option name="PARENT_ENVS" value="true" />
|
||||
<envs>
|
||||
<env name="PYTHONUNBUFFERED" value="1" />
|
||||
</envs>
|
||||
<option name="SDK_HOME" value="" />
|
||||
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
|
||||
<option name="IS_MODULE_SDK" value="true" />
|
||||
<option name="ADD_CONTENT_ROOTS" value="true" />
|
||||
<option name="ADD_SOURCE_ROOTS" value="true" />
|
||||
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/main.py" />
|
||||
<option name="PARAMETERS" value="" />
|
||||
<option name="SHOW_COMMAND_LINE" value="false" />
|
||||
<option name="EMULATE_TERMINAL" value="false" />
|
||||
<option name="MODULE_MODE" value="false" />
|
||||
<option name="REDIRECT_INPUT" value="false" />
|
||||
<option name="INPUT_FILE" value="" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<list>
|
||||
<item itemvalue="Python.main" />
|
||||
<item itemvalue="Python.chromakey" />
|
||||
</list>
|
||||
<recent_temporary>
|
||||
<list>
|
||||
<item itemvalue="Python.chromakey" />
|
||||
</list>
|
||||
</recent_temporary>
|
||||
</component>
|
||||
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
||||
<component name="TaskManager">
|
||||
<task active="true" id="Default" summary="Default task">
|
||||
<changelist id="03a3dd8f-6322-46a9-91ac-da33c526d3ed" name="Default Changelist" comment="" />
|
||||
<created>1602408864067</created>
|
||||
<option name="number" value="Default" />
|
||||
<option name="presentableId" value="Default" />
|
||||
<updated>1602408864067</updated>
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
<component name="WindowStateProjectService">
|
||||
<state x="621" y="294" width="732" height="644" key="#Inspections" timestamp="1602414363028">
|
||||
<screen x="64" y="26" width="1856" height="1174" />
|
||||
</state>
|
||||
<state x="621" y="294" width="732" height="644" key="#Inspections/64.26.1856.1174@64.26.1856.1174" timestamp="1602414363028" />
|
||||
<state x="450" y="271" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1602427205388">
|
||||
<screen x="64" y="26" width="1856" height="1174" />
|
||||
</state>
|
||||
<state x="450" y="271" key="#com.intellij.execution.impl.EditConfigurationsDialog/64.26.1856.1174@64.26.1856.1174" timestamp="1602427205388" />
|
||||
<state width="1833" height="388" key="GridCell.Tab.0.bottom" timestamp="1602436299536">
|
||||
<screen x="64" y="26" width="1856" height="1174" />
|
||||
</state>
|
||||
<state width="1833" height="388" key="GridCell.Tab.0.bottom/64.26.1856.1174@64.26.1856.1174" timestamp="1602436299536" />
|
||||
<state width="1833" height="388" key="GridCell.Tab.0.center" timestamp="1602436299536">
|
||||
<screen x="64" y="26" width="1856" height="1174" />
|
||||
</state>
|
||||
<state width="1833" height="388" key="GridCell.Tab.0.center/64.26.1856.1174@64.26.1856.1174" timestamp="1602436299536" />
|
||||
<state width="1833" height="388" key="GridCell.Tab.0.left" timestamp="1602436299535">
|
||||
<screen x="64" y="26" width="1856" height="1174" />
|
||||
</state>
|
||||
<state width="1833" height="388" key="GridCell.Tab.0.left/64.26.1856.1174@64.26.1856.1174" timestamp="1602436299535" />
|
||||
<state width="1833" height="388" key="GridCell.Tab.0.right" timestamp="1602436299536">
|
||||
<screen x="64" y="26" width="1856" height="1174" />
|
||||
</state>
|
||||
<state width="1833" height="388" key="GridCell.Tab.0.right/64.26.1856.1174@64.26.1856.1174" timestamp="1602436299536" />
|
||||
<state width="1833" height="388" key="GridCell.Tab.1.bottom" timestamp="1602436299537">
|
||||
<screen x="64" y="26" width="1856" height="1174" />
|
||||
</state>
|
||||
<state width="1833" height="388" key="GridCell.Tab.1.bottom/64.26.1856.1174@64.26.1856.1174" timestamp="1602436299537" />
|
||||
<state width="1833" height="388" key="GridCell.Tab.1.center" timestamp="1602436299536">
|
||||
<screen x="64" y="26" width="1856" height="1174" />
|
||||
</state>
|
||||
<state width="1833" height="388" key="GridCell.Tab.1.center/64.26.1856.1174@64.26.1856.1174" timestamp="1602436299536" />
|
||||
<state width="1833" height="388" key="GridCell.Tab.1.left" timestamp="1602436299536">
|
||||
<screen x="64" y="26" width="1856" height="1174" />
|
||||
</state>
|
||||
<state width="1833" height="388" key="GridCell.Tab.1.left/64.26.1856.1174@64.26.1856.1174" timestamp="1602436299536" />
|
||||
<state width="1833" height="388" key="GridCell.Tab.1.right" timestamp="1602436299536">
|
||||
<screen x="64" y="26" width="1856" height="1174" />
|
||||
</state>
|
||||
<state width="1833" height="388" key="GridCell.Tab.1.right/64.26.1856.1174@64.26.1856.1174" timestamp="1602436299536" />
|
||||
</component>
|
||||
<component name="XDebuggerManager">
|
||||
<breakpoint-manager>
|
||||
<breakpoints>
|
||||
<line-breakpoint enabled="true" suspend="THREAD" type="python-line">
|
||||
<url>file://$PROJECT_DIR$/main.py</url>
|
||||
<line>8</line>
|
||||
<option name="timeStamp" value="1" />
|
||||
</line-breakpoint>
|
||||
</breakpoints>
|
||||
</breakpoint-manager>
|
||||
</component>
|
||||
</project>
|
||||
+129
@@ -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()
|
||||
@@ -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/
|
||||
BIN
Binary file not shown.
|
After Width: | Height: | Size: 1013 KiB |
Reference in New Issue
Block a user