              40 40))
 
 (defn draw-centroids [{:keys [detector]}]
-  (q/push-style)
-  (q/no-stroke)
-  (q/fill 0 255 0 80)
-  (if (< 0 (.getBlobsNumber detector))
+  (when (and detector
+             (< 0 (.getBlobsNumber detector)))
+    (q/push-style)
+    (q/no-stroke)
+    (q/fill 0 255 0 80)
     (dorun
      (map #(draw-centroid detector %)
-          (range 0 (.getBlobsNumber detector)))))
-  (q/pop-style))
+          (range 0 (.getBlobsNumber detector))))
+    (q/pop-style)))
 
 (defn draw [state]
   (let [{:keys [p-image]} state]

  :on-close on-close
  :middleware [m/fun-mode])


(def PIX-CNT1 (* WIDTH HEIGHT 4))
(def PIX-CNT2 (* WIDTH HEIGHT))

(def BW-THRESH 200) ; between 0-255 (gray -> BW cut-off)
(def this (atom nil))

;; bArray is the temporary byte array buffer for OpenCV cv::Mat.
;; iArray is the temporary integer array buffer for PImage pixels.
(defn setup []
  (q/frame-rate 60)
  {:b-array (byte-array PIX-CNT1)
   :i-array (int-array PIX-CNT2)
