From 5390176cff1071ae01d006904835c97a187004b4 Mon Sep 17 00:00:00 2001
From: Edmond Yoo <hj3yoo@uwaterloo.ca>
Date: Sun, 19 Aug 2018 02:55:11 +0000
Subject: [PATCH] card box detection algo draft

---
 /dev/null        |  280 ----------------------------------------------
 .gitignore       |    4 
 card_detector.py |   65 ++++++++--
 3 files changed, 55 insertions(+), 294 deletions(-)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a5fbd1e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+#IDE/compiler
+.idea/
+__pycache/
+
diff --git a/.idea/.name b/.idea/.name
deleted file mode 100644
index 057ce0a..0000000
--- a/.idea/.name
+++ /dev/null
@@ -1 +0,0 @@
-MTGCardDetector
\ No newline at end of file
diff --git a/.idea/MTGCardDetector.iml b/.idea/MTGCardDetector.iml
deleted file mode 100644
index f65a012..0000000
--- a/.idea/MTGCardDetector.iml
+++ /dev/null
@@ -1,14 +0,0 @@
-<?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 3.6 Conda" jdkType="Python SDK" />
-    <orderEntry type="sourceFolder" forTests="false" />
-  </component>
-  <component name="PyDocumentationSettings">
-    <option name="renderExternalDocumentation" value="true" />
-  </component>
-  <component name="TestRunnerService">
-    <option name="PROJECT_TEST_RUNNER" value="Unittests" />
-  </component>
-</module>
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
deleted file mode 100644
index ef3e5d2..0000000
--- a/.idea/misc.xml
+++ /dev/null
@@ -1,7 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="JavaScriptSettings">
-    <option name="languageLevel" value="ES6" />
-  </component>
-  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.6 Conda" project-jdk-type="Python SDK" />
-</project>
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
deleted file mode 100644
index 8cb1deb..0000000
--- a/.idea/modules.xml
+++ /dev/null
@@ -1,8 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="ProjectModuleManager">
-    <modules>
-      <module fileurl="file://$PROJECT_DIR$/.idea/MTGCardDetector.iml" filepath="$PROJECT_DIR$/.idea/MTGCardDetector.iml" />
-    </modules>
-  </component>
-</project>
\ No newline at end of file
diff --git a/.idea/other.xml b/.idea/other.xml
deleted file mode 100644
index 640fd80..0000000
--- a/.idea/other.xml
+++ /dev/null
@@ -1,7 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="PySciProjectComponent">
-    <option name="PY_SCI_VIEW" value="true" />
-    <option name="PY_SCI_VIEW_SUGGESTED" value="true" />
-  </component>
-</project>
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
deleted file mode 100644
index 54c7a9b..0000000
--- a/.idea/workspace.xml
+++ /dev/null
@@ -1,280 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="ChangeListManager">
-    <list default="true" id="030dc7c9-2c79-444e-ac50-b591cb5968ea" name="Default Changelist" comment="" />
-    <option name="EXCLUDED_CONVERTED_TO_IGNORED" 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="FUSProjectUsageTrigger">
-    <session id="-739362152">
-      <usages-collector id="statistics.lifecycle.project">
-        <counts>
-          <entry key="project.open.time.11" value="1" />
-          <entry key="project.open.time.3" value="1" />
-          <entry key="project.opened" value="2" />
-        </counts>
-      </usages-collector>
-      <usages-collector id="statistics.file.extensions.open">
-        <counts>
-          <entry key="py" value="6" />
-        </counts>
-      </usages-collector>
-      <usages-collector id="statistics.file.types.open">
-        <counts>
-          <entry key="Python" value="6" />
-        </counts>
-      </usages-collector>
-      <usages-collector id="statistics.file.extensions.edit">
-        <counts>
-          <entry key="py" value="1688" />
-        </counts>
-      </usages-collector>
-      <usages-collector id="statistics.file.types.edit">
-        <counts>
-          <entry key="Python" value="1688" />
-        </counts>
-      </usages-collector>
-    </session>
-  </component>
-  <component name="FavoritesManager">
-    <favorites_list name="MTGCardDetector" />
-  </component>
-  <component name="FileEditorManager">
-    <leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
-      <file pinned="false" current-in-tab="true">
-        <entry file="file://$PROJECT_DIR$/card_detector.py">
-          <provider selected="true" editor-type-id="text-editor">
-            <state relative-caret-position="285">
-              <caret line="15" column="27" selection-start-line="15" selection-start-column="27" selection-end-line="15" selection-end-column="27" />
-              <folding>
-                <element signature="e#0#10#0" expanded="true" />
-              </folding>
-            </state>
-          </provider>
-        </entry>
-      </file>
-      <file pinned="false" current-in-tab="false">
-        <entry file="file://$PROJECT_DIR$/fetch_data.py">
-          <provider selected="true" editor-type-id="text-editor">
-            <state relative-caret-position="133">
-              <caret line="7" column="21" selection-start-line="7" selection-start-column="21" selection-end-line="7" selection-end-column="21" />
-              <folding>
-                <marker date="1534628908849" expanded="true" signature="253:258" ph="..." />
-              </folding>
-            </state>
-          </provider>
-        </entry>
-      </file>
-    </leaf>
-  </component>
-  <component name="FileTemplateManagerImpl">
-    <option name="RECENT_TEMPLATES">
-      <list>
-        <option value="Python Script" />
-      </list>
-    </option>
-  </component>
-  <component name="IdeDocumentHistory">
-    <option name="CHANGED_PATHS">
-      <list>
-        <option value="$PROJECT_DIR$/data/fetch_data.py" />
-        <option value="$PROJECT_DIR$/fetch_data.py" />
-        <option value="$PROJECT_DIR$/card_detector.py" />
-      </list>
-    </option>
-  </component>
-  <component name="JsBuildToolGruntFileManager" detection-done="true" sorting="DEFINITION_ORDER" />
-  <component name="JsBuildToolPackageJson" detection-done="true" sorting="DEFINITION_ORDER" />
-  <component name="JsGulpfileManager">
-    <detection-done>true</detection-done>
-    <sorting>DEFINITION_ORDER</sorting>
-  </component>
-  <component name="ProjectFrameBounds" extendedState="6">
-    <option name="x" value="156" />
-    <option name="y" value="30" />
-    <option name="width" value="1390" />
-    <option name="height" value="1000" />
-  </component>
-  <component name="ProjectView">
-    <navigator proportions="" version="1">
-      <foldersAlwaysOnTop value="true" />
-    </navigator>
-    <panes>
-      <pane id="ProjectPane">
-        <subPane>
-          <expand>
-            <path>
-              <item name="MTGCardDetector" type="b2602c69:ProjectViewProjectNode" />
-              <item name="MTGCardDetector" type="462c0819:PsiDirectoryNode" />
-            </path>
-            <path>
-              <item name="MTGCardDetector" type="b2602c69:ProjectViewProjectNode" />
-              <item name="MTGCardDetector" type="462c0819:PsiDirectoryNode" />
-              <item name="data" type="462c0819:PsiDirectoryNode" />
-            </path>
-          </expand>
-          <select />
-        </subPane>
-      </pane>
-      <pane id="Scope" />
-    </panes>
-  </component>
-  <component name="PropertiesComponent">
-    <property name="WebServerToolWindowFactoryState" value="false" />
-    <property name="last_opened_file_path" value="$PROJECT_DIR$" />
-    <property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
-  </component>
-  <component name="RecentsManager">
-    <key name="MoveFile.RECENT_KEYS">
-      <recent name="$PROJECT_DIR$" />
-    </key>
-  </component>
-  <component name="RunDashboard">
-    <option name="ruleStates">
-      <list>
-        <RuleState>
-          <option name="name" value="ConfigurationTypeDashboardGroupingRule" />
-        </RuleState>
-        <RuleState>
-          <option name="name" value="StatusDashboardGroupingRule" />
-        </RuleState>
-      </list>
-    </option>
-  </component>
-  <component name="RunManager">
-    <configuration default="true" type="PythonConfigurationType" factoryName="Python">
-      <module name="MTGCardDetector" />
-      <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="" />
-      <option name="IS_MODULE_SDK" value="false" />
-      <option name="ADD_CONTENT_ROOTS" value="true" />
-      <option name="ADD_SOURCE_ROOTS" value="true" />
-      <EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
-      <option name="SCRIPT_NAME" value="" />
-      <option name="PARAMETERS" value="" />
-      <option name="SHOW_COMMAND_LINE" value="true" />
-      <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="card_detector" type="PythonConfigurationType" factoryName="Python" temporary="true">
-      <module name="MTGCardDetector" />
-      <option name="INTERPRETER_OPTIONS" value="" />
-      <option name="PARENT_ENVS" value="true" />
-      <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" />
-      <EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
-      <option name="SCRIPT_NAME" value="$PROJECT_DIR$/card_detector.py" />
-      <option name="PARAMETERS" value="" />
-      <option name="SHOW_COMMAND_LINE" value="true" />
-      <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>
-    <recent_temporary>
-      <list>
-        <item itemvalue="Python.card_detector" />
-      </list>
-    </recent_temporary>
-  </component>
-  <component name="SvnConfiguration">
-    <configuration />
-  </component>
-  <component name="TaskManager">
-    <task active="true" id="Default" summary="Default task">
-      <changelist id="030dc7c9-2c79-444e-ac50-b591cb5968ea" name="Default Changelist" comment="" />
-      <created>1534624283148</created>
-      <option name="number" value="Default" />
-      <option name="presentableId" value="Default" />
-      <updated>1534624283148</updated>
-    </task>
-    <servers />
-  </component>
-  <component name="TodoView">
-    <todo-panel id="selected-file">
-      <is-autoscroll-to-source value="true" />
-    </todo-panel>
-    <todo-panel id="all">
-      <are-packages-shown value="true" />
-      <is-autoscroll-to-source value="true" />
-    </todo-panel>
-  </component>
-  <component name="ToolWindowManager">
-    <frame x="67" y="28" width="1853" height="1052" extended-state="6" />
-    <editor active="true" />
-    <layout>
-      <window_info active="true" content_ui="combo" id="Project" order="0" sideWeight="0.4983871" visible="true" weight="0.17542888" />
-      <window_info id="Structure" order="1" side_tool="true" weight="0.25" />
-      <window_info id="Favorites" order="2" sideWeight="0.5016129" side_tool="true" weight="0.25235197" />
-      <window_info anchor="bottom" id="Message" order="0" />
-      <window_info anchor="bottom" id="Find" order="1" />
-      <window_info anchor="bottom" id="Run" order="2" />
-      <window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
-      <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
-      <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
-      <window_info anchor="bottom" id="TODO" order="6" weight="0.33853006" />
-      <window_info anchor="bottom" id="Docker" order="7" show_stripe_button="false" />
-      <window_info anchor="bottom" id="Version Control" order="8" show_stripe_button="false" />
-      <window_info anchor="bottom" id="Database Changes" order="9" show_stripe_button="false" />
-      <window_info anchor="bottom" id="Event Log" order="10" side_tool="true" />
-      <window_info anchor="bottom" id="Terminal" order="11" />
-      <window_info anchor="bottom" id="Python Console" order="12" visible="true" weight="0.32850778" />
-      <window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
-      <window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
-      <window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
-      <window_info anchor="right" id="SciView" order="3" />
-      <window_info anchor="right" x="0" y="0" width="413" height="852" id="Documentation" order="4" side_tool="true" visible="true" weight="0.08190371" />
-      <window_info anchor="right" id="Database" order="5" />
-    </layout>
-  </component>
-  <component name="TypeScriptGeneratedFilesManager">
-    <option name="version" value="1" />
-  </component>
-  <component name="VcsContentAnnotationSettings">
-    <option name="myLimit" value="2678400000" />
-  </component>
-  <component name="editorHistoryManager">
-    <entry file="file://$USER_HOME$/.PyCharm2018.2/system/python_stubs/433684611/cv2/__init__.py">
-      <provider selected="true" editor-type-id="text-editor">
-        <state relative-caret-position="201">
-          <caret line="4594" column="4" selection-start-line="4594" selection-start-column="4" selection-end-line="4594" selection-end-column="10" />
-        </state>
-      </provider>
-    </entry>
-    <entry file="file://$PROJECT_DIR$/fetch_data.py">
-      <provider selected="true" editor-type-id="text-editor">
-        <state relative-caret-position="133">
-          <caret line="7" column="21" selection-start-line="7" selection-start-column="21" selection-end-line="7" selection-end-column="21" />
-          <folding>
-            <marker date="1534628908849" expanded="true" signature="253:258" ph="..." />
-          </folding>
-        </state>
-      </provider>
-    </entry>
-    <entry file="file://$PROJECT_DIR$/card_detector.py">
-      <provider selected="true" editor-type-id="text-editor">
-        <state relative-caret-position="285">
-          <caret line="15" column="27" selection-start-line="15" selection-start-column="27" selection-end-line="15" selection-end-column="27" />
-          <folding>
-            <element signature="e#0#10#0" expanded="true" />
-          </folding>
-        </state>
-      </provider>
-    </entry>
-  </component>
-</project>
\ No newline at end of file
diff --git a/card_detector.py b/card_detector.py
index 88af74d..81e3b6b 100644
--- a/card_detector.py
+++ b/card_detector.py
@@ -3,24 +3,61 @@
 import pandas as pd
 
 if __name__ == '__main__':
-    #img_test = cv2.imread('data/rtr-174-jarad-golgari-lich-lord.jpg')
-    #img_test = cv2.imread('data/cn2-78-queen-marchesa.png')
-    #img_test = cv2.imread('data/c16-143-burgeoning.png')
-    #img_test = cv2.imread('data/handOfCards.jpg')
-    img_test = cv2.imread('data/pro_tour_side.png')
+    # img_test = cv2.imread('data/rtr-174-jarad-golgari-lich-lord.jpg')
+    # img_test = cv2.imread('data/cn2-78-queen-marchesa.png')
+    img_test = cv2.imread('data/c16-143-burgeoning.png')
+    # img_test = cv2.imread('data/li38_handOfCards.jpg')
+    # img_test = cv2.imread('data/pro_tour_side.png')
     img_gray = cv2.cvtColor(img_test, cv2.COLOR_BGR2GRAY)
-    _, img_thresh = cv2.threshold(img_gray, 80, 255, cv2.THRESH_BINARY)
-    #cv2.imshow('original', img_test)
+    _, img_thresh = cv2.threshold(img_gray, 50, 255, cv2.THRESH_BINARY)
+    # cv2.imshow('original', img_test)
     cv2.imshow('threshold', img_thresh)
 
-    kernel = np.ones((4, 4), np.uint8)
+    kernel = np.ones((7, 7), np.uint8)
     img_dilate = cv2.dilate(img_thresh, kernel, iterations=1)
-    #img_erode = cv2.erode(img_thresh, kernel, iterations=1)
-    #img_open = cv2.morphologyEx(img_thresh, cv2.MORPH_OPEN, kernel)
+    # img_erode = cv2.erode(img_thresh, kernel, iterations=1)
+    # img_open = cv2.morphologyEx(img_thresh, cv2.MORPH_OPEN, kernel)
     img_close = cv2.morphologyEx(img_thresh, cv2.MORPH_CLOSE, kernel)
     cv2.imshow('dilated', img_dilate)
-    #cv2.imshow('eroded', img_erode)
-    #cv2.imshow('opened', img_open)
-    #cv2.imshow('closed', img_close)
+    # cv2.imshow('eroded', img_erode)
+    # cv2.imshow('opened', img_open)
+    # cv2.imshow('closed', img_close)
+    img_edge = cv2.Canny(img_dilate, 100, 200)
+    cv2.imshow('edge', img_edge)
 
-    cv2.waitKey(0)
+    lines = cv2.HoughLines(img_edge, 1, np.pi / 180, 200)
+    if lines is not None:
+        img_hough = cv2.cvtColor(img_dilate.copy(), cv2.COLOR_GRAY2BGR)
+        for line in lines:
+            rho, theta = line[0]
+            a = np.cos(theta)
+            b = np.sin(theta)
+            x0 = a * rho
+            y0 = b * rho
+            x1 = int(x0 + 1000 * (-b))
+            y1 = int(y0 + 1000 * (a))
+            x2 = int(x0 - 1000 * (-b))
+            y2 = int(y0 - 1000 * (a))
+            cv2.line(img_hough, (x1, y1), (x2, y2), (0, 0, 255), 2)
+        cv2.imshow('hough', img_hough)
+    else:
+        print('Hough couldn\'t find any lines')
+    '''
+    lines = cv2.HoughLinesP(img_edge, 1, np.pi / 180, 200, 50, 100)
+    if lines is not None:
+        img_hough = cv2.cvtColor(img_dilate.copy(), cv2.COLOR_GRAY2BGR)
+        for line in lines:
+            x1, y1, x2, y2 = line[0]
+            cv2.line(img_hough, (x1, y1), (x2, y2), (0, 0, 255), 3)
+        cv2.imshow('hough', img_hough)
+    else:
+        print('Hough couldn\'t find any lines')
+
+    img_contour, contours, hierchy = cv2.findContours(img_dilate, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
+    img_contour = cv2.cvtColor(img_contour, cv2.COLOR_GRAY2BGR)
+    if len(contours) > 0:
+        cv2.drawContours(img_contour, contours, -1, (0, 0, 255), 1)
+
+    cv2.imshow('contours', img_contour)
+    '''
+    cv2.waitKey(0
\ No newline at end of file

--
Gitblit v1.10.0