Python libccv-如何从内存中的字节读取图像
我正在尝试将libccv与Python结合使用(我已经使用SWIG创建了包装器)。我的设想如下:Python libccv-如何从内存中的字节读取图像,python,c,computer-vision,swig,Python,C,Computer Vision,Swig,我正在尝试将libccv与Python结合使用(我已经使用SWIG创建了包装器)。我的设想如下: 我有记忆中的图像 我想把这个图像(字节)传递给C函数,用SWIG为Python包装 C代码将使用libccv功能处理图像 Python代码: bytes = open("input.jpg","rb").read() result = ccvwrapper.use_ccv(bytes, 800, 600) C代码: int use_ccv(char *bytes, int width, int h
libccv
功能处理图像bytes = open("input.jpg","rb").read()
result = ccvwrapper.use_ccv(bytes, 800, 600)
C代码:
int use_ccv(char *bytes, int width, int height){
int status = 0;
ccv_enable_default_cache();
ccv_dense_matrix_t* image = 0;
ccv_read(bytes, &image, CCV_IO_ANY_RAW, width, height, width * 3);
if (image != 0)
{
//process the image
ccv_matrix_free(image);
status = 1;
}
ccv_drain_cache();
return status;
}
我尝试了ccv\u read
的type、rows、cols、scanline
参数的几种组合,但每次我得到SIGSEV或image
变量都是0
我不想使用ccv\u read
函数重载,这会占用文件路径,因为我不想引入将映像写入磁盘的开销
使用libccv从内存中读取图像的正确方法是什么?我已经弄明白了,诀窍是使用
fmemopen()
函数以流的形式打开内存,它还可以被接受文件*
指针的API读取
完整代码:
int* swt(char *bytes, int array_length, int width, int height){
ccv_dense_matrix_t* image = 0;
FILE *stream;
stream = fmemopen(bytes, array_length, "r");
if(stream != NULL){
int type = CCV_IO_JPEG_FILE | CCV_IO_GRAY;
int ctype = (type & 0xF00) ? CCV_8U | ((type & 0xF00) >> 8) : 0;
_ccv_read_jpeg_fd(stream, &image, ctype);
}
if (image != 0){
// here we have access to image in libccv format, so any processing can be done
}
}
Python的用法(使用SWIG构建C代码之后):
我在一篇博文中解释了所有细节:我明白了,诀窍是使用
fmemopen()
函数以流的形式打开内存,它还可以被接受文件*
指针的API读取
完整代码:
int* swt(char *bytes, int array_length, int width, int height){
ccv_dense_matrix_t* image = 0;
FILE *stream;
stream = fmemopen(bytes, array_length, "r");
if(stream != NULL){
int type = CCV_IO_JPEG_FILE | CCV_IO_GRAY;
int ctype = (type & 0xF00) ? CCV_8U | ((type & 0xF00) >> 8) : 0;
_ccv_read_jpeg_fd(stream, &image, ctype);
}
if (image != 0){
// here we have access to image in libccv format, so any processing can be done
}
}
Python的用法(使用SWIG构建C代码之后):
我在一篇博客文章中解释了所有细节:传递给C函数的指针是有效的(字节数ptr)?我是说,为了以防万一,精神检查可能很好。是的,它是有效的。我可以使用C函数中的
fwrite()
将这些字节写回文件,它会生成完全相同的图像。传递给C函数的指针是有效的(字节ptr)?我是说,为了以防万一,精神检查可能很好。是的,它是有效的。我可以使用C函数中的fwrite()
将这些字节写回文件,它会生成完全相同的图像。