- 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/
我在网上搜索但没有找到任何合适的文章解释如何使用 javascript 使用 WCF 服务,尤其是 WebScriptEndpoint。 任何人都可以对此给出任何指导吗? 谢谢 最佳答案 这是一篇关于
我正在编写一个将运行 Linux 命令的 C 程序,例如: cat/etc/passwd | grep 列表 |剪切-c 1-5 我没有任何结果 *这里 parent 等待第一个 child (chi
所以我正在尝试处理文件上传,然后将该文件作为二进制文件存储到数据库中。在我存储它之后,我尝试在给定的 URL 上提供文件。我似乎找不到适合这里的方法。我需要使用数据库,因为我使用 Google 应用引
我正在尝试制作一个宏,将下面的公式添加到单元格中,然后将其拖到整个列中并在 H 列中复制相同的公式 我想在 F 和 H 列中输入公式的数据 Range("F1").formula = "=IF(ISE
问题类似于this one ,但我想使用 OperatorPrecedenceParser 解析带有函数应用程序的表达式在 FParsec . 这是我的 AST: type Expression =
我想通过使用 sequelize 和 node.js 将这个查询更改为代码取决于在哪里 select COUNT(gender) as genderCount from customers where
我正在使用GNU bash,版本5.0.3(1)-发行版(x86_64-pc-linux-gnu),我想知道为什么简单的赋值语句会出现语法错误: #/bin/bash var1=/tmp
这里,为什么我的代码在 IE 中不起作用。我的代码适用于所有浏览器。没有问题。但是当我在 IE 上运行我的项目时,它发现错误。 而且我的 jquery 类和 insertadjacentHTMl 也不
我正在尝试更改标签的innerHTML。我无权访问该表单,因此无法编辑 HTML。标签具有的唯一标识符是“for”属性。 这是输入和标签的结构:
我有一个页面,我可以在其中返回用户帖子,可以使用一些 jquery 代码对这些帖子进行即时评论,在发布新评论后,我在帖子下插入新评论以及删除 按钮。问题是 Delete 按钮在新插入的元素上不起作用,
我有一个大约有 20 列的“管道分隔”文件。我只想使用 sha1sum 散列第一列,它是一个数字,如帐号,并按原样返回其余列。 使用 awk 或 sed 执行此操作的最佳方法是什么? Accounti
我需要将以下内容插入到我的表中...我的用户表有五列 id、用户名、密码、名称、条目。 (我还没有提交任何东西到条目中,我稍后会使用 php 来做)但由于某种原因我不断收到这个错误:#1054 - U
所以我试图有一个输入字段,我可以在其中输入任何字符,但然后将输入的值小写,删除任何非字母数字字符,留下“。”而不是空格。 例如,如果我输入: 地球的 70% 是水,-!*#$^^ & 30% 土地 输
我正在尝试做一些我认为非常简单的事情,但出于某种原因我没有得到想要的结果?我是 javascript 的新手,但对 java 有经验,所以我相信我没有使用某种正确的规则。 这是一个获取输入值、检查选择
我想使用 angularjs 从 mysql 数据库加载数据。 这就是应用程序的工作原理;用户登录,他们的用户名存储在 cookie 中。该用户名显示在主页上 我想获取这个值并通过 angularjs
我正在使用 autoLayout,我想在 UITableViewCell 上放置一个 UIlabel,它应该始终位于单元格的右侧和右侧的中心。 这就是我想要实现的目标 所以在这里你可以看到我正在谈论的
我需要与 MySql 等效的 elasticsearch 查询。我的 sql 查询: SELECT DISTINCT t.product_id AS id FROM tbl_sup_price t
我正在实现代码以使用 JSON。 func setup() { if let flickrURL = NSURL(string: "https://api.flickr.com/
我尝试使用for循环声明变量,然后测试cols和rols是否相同。如果是,它将运行递归函数。但是,我在 javascript 中执行 do 时遇到问题。有人可以帮忙吗? 现在,在比较 col.1 和
我举了一个我正在处理的问题的简短示例。 HTML代码: 1 2 3 CSS 代码: .BB a:hover{ color: #000; } .BB > li:after {
我是一名优秀的程序员,十分优秀!