       (-> state
           (assoc-in [:old-gray-mat] old-gray-mat)
           (assoc-in [:old-corners] (update-corners! old-gray-mat (MatOfPoint2f.)))))
-    (let [{:keys [old-gray-mat old-corners new-gray-mat new-corners]} state]
+    (let [{:keys [old-gray-mat old-corners new-gray-mat new-corners lk-status]} state]
       (.copyTo new-gray-mat old-gray-mat)
       (if (= 0 (mod (q/frame-count) RESET-FRAMES))
-        (update-corners! old-gray-mat old-corners)
-        (.copyTo new-corners old-corners))
+        (do
+          ;; Reuse existing valid corners (still detected):
+          (refresh-corners! new-gray-mat lk-status new-corners old-corners)
+
+          ;; Brute-Force reset of corners:
+          #_(update-corners! new-gray-mat old-corners))
+        (do
+          (.copyTo new-corners old-corners)))
       state))) 
 
 (defn update-optical-flow [state]

(def WIDTH 640)
(def HEIGHT 480)

(def MAX-CORNERS 800)
(def BLANK-MASK (Mat.))
(def WIN-SIZE-W 10)
(def WIN-SIZE (Size. WIN-SIZE-W WIN-SIZE-W))
(def NEG-ONE-SIZE (Size. -1 -1))
(def TERM-CRITERIA (TermCriteria. (+ TermCriteria/EPS TermCriteria/COUNT) 20 0.03))

(def RESET-FRAMES 100)
(def PT-D 10)

;; pixCnt1 is the number of bytes in the pixel buffer

(ns videotest.optical-flow-crit
  (:require
   [quil.core :as q]
   [quil.middleware :as m]
   [clojure.set :as cset])
  (:import
   [org.opencv.highgui Highgui VideoCapture]
   [org.opencv.core CvType Mat MatOfByte MatOfFloat MatOfPoint MatOfPoint2f Size TermCriteria]
