From 4f0d2ec8e8237f6f4a5564b8165b2d13fd91e6c5 Mon Sep 17 00:00:00 2001 From: Patrick Kingston Date: Sat, 10 Jan 2026 20:05:41 -0500 Subject: Init repository, work out basic opencv - Create repository - Init clojure project - Get feet wet with opencv --- src/core.clj | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/core.clj (limited to 'src/core.clj') 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)))) -- cgit v1.2.3