aboutsummaryrefslogtreecommitdiff
path: root/src/core.clj
blob: c0a666f4ed6aa5d96f87819782ab96a1a607e155 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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))))