gpt4 book ai didi

c - 我如何在 CFFI 中包装包含结构指针的结构?

转载 作者:太空宇宙 更新时间:2023-11-04 04:47:48 24 4
gpt4 key购买 nike

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 结构...

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

(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 插槽,没有要包装的结构,所以我希望有人能给我一个快速的类(class),告诉我如何在 CFFI 中包装包含结构指针的结构,如果我认为这一行是正确的

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

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

我真的很感激任何帮助

编辑

最佳答案

天哪,这个问题太长了!

无论如何,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))

希望对您有所帮助!

关于c - 我如何在 CFFI 中包装包含结构指针的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19129566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com