[cffi-devel] How do i cast variables

Joeish W joeish80829 at yahoo.com
Sat Oct 26 01:45:39 UTC 2013


I do thank you for your input ,,,what you suggested worked great...Now however i got stopped on the very next step...I do like to keep these short so any info you need pls ask and will send asap

here is my code and im attempting to emulate the while loop in the link i posted for the c version of this code
it is the first item in the loop that i could use help with, the 


result = cvApproxPoly(contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0);

Im getting a "Bad argument (Input array is not a valid matrix) in cvPointSeqFromMat" error and the c code runs fine in emacs so i assume its the conversion. It might however be my CvSeq defcstruct....swig wrapped it but the c CvSeq struct  looks different from other c structs(i can post the code if needed) and the accessing of the cv-seq defcstructs foreign-slots is acting differently than in c..

im positive i have the function (approx-poly) wrapped right and chris showed me how to cast so i was hoping  you could look at my code and see if im doing things right as far as converting the c  code i posted to help me debug it..thanks again for all your extremely helpful advice on this matter


Note:.i use (< n 100) in the while loop because the contour-ptr is always a pointer so (pointerp contour-ptr) causes unreachable code errors... 


(defun find-contours-example (&optional (width *default-width*)
                  (height *default-height*))
  (let* ((window-name-1 "Original IMG - FIND-CONTOURS Example")
     (window-name-2 "Tracked IMG - FIND-CONTOURS Example")
     (img (load-image "/home/w/quicklisp/dists/quicklisp/software/cl-opencv-master/images/FindingContours.png" 1))
     (img-grayscale (create-image  (get-size img) +ipl-depth-8u+ 1))
     (storage (create-mem-storage 0))
         (n 0))
    (named-window window-name-1 +window-normal+)
    (named-window window-name-2 +window-autosize+)
    (move-window window-name-1 518 185)
    (move-window window-name-2 969 185)
    (cvt-color img img-grayscale +bgr2gray+)
    (threshold img-grayscale img-grayscale 128 255 +thresh-binary+)
    (with-foreign-object (contour-ptr '(:pointer (:struct cv-seq)))
      (with-foreign-object (result-ptr '(:pointer (:struct cv-seq)))
    (find-contours img-grayscale  storage contour-ptr
               (size-of cv-contour) +retr-list+ 
               +chain-approx-simple+ (point 0 0))
    (loop 
       while (< n 100)
       do ;(setf (mem-ref result-ptr `(:pointer (:struct cv-seq)))  (approx-poly contour-ptr (size-of cv-contour) storage +poly-approx-dp+ (* (contour-perimeter contour-ptr) 0.02) 0))
         (incf n))))
(show-image window-name-1 img)
(show-image window-name-2 img-grayscale)
(loop while (not (= (wait-key 0) 27)))
(destroy-all-windows)
(release-mem-storage storage)
(release-image img)
(release-image img-grayscale)))






On Friday, October 25, 2013 4:16 AM, Martin Simmons <martin at lispworks.com> wrote:
 
[ please post back to the list, not private email ]
>
>It looks like you are confused about the difference in CFFI between a pointer
>to a struct and a pointer to a pointer to a struct.  Unfortunately, CFFI
>pointers are untyped so you don't get errors by doing the wrong thing.
>
>To call cvFindContours as in the example, you need a pointer to a pointer to a
>struct (&contour in the C code).  Whenever you want to translate C code that
>uses &variable, you need to allocate a pointer to hold the value and then
>dereference it after calling the function.
>
>E.g. something like this (untested):
>
>(cffi:with-foreign-object (contour-ptr (:pointer (:struct cv-seq)))
>  ;; contour-ptr has foreign type (:pointer (:pointer (:struct cv-seq)))
>  (cv-find-contours ... contour-ptr ...)
>  (mem-ref contour-ptr '(:pointer (:struct cv-seq))))
>
>__Martin
>
>
>
>>>>>> On Wed, 23 Oct 2013 17:04:07 -0700 (PDT), Joeish W said:
>> 
>> well I'm trying to learn about Sequences in opencv. So this was practice at making the code at this link 
>> 
>> http://opencv-srf.blogspot.com/2011/09/object-detection-tracking-using-contours.html
>> 
>> under the 
>> 
>> Shape Detection & Tracking using Contours 
>> heading .  in that code there is this line CvSeq* contours;  since i dont know how to initialize a struct pointer in lisp as it is done there ive been using  CvSeq* contours = cvCreateSeq(0, sizeof(CvSeq),sizeof(CvPoint),storage); which also initializes a CvSeq* and works in that example. if you could advise me initializing structs I would be most grateful.....Basically I'm starting off by mastering the CvSeq parts of the code.
>> .
>> 
>> 
>> 
>> On Wednesday, October 23, 2013 3:23 AM, Martin Simmons <martin at lispworks.com> wrote:
>>  
>>>>>> On Tue, 22 Oct 2013 15:48:50 -0700 (PDT), Joeish W said:
>> >
>> >> 
>> >> take this for instance 
>> >> 
>> >> CvPoint points[2];
>> >> 
>> >> 
>> >> id like to be able to cast  points[2] to the CvPoint struct  more info here http://docs.opencv.org/modules/core/doc/old_basic_structures.html?highlight=cvpoint#CvPoint
>> >> 
>> >> 
>> >> here is my struct which works for other things
>> >> 
>> >> 
>> >> 
>> >> ;; (cffi:foreign-type-size '(:struct cv-point)) = 8 
>> >> (cffi:defcstruct cv-point 
>> >> (x :int) 
>> >> (y :int)) 
>> >> 
>> >> 
>> >> put in a defparameter you cant just go 
>> >> 
>> >> (defparameter points (cffi:foreign-alloc :int :count 2))
>> >> 
>> >> (defparameter a ((:struct cv-point) points))
>> >> 
>> >> 
>> >> you get an illegal function call error...any help is appreciated
>> >
>> >Your example is inconsistent -- in C, your points variable is an array of two
>> >CvPoints, but in Lisp it is an array of two ints.
>> >
>> >What are you really trying to do?
>> >
>> >I suggest you post the C code that you are trying to convert.  The opencv doc
>> >for CvPoint doesn't use casting or arrays of ints.
>> >
>> >__Martin
>> >
>> >
>> >
>> >
>> >
>> 
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/cffi-devel/attachments/20131025/27580a18/attachment.html>


More information about the cffi-devel mailing list