cgo 类型转换问题??

typedef struct cv_image_t {
unsigned char *data; ///< 图像数据指针
cv_pixel_format pixel_format; ///< 像素格式
int width; ///< 宽度(以像素为单位)
int height; ///< 高度(以像素为单位)
int stride; ///< 跨度, 即每行所占的字节数
cv_time_t time_stamp; ///< 时间戳
} cv_image_t;


buf := bytes.NewBuffer(b)
m, _, err := image.Decode(buf)
if err != nil {
fmt.Println("error: ", err.Error())
}

gary := image.NewGray(m.Bounds())
data := gary.Pix //[]uint8

var image = C.cv_image_t{data: *C.uchar(data), pixel_format: C.CV_PIX_FMT_GRAY8, width: C.int(12), height: C.int(12), stride: C.int(gary.Stride), time_stamp: C.cv_time_t{2, 3}}

怎么把 []uint8 转换成

unsigned char *data; ///< 图像数据指针

已邀请:

yangbt - https://github.com/ybtq 在健身房写代码的程序猿

赞同来自:

data := (*C.uchar)(unsafe.Pointer(&gray.pix[0]))

gary

赞同来自:

gray := image.NewGray(m.Bounds())
//data := gary.Pix //[]uint8
// // cuchar := uint8(st)
// var u C.uchar
// for k, v := range data {
// cuchar[k] = C.uchar(v)
// }
//fmt.Println(cuchar)
//carr := C.uchar(data[0])
data := (C.uchar)(unsafe.Pointer(&gray.Pix[0]))
//carr := (
C.uchar)(unsafe.Pointer(&data[0]))
var image = C.cv_image_t{data: data, pixel_format: C.CV_PIX_FMT_GRAY8, width: C.int(12), height: C.int(12), stride: C.int(gray.Stride), time_stamp: C.cv_time_t{2, 3}}


var count C.int
var face *C.cv_face_t
ret = C.mid_face_detector_detect(detector_handle, &image, &face, &count)
fmt.Println(ret)

panic: runtime error: cgo argument has Go pointer to Go pointer

goroutine 1 [running]:
main.main.func5(0xa2d010, 0xc420074180, 0xc42000e0a8, 0xc420016158, 0x38400)
/data/go/src/github.com/gary/sensekeeper_grpc/feature/feature.go:90 +0xcf
main.main()
/data/go/src/github.com/gary/sensekeeper_grpc/feature/feature.go:90 +0x4f5

yangbt - https://github.com/ybtq 在健身房写代码的程序猿

赞同来自:

C.uchar前面加个*号

gary

赞同来自:

(*C.uchar)(unsafe.Pointer(&gray.Pix[0])) 不行啊

yangbt - https://github.com/ybtq 在健身房写代码的程序猿

赞同来自:

你可以试试这个示例:


package main

import (
"fmt"
"unsafe"
)

/*
typedef unsigned char uint8;
uint8 pix[10];
*/
import "C"

func main() {
data := (*C.uchar)(unsafe.Pointer(&C.pix[0]))
fmt.Println(data)
}

gary

赞同来自:

var count C.int
var face *C.cv_face_t
ret = C.mid_face_detector_detect(detector_handle, &image, &face, &count)
fmt.Println(ret)
face是C的一个结构体指针我怎么指针加
......
face[0]
.......
face[1]
用go怎么指向下一个指针

yangbt - https://github.com/ybtq 在健身房写代码的程序猿

赞同来自:

大致如下:


/*
cv_face_t *NextFace(cv_face_t *cv)
{
if (cv == NULL)
{
return NULL;
}
return ++cv;
}
*/
import "C"

func main() {
var cv *C.cv_face_t;
cv_next := C.NextFace(cv)
}

弄人脸识别(or对齐)的?

gary

赞同来自:

谢谢!是的

要回复问题请先登录注册