getting Error when wrapping cvCreateTrackbar with CFFI?

Discussion of Common Lisp
Post Reply
joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

getting Error when wrapping cvCreateTrackbar with CFFI?

Post by joeish80829 » Tue Sep 03, 2013 11:46 pm

I need to understand how to get past this type of error

The value 0 is not of type SB-SYS:SYSTEM-AREA-POINTER. [Condition of type TYPE-ERROR]

I'm getting it because of the 0 in my create-trackbar call in the function below

this funtion works without the create-trackbar line

(defun display (filename)
"Open the image FILENAME and show it in a window."
(let* ((img (load-image filename 1))
(img-size (get-size img))
(dest (create-image img-size +ipl-depth-8u+ 3))
(bright 50)
(scalar (make-cv-scalar (- bright 50) (- bright 50) (- bright 50))))
(named-window "Display" 1)
(create-trackbar "brightness" "Display" bright 100 0)
(add-scalar img scalar dest)
(show-image "Display" dest)
(loop while (not (= (wait-key 0) 27)))
(release-image img)
(destroy-all-windows)))

My attempt at wrapping the fuction is below so hopefully you can see it and give me pointers I just need to know how to wrap the CvTrackbarCallback on_change=NULL part of the below definition then I can apply the knowledge you give me to future wraps....I've already added 20 new functions to cl-opencv and plan to make it complete but I can use your help

;; int cvCreateTrackbar(const char* trackbar_name, const char* window_name, int*
;; value, int count, CvTrackbarCallback on_change=NULL)

;; CvTrackbarCallback
(cffi:defctype cv-trackbar-callback :pointer)

(cffi:defcfun ("cvCreateTrackbar" %create-trackbar) :int
"Creates a trackbar and attaches it to the specified window."
(trackbar-name :string)
(window-name :string)
(value :int)
(count :int)
(on-change cv-trackbar-callback))

(defun create-trackbar (trackbar-name window-name value count &optional on-change)
;(if (= 0 on-change) (setf on-change (cffi:null-pointer)))
(%create-trackbar trackbar-name window-name value count on-change))

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: getting Error when wrapping cvCreateTrackbar with CFFI?

Post by Goheeca » Wed Sep 04, 2013 3:57 am

I'd wrap it as follows:

Code: Select all

(defun create-trackbar (trackbar-name window-name value count &optional (on-change (cffi:null-pointer)))
  (%create-trackbar trackbar-name window-name value count on-change))
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: getting Error when wrapping cvCreateTrackbar with CFFI?

Post by joeish80829 » Thu Sep 05, 2013 12:41 am

thanks for your suggention....I tried it and rewrote the code I used to call the function and now am getting a:

Unhandled memory fault at #xA.
[Condition of type SB-SYS:MEMORY-FAULT-ERROR]

here is where i define create-trackbar...i edited you suggestion which didn't work unless im using the function wrong:

;; CvTrackbarCallback
(cffi:defctype cv-trackbar-callback :pointer)

;; int cvCreateTrackbar(const char* trackbar_name, const char* window_name, int* value, int count, CvTrackbarCallback
;; on_change=NULL )
(cffi:defcfun ("cvCreateTrackbar" %create-trackbar) :int
"Creates a trackbar and attaches it to the specified window."
(trackbar-name :string)
(window-name :string)
(value :pointer)
(count :int)
(on-change cv-trackbar-callback))

(defun create-trackbar (trackbar-name window-name &optional (value (cffi:null-pointer)) count (on-change (cffi:null-pointer)))
;(if (= 0 value) (setf value (cffi:null-pointer)))
(%create-trackbar trackbar-name window-name value count on-change))

here is the code i use the funtion create-trackbar in:


;; callback function seems to work..I think im getting error with the cont variable in (create-trackbar "contrast" "Display" cont 100 change-cont)
(defun change-contrast (contrast)
(if (< contrast 10) (incf contrast 1)
(decf contrast 1)))

(defun image-create-trackbar (filename)
"Open the image FILENAME and show it in a window."
(let* ((img (load-image filename 1))
(img-size (get-size img))
(dest (create-image img-size +ipl-depth-8u+ 3))
(contrast 10)
(cont (cffi:make-pointer contrast))
(change-cont (cffi:make-pointer (change-contrast contrast)))
(scalar (make-cv-scalar (- contrast 10) (- contrast 10) (- contrast 10))))
(named-window "Display" 1)
(create-trackbar "contrast" "Display" cont 100 change-cont)
(add-scalar img scalar dest)
(show-image "Display" dest)
(loop while (not (= (wait-key 0) 27)))
(release-image img)
(destroy-all-windows)))

(image-create-trackbar "/home/w/Downloads/opencv-2.4.6.1/samples/c/100_0229.JPG" )

any help would be much appreciated I already added 29 new funtions to cl-opencv and plan to make it complete....so any help you give me will aid in a badass lisp cv library being available soon

Post Reply