- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的印象是 CFFI 不能按值传递结构,但 CFFI 文档说:
To pass or return a structure by value to a function, load the cffi-libffi system and specify the structure as
(:struct structure-name)
. To pass or return the pointer, you can use either:pointer
or(:pointer (:struct structure-name))
.
get-size
,它是这个 opencv 函数的包装器:
CvSize cvGetSize(const CvArr* arr)
cvGetSize
:
(defmacro make-structure-serializers (struct slot1 slot2)
"Create a serialization and deserialization function for the
structure STRUCT with integer slots SLOT1 and SLOT2. These functions
will pack and unpack the structure into an INT64."
(let ((pack-fn (intern (concatenate 'string (string struct)
(string '->int64))))
(slot1-fn (intern (concatenate 'string (string struct) "-"
(string slot1))))
(slot2-fn (intern (concatenate 'string (string struct) "-"
(string slot2))))
(unpack-fn (intern (concatenate 'string (string 'int64->)
(string struct))))
(make-fn (intern (concatenate 'string (string 'make-)
(string struct)))))
`(progn
(defun ,pack-fn (s)
(+ (,slot1-fn s) (ash (,slot2-fn s) 32)))
(defun ,unpack-fn (n)
(,make-fn ,slot1 (logand n #x00000000ffffffff)
,slot2 (ash n -32))))))
;; CvSize - Input = (defparameter a (make-size :width 640 :height 480)) Output = #S(SIZE :WIDTH 640 :HEIGHT 480) for
;; following the two.
(defstruct size (width 0) (height 0))
(make-structure-serializers :size :width :height)
;; CvSize cvGetSize(const CvArr* arr)
(cffi:defcfun ("cvGetSize" %get-size) :int64
(arr cv-array))
(defun get-size (arr)
"Get the dimensions of the OpenCV array ARR. Return a size struct with the
dimensions."
(let ((nsize (%get-size arr)))
(int64->size nsize)))
cvGetSize
结构
CvSize
按值(value)?
(:struct structure-name)
”和“使用: ” pointer or (:pointer (:struct structure-name))""传递或返回指针。"
cvGetSize
来详细说明如何做到这一点。包装器:
;; CvSize cvGetSize(const CvArr* arr)
(cffi:defcfun ("cvGetSize" %get-size) :int64
(arr cv-array))
(defun get-size (arr)
"Get the dimensions of the OpenCV array ARR. Return a size struct with the
dimensions."
(let ((nsize (%get-size arr)))
(int64->size nsize)))
CL-OPENCV> (asdf:oos 'asdf:load-op :cffi-libffi)
#<ASDF:LOAD-OP NIL {10076CCF13}>
NIL
CL-OPENCV> (cffi:defcstruct cv-size
(width :int)
(height :int))
(:STRUCT CV-SIZE)
CL-OPENCV>
(defcfun ("cvGetSize" %get-size) (:struct cv-size)
(arr cv-array))
; in: DEFCFUN ("cvGetSize" %GET-SIZE)
; ("cvGetSize" CL-OPENCV::%GET-SIZE)
;
; caught ERROR:
; illegal function call
;
; compilation unit finished
; caught 1 ERROR condition
Execution of a form compiled with errors.
Form:
("cvGetSize" %GET-SIZE)
Compile-time error:
illegal function call
[Condition of type SB-INT:COMPILED-PROGRAM-ERROR]
Restarts:
0: [RETRY] Retry SLIME REPL evaluation request.
1: [*ABORT] Return to SLIME's top level.
2: [ABORT] Abort thread (#<THREAD "repl-thread" RUNNING {1007BA8063}>)
Backtrace:
0: ((LAMBDA ()))
[No Locals]
1: (SB-INT:SIMPLE-EVAL-IN-LEXENV ("cvGetSize" %GET-SIZE) #<NULL-LEXENV>)
Locals:
SB-DEBUG::ARG-0 = ("cvGetSize" %GET-SIZE)
SB-DEBUG::ARG-1 = #<NULL-LEXENV>
2: (SB-INT:SIMPLE-EVAL-IN-LEXENV (DEFCFUN ("cvGetSize" %GET-SIZE) (:STRUCT CV-SIZE) (ARR CV-ARRAY)) #<NULL-LEXENV>)
Locals:
SB-DEBUG::ARG-0 = (DEFCFUN ("cvGetSize" %GET-SIZE) (:STRUCT CV-SIZE) (ARR CV-ARRAY))
SB-DEBUG::ARG-1 = #<NULL-LEXENV>
3: (EVAL (DEFCFUN ("cvGetSize" %GET-SIZE) (:STRUCT CV-SIZE) (ARR CV-ARRAY)))
Locals:
(ql:quickload "lisp-unit")
(in-package :gsl)
(lisp-unit:run-tests)
To load "lisp-unit":
Load 1 ASDF system:
lisp-unit
Loading "lisp-unit"
..................................
Unit Test Summary
| 4023 assertions total
| 4022 passed
| 1 failed
| 0 execution errors
| 0 missing tests
#<TEST-RESULTS-DB Total(4023) Passed(4022) Failed(1) Errors(0)>
(defcfun ("cvGetSize" %get-size) cv-size
(arr cv-array))
Execution of a form compiled with errors.
Form:
("cvGetSize" %GET-SIZE)
Compile-time error:
CL-OPENCV> ;; ;(cffi:foreign-type-size '(:struct ipl-image)) = 144
(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)
(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))
output>(:STRUCT IPL-IMAGE)
;; IplImage* cvCreateImage(CvSize size, int depth, int channels)
(cffi:defcfun ("cvCreateImage" %create-image) (:struct ipl-image)
(size :int64)
(depth :int)
(channels :int))
(defun create-image (size depth channels)
"Create an image with dimensions given by SIZE, DEPTH bits per
channel, and CHANNELS number of channels."
(let ((nsize (size->int64 size)))
(%create-image nsize depth channels)))
(defparameter img-size (make-size :width 640 :height 480))
(defparameter img (create-image img-size +ipl-depth-8u+ 1))
(defparameter img-size (make-size :width 640 :height 480))
(defparameter img (create-image img-size +ipl-depth-8u+ 1))
(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))
(cffi:mem-ref img :char )
(format t "n-size ~a ~%" n-size)
(format t "id ~a ~%" id)
(format t "n-channels ~a ~%" n-channels)
(format t "alpha-channel ~a ~%" alpha-channel)
(format t "depth ~a ~%" depth)
(format t "color-model ~a ~%" color-model)
(format t "channel-seq ~a ~%" channel-seq)
(format t "data-order ~a ~%" data-order)
(format t "origin ~a ~%" origin)
(format t "align ~a ~%" align)
(format t "width ~a ~%" width)
(format t "height ~a ~%" height)
(format t "roi ~a ~%" roi)
(format t "mask-roi ~a ~%" mask-roi)
(format t "image-id ~a ~%" image-id)
(format t "tile-info ~a ~%" tile-info)
(format t "image-size ~a ~%" image-size)
(format t "image-data ~a ~%" image-data)
(format t "width-step ~a ~%" width-step)
(format t "border-mode ~a ~%" border-mode)
(format t "border-const ~a ~%" border-const)
(format t "image-data-origin ~a ~%" image-data-origin))
output>
n-size 144
id 0
n-channels 1
alpha-channel 0
depth 8
color-model #.(SB-SYS:INT-SAP #X59415247)
channel-seq #.(SB-SYS:INT-SAP #X400000000)
data-order 640
origin 480
align 0
width 0
height 0
roi #.(SB-SYS:INT-SAP #X00000000)
mask-roi #.(SB-SYS:INT-SAP #X00000000)
image-id #.(SB-SYS:INT-SAP #X0004B000)
tile-info #.(SB-SYS:INT-SAP #X7FFFF7F04020)
image-size 640
image-data NIL
width-step 0
border-mode #.(SB-SYS:INT-SAP #X00000000)
border-const #.(SB-SYS:INT-SAP #X00000000)
image-data-origin
color-model #.(SB-SYS:INT-SAP #X59415247)
IplImage* img=cvCreateImage(cvSize(640,480), IPL_DEPTH_8U, 3);
cout << "colorModel = " << endl << " " << img->colorModel << endl << endl;
output> colorModel =
RGB
CL-OPENCV> (asdf:oos 'asdf:load-op :cffi-libffi)
#<ASDF:LOAD-OP NIL {1006D7B1F3}>
NIL
CL-OPENCV>
;; ;(cffi:foreign-type-size '(:struct cv-size)) = 8
(cffi:defcstruct cv-size
(width :int)
(height :int))
;; CvSize cvGetSize(const CvArr* arr)
(cffi:defcfun ("cvGetSize" %get-size) (:struct cv-size)
(arr cv-array))
STYLE-WARNING: redefining CL-OPENCV::%GET-SIZE in DEFUN
%GET-SIZE
CL-OPENCV>
(defparameter img-size (make-size :width 640 :height 480))
(defparameter img (create-image img-size +ipl-depth-8u+ 1))
IMG
CL-OPENCV>
(defparameter size (%get-size img))
SIZE
CL-OPENCV> size
(HEIGHT 480 WIDTH 640)
CL-OPENCV>
The value (HEIGHT 480 WIDTH 640)
is not of type
SB-SYS:SYSTEM-AREA-POINTER.
[Condition of type TYPE-ERROR]
CL-OPENCV> (defun get-size (arr)
"Get the dimensions of the OpenCV array ARR. Return a size struct with the
dimensions."
(cffi:with-foreign-slots ((width height) (%get-size arr) (:struct cv-size))
(make-size :width width :height height)))
STYLE-WARNING: redefining CL-OPENCV:GET-SIZE in DEFUN
GET-SIZE
CL-OPENCV>
(defparameter img-size (make-size :width 640 :height 480))
(defparameter img (create-image img-size +ipl-depth-8u+ 1))
IMG
CL-OPENCV>
(defparameter size (get-size img))
The value (HEIGHT 480 WIDTH 640)
is not of type
SB-SYS:SYSTEM-AREA-POINTER.
[Condition of type TYPE-ERROR]
(defparameter size (get-size img))
CL-OPENCV>
;; ;(cffi:foreign-type-size '(:struct cv-size)) = 8
(cffi:defcstruct cv-size
(width :int)
(height :int))
;; CvSize cvGetSize(const CvArr* arr)
(cffi:defcfun ("cvGetSize" %get-size) (:struct cv-size)
(arr cv-array))
STYLE-WARNING: redefining CL-OPENCV::%GET-SIZE in DEFUN
%GET-SIZE
CL-OPENCV> (defparameter capture (create-camera-capture 0))
CAPTURE
CL-OPENCV> (defparameter frame (query-frame capture))
FRAME
CL-OPENCV>
(defparameter size (%get-size frame))
SIZE
CL-OPENCV> size
(HEIGHT 480 WIDTH 640)
CL-OPENCV> (cffi:with-foreign-slots ((width height) size (:struct cv-size))
(list width height ))
The value (HEIGHT 480 WIDTH 640)
is not of type
SB-SYS:SYSTEM-AREA-POINTER.
[Condition of type
(HEIGHT 480 WIDTH 640)
CL-OPENCV> (first size)
HEIGHT
(defparameter capture (create-camera-capture 0))
(defparameter frame (query-frame capture))
;; IplImage* cvCreateImage(CvSize size, int depth, int channels)
(cffi:defcfun ("cvCreateImage" %create-image) ipl-image
(size cv-size)
(depth :int)
(channels :int))
(defun get-size (arr)
"Get the dimensions of the OpenCV array ARR. Return a size struct with the
dimensions."
(setf arr (%get-size arr))
(make-size :width (cadddr arr) :height (cadr arr)))
here is the repl output :
; SLIME 2012-05-25
CL-OPENCV> ;; CvSize cvGetSize(const CvArr* arr)
(cffi:defcfun ("cvGetSize" get-size) (:pointer (:struct cv-size))
(arr cv-array))
STYLE-WARNING: redefining CL-OPENCV:GET-SIZE in DEFUN
GET-SIZE
CL-OPENCV> ;; IplImage* cvCreateImage(CvSize size, int depth, int channels)
(cffi:defcfun ("cvCreateImage" create-image) (:pointer (:struct ipl-image))
(size (:pointer (:struct ipl-image)))
(depth :int)
(channels :int))
STYLE-WARNING: redefining CL-OPENCV:CREATE-IMAGE in DEFUN
CREATE-IMAGE
CL-OPENCV> (defun detect-red-objects (&optional (camera-index 0))
"Uses IN-RANGE-SCALAR to detect red objects"
(with-capture (capture (create-camera-capture camera-index))
(let ((window-name-1 "Video")
(window-name-2 "Ball"))
(named-window window-name-1)
(named-window window-name-2)
(move-window window-name-1 290 225)
(move-window window-name-2 940 225)
(do* ((frame (query-frame capture) (query-frame capture))
(img (clone-image frame))
(frame (clone-image img))
(img-size (get-size frame))
(img-hsv (create-image img-size +ipl-depth-8u+ 3))
(img-hsv-size (get-size img-hsv))
(img-thresh (create-image img-hsv-size +ipl-depth-8u+ 1))
(scalar-1 (make-cv-scalar 170.0 160.0 60.0))
(scalar-2 (make-cv-scalar 180.0 256.0 256.0)))
((plusp (wait-key *millis-per-frame*)) nil)
(smooth frame frame +gaussian+ 3 3)
(cvt-color frame img-hsv +bgr2hsv+)
(in-range-s img-hsv scalar-1 scalar-2 img-thresh)
(smooth img-thresh img-thresh +gaussian+ 3 3)
(show-image window-name-1 frame)
(show-image window-name-2 img-thresh))
(destroy-all-windows))))
DETECT-RED-OBJECTS
(the function detect-red-objects runs btw!...
I messed up the struct on create-image the first time but it still ran...weird...but it runs when put the create-image struct back to cv-size....so no prob there...here is revised repl output
; SLIME 2012-05-25
CL-OPENCV> ;; CvSize cvGetSize(const CvArr* arr)
(cffi:defcfun ("cvGetSize" get-size) (:pointer (:struct cv-size))
(arr cv-array))
STYLE-WARNING: redefining CL-OPENCV:GET-SIZE in DEFUN
GET-SIZE
CL-OPENCV> ;; 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))
STYLE-WARNING: redefining CL-OPENCV:CREATE-IMAGE in DEFUN
CREATE-IMAGE
CL-OPENCV> (defun detect-red-objects (&optional (camera-index 0))
"Uses IN-RANGE-SCALAR to detect red objects"
(with-capture (capture (create-camera-capture camera-index))
(let ((window-name-1 "Video")
(window-name-2 "Ball"))
(named-window window-name-1)
(named-window window-name-2)
(move-window window-name-1 290 225)
(move-window window-name-2 940 225)
(do* ((frame (query-frame capture) (query-frame capture))
(img (clone-image frame))
(frame (clone-image img))
(img-size (get-size frame))
(img-hsv (create-image img-size +ipl-depth-8u+ 3))
(img-hsv-size (get-size img-hsv))
(img-thresh (create-image img-hsv-size +ipl-depth-8u+ 1))
(scalar-1 (make-cv-scalar 170.0 160.0 60.0))
(scalar-2 (make-cv-scalar 180.0 256.0 256.0)))
((plusp (wait-key *millis-per-frame*)) nil)
(smooth frame frame +gaussian+ 3 3)
(cvt-color frame img-hsv +bgr2hsv+)
(in-range-s img-hsv scalar-1 scalar-2 img-thresh)
(smooth img-thresh img-thresh +gaussian+ 3 3)
(show-image window-name-1 frame)
(show-image window-name-2 img-thresh))
(destroy-all-windows))))
DETECT-RED-OBJECTS
(cffi:defcstruct (cv-size :class cv-size-type)
(width :int)
(height :int))
(defmethod cffi:translate-from-foreign (p (type cv-size-type))
(let ((plist (call-next-method)))
(make-size :width (getf plist 'width)
:height (getf plist 'height))))
;; CvSize cvGetSize(const CvArr* arr)
(cffi:defcfun ("cvGetSize" get-size) (:struct cv-size)
(arr cv-arr))
;; IplImage* cvCreateImage(CvSize size, int depth, int channels)
(cffi:defcfun ("cvCreateImage" %create-image) ipl-image
(size :int64)
(depth :int)
(channels :int))
(defun create-image (size depth channels)
"Create an image with dimensions given by SIZE, DEPTH bits per
channel, and CHANNELS number of channels."
(let ((nsize (size->int64 size)))
(%create-image nsize depth channels)))
(DEFUN SIZE->INT64 (S) (+ (SIZE-WIDTH S) (ASH (SIZE-HEIGHT S) 32)))
(defmethod cffi:translate-from-foreign (p (type cv-size-type))
(let ((plist (call-next-method)))
(make-size :width (getf plist 'width)
:height (getf plist 'height))))
最佳答案
要“加载 cffi-libffi 系统”,您需要在库的 .asd
中将其指定为依赖项。文件。 (注意:cffi-libffi 需要在您的系统上安装 C 库 libffi。)
才能使用(:struct structure-name)
, 你需要先用 cffi:defcstruct
定义结构体,像这样(我在这里假设 cffi:defcstruct
、 cffi:defcfun
和 cffi:with-foreign-slots
是在当前包中导入的):
(defcstruct cv-size
(width :int)
(height :int))
(:struct cv-size)
在
defcfun
像这样:
(defcfun ("cvGetSize" %get-size) (:struct cv-size)
(arr cv-array))
get-size
对于传值结构。
get-size
像这样:
(defun get-size (arr)
"Get the dimensions of the OpenCV array ARR. Return a size struct with the
dimensions."
(let ((%size (%get-size arr)))
(make-size :width (getf %size 'width)
:height (getf %size 'height))))
translate-from-foreign
方法,以便它直接创建结构,而无需创建中间 plist:
(defmethod cffi:translate-from-foreign (p (type cv-size-type))
(with-foreign-slots ((width height) p (:struct cv-size))
(make-size :width width :height height)))
关于c - 使用 cffi-libffi 按值传递结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18971611/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!