Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added cvAbsDiff and cvSub functions.

Added two OpenCV functions and test cases for those functions. Also
began adding cvAbsDiffS, but this is not working yet. Updated TODO with
some ideas for useful macros.
  • Loading branch information...
commit 095431a5c335c4c71763b515de2329904e15baf3 1 parent 7ab4e43
@jbromley jbromley authored
View
2  TODO
@@ -1,6 +1,8 @@
TODO
====
+* with-images macro to automatically release images when done
+* with-window macro to automatically destroy windows when done
* Figure out how to do callbacks for foreign functions in Lisp.
* Figure out how to better deal with passing structs by value.
* Figure out how to do better error handling of foreign function exceptions.
View
50 core.lisp
@@ -36,6 +36,21 @@
(i2 (+ (cv-rect-width r) (ash (cv-rect-height r) 32))))
(list i1 i2)))
+;; CvScalar
+;; In OpenCV a scalar is a struct whose single member is an array of
+;; four double values. We just use a list with values coerced to
+;; double. We provide a helper function to create a Lisp version of a
+;; scalar. The helper ensures that there are only four values in the
+;; scalar.
+(defun make-scalar (&rest args)
+ (mapcar #'(lambda (v) (coerce v 'double-float))
+ (cond ((> (length args) 4) (subseq args 0 4))
+ ((< (length args) 4)
+ (do ((new-args (reverse args)))
+ ((= (length new-args) 4) (reverse new-args))
+ (push 0 new-args)))
+ (t args))))
+
;; CvMat
(defctype cv-matrix :pointer)
@@ -60,6 +75,27 @@
;;; Operations on Arrays
+;; void cvAbsDiff(const CvArr* src1, const CvArr* src2, CvArr* dst)
+(defcfun ("cvAbsDiff" abs-diff) :void
+ "Calculate the absolute difference between elements in SRC1 and SRC2
+and store them in DEST."
+ (src1 cv-array)
+ (src2 cv-array)
+ (dest cv-array))
+
+;; void cvAbsDiffS(const CvArr* src, CvArr* dst, CvScalar value)
+(defcfun ("cvAbsDiffS" %abs-diff-scalar) :void
+ (src cv-array)
+ (dest cv-array)
+ (s1 :double)
+ (s2 :double)
+ (s3 :double)
+ (s4 :double))
+
+(defun abs-diff-scalar (src dest scalar)
+ "Calculate the absolute difference between elements of SRC and a fixed vector of values SCALAR. Store the result in DEST."
+ (apply #'%abs-diff-scalar src dest scalar))
+
;; void cvCopy(const CvArr* src, CvArr* dst, const CvArr* mask=NULL)
(defcfun ("cvCopy" %copy) :void
(src cv-array)
@@ -119,3 +155,17 @@ dimensions."
"Set the ROI of IMAGE to the rectangle RECT."
(let ((irect (cv-rect-to-int64s rect)))
(%set-image-roi image (first irect) (second irect))))
+
+;; void cvSub(const CvArr* src1, const CvArr* src2, CvArr* dst,
+;; const CvArr* mask=NULL)
+(defcfun ("cvSub" %subtract) :void
+ (src1 cv-array)
+ (src2 cv-array)
+ (dest cv-array)
+ (mask cv-array))
+
+(defun subtract (src1 src2 dest &optional (mask (null-pointer)))
+ "Subtract elements of SRC2 from SRC1 for the set bits in MASK and
+store the result in DEST. This operation is saturating for types with
+limited range."
+ (%subtract src1 src2 dest mask))
View
6 package.lisp
@@ -23,6 +23,7 @@
; #:cv-rect
; #:cv-scalar
+ #:make-scalar
#:cv-matrix
#:ipl-image
@@ -38,12 +39,17 @@
#:cv-array
;; core - operations on arrays
+ #:abs-diff ; TODO
+ #:abs-diff-scalar ; TODO
#:copy
#:create-image
#:get-size
#:release-image
#:reset-image-roi
#:set-image-roi
+ #:subtract ; TODO
+ #:subtract-scalar ; TODO
+ #:sub-r-scalar ; TODO
;; imgproc - image processing - miscellaneous image transformations
#:+thresh-binary+
View
5 test/package.lisp
@@ -5,5 +5,8 @@
(:export
#:display
#:show-camera
- #:show-camera-threshold))
+ #:show-camera-threshold
+ #:camera-frame-diff
+ #:camera-abs-diff
+ #:camera-subtract))
View
69 test/test.lisp
@@ -1,8 +1,8 @@
(in-package #:cl-opencv-test)
;;various settings that depend on the camera in use
-(defvar *default-width* 320)
-(defvar *default-height* 240)
+(defvar *default-width* 640)
+(defvar *default-height* 480)
(defvar *frames-per-second* 30)
(defvar *millis-per-frame* (round (/ 1000 *frames-per-second*)))
@@ -65,4 +65,67 @@
(release-image threshold)
(release-image grayscale))))
-
+(defun camera-frame-diff (&optional (camera-index 0) (width *default-width*)
+ (height *default-height*))
+ (with-capture (capture (create-camera-capture camera-index))
+ (let* ((img-size (make-cv-size :width width :height height))
+ (window-name "Frame Difference")
+ (images (list (create-image img-size +ipl-depth-8u+ 1)
+ (create-image img-size +ipl-depth-8u+ 1)))
+ (dest (create-image img-size +ipl-depth-8u+ 1)))
+ (set-capture-property capture +cap-prop-frame-width+
+ (cv-size-width img-size))
+ (set-capture-property capture +cap-prop-frame-height+
+ (cv-size-height img-size))
+ (named-window window-name)
+ (do ((frame (query-frame capture) (query-frame capture))
+ (frame-num 0 (1+ frame-num)))
+ ((plusp (wait-key *millis-per-frame*)) nil)
+ (convert-image frame (elt images (mod frame-num 2)))
+ (abs-diff (first images) (second images) dest)
+ (show-image window-name dest))
+ (destroy-window window-name)
+ (mapcar #'release-image images)
+ (release-image dest))))
+
+;; TODO fix camera-abs-diff
+(defun camera-abs-diff (&optional (camera-index 0) (width *default-width*)
+ (height *default-height*))
+ (with-capture (capture (create-camera-capture camera-index))
+ (let* ((img-size (make-cv-size :width width :height height))
+ (window-name "Frame Absolute Difference")
+ (dest (create-image img-size +ipl-depth-8u+ 3))
+ (scalar (make-scalar 128.0 128.0 128.0)))
+ (set-capture-property capture +cap-prop-frame-width+
+ (cv-size-width img-size))
+ (set-capture-property capture +cap-prop-frame-height+
+ (cv-size-height img-size))
+ (named-window window-name)
+ (do ((frame (query-frame capture) (query-frame capture)))
+ ((plusp (wait-key *millis-per-frame*)) nil)
+ (abs-diff-scalar frame dest scalar)
+ (show-image window-name dest))
+ (destroy-window window-name)
+ (release-image dest))))
+
+(defun camera-subtract (&optional (camera-index 0) (width *default-width*)
+ (height *default-height*))
+ (with-capture (capture (create-camera-capture camera-index))
+ (let* ((img-size (make-cv-size :width width :height height))
+ (window-name "Frame Subtract")
+ (last-frame (create-image img-size +ipl-depth-8u+ 3))
+ (dest (create-image img-size +ipl-depth-8u+ 3)))
+ (set-capture-property capture +cap-prop-frame-width+
+ (cv-size-width img-size))
+ (set-capture-property capture +cap-prop-frame-height+
+ (cv-size-height img-size))
+ (named-window window-name)
+ (do ((frame (query-frame capture) (query-frame capture)))
+ ((plusp (wait-key *millis-per-frame*)) nil)
+ (subtract frame last-frame dest)
+ (show-image window-name dest)
+ (copy frame last-frame))
+ (destroy-window window-name)
+ (release-image last-frame)
+ (release-image dest))))
+
Please sign in to comment.
Something went wrong with that request. Please try again.