aboutsummaryrefslogtreecommitdiff
path: root/src/core.clj
diff options
context:
space:
mode:
authorPatrick Kingston <patrick@pkingston.xyz>2026-01-10 20:05:41 -0500
committerPatrick Kingston <patrick@pkingston.xyz>2026-01-10 20:05:41 -0500
commit4f0d2ec8e8237f6f4a5564b8165b2d13fd91e6c5 (patch)
tree744dfa98bf25dc54d03ca5f81a32efcc6f2b3dc1 /src/core.clj
Init repository, work out basic opencv
- Create repository - Init clojure project - Get feet wet with opencv
Diffstat (limited to 'src/core.clj')
-rw-r--r--src/core.clj79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/core.clj b/src/core.clj
new file mode 100644
index 0000000..c0a666f
--- /dev/null
+++ b/src/core.clj
@@ -0,0 +1,79 @@
+(ns core
+ (:require [opencv4.core :as cv]
+ [opencv4.video :as vid]
+ [opencv4.utils :as u]))
+
+
+
+(defn cell-occupied?
+ [^org.opencv.core.Mat frame col row]
+ (let [;; Get Region of Interest
+ roi-rect (get-cell-rect col row)
+ ;; Get a view into the main Mat
+ cell-mat (.submat frame roi-rect)
+ ;; Get the mean color for the cell
+ avg-color (cv/mean cell-mat)
+ ;; Sum the brightness of the channels
+ brightness (reduce + (.-val avg-color))]
+ ;; Clean up after ourselves
+ (.release cell-mat)
+ (> brightness 20.0)))
+
+#_(def capture (vid/capture-device 2))
+
+(def board-rows 20)
+(def board-cols 10)
+(def board-x 254)
+(def board-y 91)
+(def board-width 188)
+(def board-height 328)
+(def cell-height (/ board-height 20))
+(def cell-width (/ board-width 10))
+
+(defn get-board-rect
+ "Returns an OpenCV Rect for the whole board"
+ []
+ (cv/new-rect board-x
+ board-y
+ board-width
+ board-height))
+
+(do
+ (let [_foo (.release frame-mat)
+ frame-mat (u/mat-from-url "file:///home/patrick/Projects/TetCap/frame.jpg")
+ board-rect (get-board-rect)
+ submat (.submat frame-mat board-rect)
+ cell-sample-size 5
+ left-offset 9
+ top-offset 7]
+ #_(cv/imwrite submat "board.jpg") ;used for the plain board
+ (doseq [colidx (range board-cols)
+ rowidx (range board-rows)]
+ (let [^org.opencv.core.Point
+ topleft-point (cv/new-point
+ (+ left-offset (* colidx cell-width))
+ (+ top-offset (* rowidx cell-height)))
+ ^org.opencv.core.Point
+ bottomright-point (cv/new-point
+ (+ cell-sample-size (.-x topleft-point))
+ (+ cell-sample-size (.-y topleft-point)))
+ ^org.opencv.core.Scalar
+ rect-color (cv/new-scalar 0.0 0.0 255.0)]
+ (cv/rectangle submat topleft-point bottomright-point rect-color -1)))
+ (u/imshow submat)
+ (.release submat)))
+
+
+#_#_(.width frame-mat)
+(.height frame-mat)
+
+#_(cv/imwrite frame-mat "frame.jpg")
+
+#_(.release capture)
+#_(.release frame-mat)
+
+#_(doseq [i (range 10)]
+ (let [cap (vid/capture-device i)]
+ (when (.isOpened cap)
+ (println "Device at " i)
+ (.release cap))))