Using Lisp's CFFI defcstruct with OpenCV's cvSize and CvSize
Posted: Sun Sep 29, 2013 8:21 pm
I have half successfully wrapped the OpenCV cvSize function
and I would like to acces the slots of the defctruct cv-size but could use help here are my steps:
i created a cl-opencv-glue.c file and adding this
then created cl-opencv-glue.h file and adding this:
then builded a
which builds successfully
then created a wrapper for the glue function like this:
then i can run this program successfully:
the
line successfully creates a size structure for the:
which is wrapped like this
now for testing i create a size struct at the repl(w/ output):
now id like to access slots of the size pointer so i can get size.width and size.height so i can create a size-width and size-height function in lisp
so i attempt to get at the slots by running at the repl(w/ output)
and get this
my defcstruct created by swig is
and the defined Opencv struct is this(along with the CV_INLINE for reference):
here is how they are defined in /home/w/Downloads/opencv-2.4.6.1/modules/core/include/opencv2/core/types_c.h:
so i change my glue.c to(and rebuild .so)
i re-open emacs and run the (init-image-header-example) function above and it runs so i rerun the above repl test(shown below w/ output)
compared to above test of
it is similar but different the 754484 changes evertime regardless of whether return is there or not I tried every variation of the defcstruct cv-size parameter i/e replacing the cv-size in
with all the below:
and get errors for all....(didnt include errors for sake of length of post(but will if neccesary)) i then try the below to access the slots (shown /w output)
but get a
after last entry
Any help on this would be much appreciated...
Code: Select all
CV_INLINE CvSize cvSize( int width, int height )
i created a cl-opencv-glue.c file and adding this
Code: Select all
CvSize cvSize_glue(int width, int height)
{
return cvSize(width, height);
}
Code: Select all
/* CvSize cvSize(int width, int height) */
CvSize cvSize_glue(int width, int height);
Code: Select all
/usr/local/lib/libcl-opencv-glue.so
then created a wrapper for the glue function like this:
Code: Select all
;; CvSize cvSize(int width, int height)
(cffi:defcfun ("cvSize_glue" size) (:pointer (:struct cv-size))
"Constructs CvSize structure."
(width :int)
(height :int))
Code: Select all
(defun init-image-header-example (&optional (width *default-width*)
(height *default-height*))
"Initializes an image header that was previously allocated."
(let* ((img-size (size width height))
(image (create-image img-size +ipl-depth-8u+ 3))
(image-re-init (init-image-header image img-size
+ipl-depth-8u+ 3
+ipl-origin-tl+ 4))
(window-name "INIT-IMAGE-HEADER Example"))
(named-window window-name +window-normal+)
(move-window window-name 702 285)
(create-data image-re-init)
(show-image window-name image-re-init)
(loop while (not (= (wait-key 0) 27)))
(release-image image-re-init)
(destroy-window window-name)))
Code: Select all
(img-size (size width height))
Code: Select all
(create-image img-size +ipl-depth-8u+ 3)
Code: Select all
;; IplImage* cvCreateImage(CvSize size, int depth, int channels)
(cffi:defcfun ("cvCreateImage" create-image) (:pointer (:struct ipl-image))
(size (:pointer (:struct cv-size)))
(depth :int)
(channels :int))
Code: Select all
CL-OPENCV> (defparameter size (size 1280 1024))
SIZE
CL-OPENCV> size
#.(SB-SYS:INT-SAP #X40000000500)
CL-OPENCV>
so i attempt to get at the slots by running at the repl(w/ output)
Code: Select all
CL-OPENCV> (cffi:with-foreign-object (size '(:struct cv-size))
;; Initialize the slots
;; Return a list with the coordinates
(cffi:with-foreign-slots ((width height) size (:struct cv-size))
(list width height)))
Output > (754484 0)
Code: Select all
(754484 0)
Code: Select all
(cffi:defcstruct cv-size
(width :int)
(height :int))
here is how they are defined in /home/w/Downloads/opencv-2.4.6.1/modules/core/include/opencv2/core/types_c.h:
Code: Select all
typedef struct CvSize
{
int width;
int height;
}
CvSize;
CV_INLINE CvSize cvSize( int width, int height )
{
CvSize s;
s.width = width;
s.height = height;
return s;
}
Code: Select all
CvSize cvSize_glue(int width, int height)
{
cvSize(width, height);
}
Code: Select all
(cffi:with-foreign-object (size '(:struct cv-size))
;; Initialize the slots
;; Return a list with the coordinates
(cffi:with-foreign-slots ((width height) size (:struct cv-size))
(list width height)))
Output > (338509 0)
Code: Select all
(754484 0)
Code: Select all
(cffi:defcstruct cv-size
(width :int)
(height :int))
Code: Select all
:struct cv-size, (:struct cv-size), (:pointer (:struct cv-size)), :struct cv-size
Code: Select all
CL-OPENCV> (defparameter size (size 1280 1024))
SIZE
CL-OPENCV> size
#.(SB-SYS:INT-SAP #X40000000500)
CL-OPENCV> (cffi:with-foreign-slots ((width height)
size (:struct cv-size))
(format t "width = ~a~%height = ~a~%"
width height ))
Code: Select all
[Condition of type SB-SYS:MEMORY-FAULT-ERROR]
Any help on this would be much appreciated...