Created
June 27, 2014 11:33
-
-
Save MareArts/2ea9b9ca7c80c8c259e1 to your computer and use it in GitHub Desktop.
Image subtraction simple example using opencv on python
This file contains 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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" /> | |
</project> | |
This file contains 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
<?xml version="1.0" encoding="UTF-8"?> | |
<module type="PYTHON_MODULE" version="4"> | |
<component name="NewModuleRootManager"> | |
<content url="file://$MODULE_DIR$" /> | |
<orderEntry type="jdk" jdkName="Python 2.7.5 (C:\Python27\python.exe)" jdkType="Python SDK" /> | |
<orderEntry type="sourceFolder" forTests="false" /> | |
</component> | |
</module> | |
This file contains 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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7.5 (C:\Python27\python.exe)" project-jdk-type="Python SDK" /> | |
<component name="PyConsoleOptionsProvider"> | |
<option name="myPythonConsoleState"> | |
<console-settings /> | |
</option> | |
</component> | |
</project> | |
This file contains 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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectModuleManager"> | |
<modules> | |
<module fileurl="file://$PROJECT_DIR$/.idea/imgeSubtraction.iml" filepath="$PROJECT_DIR$/.idea/imgeSubtraction.iml" /> | |
</modules> | |
</component> | |
</project> | |
This file contains 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
<component name="DependencyValidationManager"> | |
<state> | |
<option name="SKIP_IMPORT_STATEMENTS" value="false" /> | |
</state> | |
</component> |
This file contains 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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="VcsDirectoryMappings"> | |
<mapping directory="" vcs="" /> | |
</component> | |
</project> | |
This file contains 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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ChangeListManager"> | |
<option name="TRACKING_ENABLED" value="true" /> | |
<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="ChangesViewManager" flattened_view="true" show_ignored="false" /> | |
<component name="CreatePatchCommitExecutor"> | |
<option name="PATCH_PATH" value="" /> | |
</component> | |
<component name="DaemonCodeAnalyzer"> | |
<disable_hints /> | |
</component> | |
<component name="ProjectLevelVcsManager" settingsEditedManually="false"> | |
<OptionsSetting value="true" id="Add" /> | |
<OptionsSetting value="true" id="Remove" /> | |
<OptionsSetting value="true" id="Checkout" /> | |
<OptionsSetting value="true" id="Update" /> | |
<OptionsSetting value="true" id="Status" /> | |
<OptionsSetting value="true" id="Edit" /> | |
<ConfirmationsSetting value="0" id="Add" /> | |
<ConfirmationsSetting value="0" id="Remove" /> | |
</component> | |
<component name="ProjectReloadState"> | |
<option name="STATE" value="0" /> | |
</component> | |
<component name="PropertiesComponent"> | |
<property name="options.lastSelected" value="org.jetbrains.plugins.sass.extensions.compass.CompassConfigurableProvider$1" /> | |
<property name="options.splitter.main.proportions" value="0.3" /> | |
<property name="options.splitter.details.proportions" value="0.2" /> | |
<property name="options.searchVisible" value="true" /> | |
</component> | |
<component name="RunManager"> | |
<list size="0" /> | |
</component> | |
<component name="ShelveChangesManager" show_recycled="false" /> | |
<component name="TaskManager"> | |
<task active="true" id="Default" summary="Default task" /> | |
<servers /> | |
</component> | |
<component name="VcsContentAnnotationSettings"> | |
<option name="myLimit" value="2678400000" /> | |
</component> | |
<component name="VcsManagerConfiguration"> | |
<option name="myTodoPanelSettings"> | |
<TodoPanelSettings /> | |
</option> | |
</component> | |
<component name="XDebuggerManager"> | |
<breakpoint-manager /> | |
</component> | |
</project> | |
This file contains 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
__author__ = 'mare' | |
import numpy as np | |
import cv2 | |
class ImgSubtraction: | |
#image load | |
def __init__(self, r_img, th1, th2): | |
self.RImg = r_img | |
self.Th1, self.Th2 = th1, th2 | |
self.cols, self.rows = r_img.shape[:2] | |
self.area = self.cols * self.rows | |
#image subtraction | |
def eval_subtraction(self, c_img): | |
#return false if c_img size is different with RImg | |
if self.RImg.shape[:2] != c_img.shape[:2]: | |
return 0 | |
ic_img = c_img | |
#subtraction | |
is_img = np.subtract(self.RImg, np.int_(ic_img)) | |
#abs | |
ia_img = np.abs(is_img) | |
#count pixels difference over than th1 | |
dcount = np.sum(ia_img > self.Th1) | |
#image change percent | |
dpersent = (dcount/np.float32(self.area) ) * 100 | |
if dpersent >= self.Th2: | |
return 1 | |
else: | |
return 0 |
This file contains 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
__author__ = 'mare' | |
import cv2 | |
from class_ImgSubtraction import ImgSubtraction | |
RImg = cv2.imread('test.png', 0) | |
CImg = cv2.imread('test2.png', 0) | |
e1 = cv2.getTickCount() | |
cImgSub = ImgSubtraction(RImg, 10, 1) | |
if cImgSub.eval_subtraction(CImg): | |
print ('image different') | |
e2 = cv2.getTickCount() | |
time = (e2 - e1)/cv2.getTickFrequency() | |
print(time, 1/time) | |
cv2.waitKey(0) |
This file contains 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
�PNG | |