【问题标题】:How do i wrap structs that contain struct pointers in CFFI?如何在 CFFI 中包装包含结构指针的结构?
【发布时间】:2013-10-02 03:33:23
【问题描述】:

IplImage struct documentation 描述了IplROI* roi 插槽,它似乎是指向核心types_c.h 头文件中定义的IplROI struct 的指针:

 typedef struct _IplROI
     {
         int  coi; /* 0 - no COI (all channels are selected)
                    , 1 - 0th channel is selected ...*/
         int  xOffset;
         int  yOffset;
         int  width;
         int  height;
      }IplROI;

但它也描述了 IplImage* maskROI 插槽,并且在核心 types_c.h 文件中没有 typedef 结构...

如果有人可以帮助我找到它,我将不胜感激,但我确实 grep 了整个 opencv 下载,但一无所获.....我试图用 lisp 包装 IplImage 结构 我用痛饮把它包起来,得到了这个

(cffi:defcstruct ipl-image
    (n-size :int)
    (id :int)
    (n-channels :int)
    (alpha-channel :int)
    (depth :int)
    (color-model :pointer) 
    (channel-seq :pointer) 
    (data-order :int)
    (origin :int)
    (align :int)
    (width :int)
    (height :int)
    (roi (:pointer (:struct ipl-roi)))
    (mask-roi :pointer)
    (image-id :pointer)
    (tile-info :pointer)
    (image-size :int)
    (image-data :string)
    (width-step :int)
    (border-mode :pointer)
    (border-const :pointer)
    (image-data-origin :string))

这里稍微改了一下

   (cffi:defcstruct ipl-image
       (n-size :int)
       (id :int)
       (n-channels :int)
       (alpha-channel :int)
       (depth :int)
       (color-model :int) ;;Ignored by OpenCV - was :pointer, 
                              changed to :int so the struct values 
                              would match OpenCV's
       (channel-seq :int) ;;Ignored by OpenCV - was :pointer, 
                              changed to :int so the struct values 
                              would match OpenCV's
       (data-order :int)
       (origin :int)
       (align :int)
       (width :int)
       (height :int)
       (roi (:pointer (:struct ipl-roi))) ;; changed so i could access (:struct ipl-roi)
       (mask-roi :pointer)
       (image-id :pointer)
       (tile-info :pointer)
       (image-size :int)
       (image-data :string)
       (width-step :int)
       (border-mode :pointer)
       (border-const :pointer)
       (image-data-origin :string))

所以当我在 emacs 中运行以下代码(显示输出)时,所有插槽值 将匹配来自完全相同代码的opencv输出,他们这样做,所以我可以访问 使用此行的 ipl-roi 结构和 ipl-image 结构

(roi (:pointer (:struct ipl-roi))) ;; 

因为我的直觉告诉我这是正确的方式

                        ; SLIME 2012-05-25
CL-OPENCV> (size-of '(:struct ipl-image))
128
CL-OPENCV> (defparameter img-size (make-size :width 640 :height 480))
(defparameter img (create-image img-size +ipl-depth-8u+ 3))

IMG
CL-OPENCV> (cffi:with-foreign-slots ((n-size id n-channels 
                         alpha-channel depth color-model 
                         channel-seq data-order origin  
                         align width height roi 
                         mask-roi image-id tile-info 
                         image-size image-data width-step 
                         border-mode border-const image-data-origin) 

                     img (:struct ipl-image))
         (format t "n-size = ~a~%id = ~a~%n-channels = 
                                ~a~%alpha-channel = ~a~%depth = ~a~%color-model = 
                                ~a~%channel-seq = ~a~%data-order = ~a~%origin = ~
                                a~%align = ~a~%width = ~a~%height = ~a~%roi = ~a~
                                %mask-roi = ~a~%image-id = ~a~%tile-info = ~a~%
                                image-size = ~a~%image-data = ~a~%width-step = 
                                ~a~%border-mode = ~a~%border-const = ~a~%image-
                                data-origin = ~a~%" 
             n-size id n-channels 
             alpha-channel depth color-model 
             channel-seq data-order origin  
             align width height roi 
             mask-rOI image-id tile-info 
             image-size image-data width-step 
             border-mode border-const image-data-origin))
n-size = 144
id = 0
n-channels = 3
alpha-channel = 0
depth = 8
color-model = 4343634
channel-seq = 5392194
data-order = 0
origin = 0
align = 4
width = 640
height = 480
roi = #.(SB-SYS:INT-SAP #X00000000)
mask-roi = #.(SB-SYS:INT-SAP #X00000000)
image-id = #.(SB-SYS:INT-SAP #X00000000)
tile-info = #.(SB-SYS:INT-SAP #X00000000)
image-size = 921600
image-data = 
width-step = 1920
border-mode = #.(SB-SYS:INT-SAP #X00000000)
border-const = #.(SB-SYS:INT-SAP #X00000000)
image-data-origin = NIL
NIL
CL-OPENCV> 

但是对于 IplImage* maskROI 插槽,没有要包装的结构,所以我希望有人可以给我一个关于如何包装 CFFI 中包含结构指针的结构的快速课程,如果我的想法正确的话

(roi (:pointer (:struct ipl-roi))) 

是正确的做法以及如何使用它

我真的很感激这方面的任何帮助

编辑

【问题讨论】:

    标签: c opencv struct common-lisp cffi


    【解决方案1】:

    这个问题太长了!

    无论如何,cffi 这个领域的官方文档非常糟糕。 我会推荐looking at this file for lots of examples of different kinds of structs with various contents

    例如:

    (cffi:defcstruct ai-node-anim  ;; this is a definition of a c struct
      (m-node-name (:struct ai-string))
      (m-num-position-keys :unsigned-int)
      (m-position-keys (:pointer (:struct ai-vector-key))) ;; here is a pointer to a struct
      (m-num-rotation-keys :unsigned-int)
      (m-rotation-keys (:pointer (:struct ai-quat-key))) ;; here is a pointer to a struct
      (m-num-scaling-keys :unsigned-int)
      (m-scaling-keys (:pointer (:struct ai-vector-key))) ;; here is a pointer to a struct
      (m-pre-state ai-anim-behaviour)
      (m-post-state ai-anim-behaviour))
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 2013-09-02
      相关资源
      最近更新 更多