(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))))