代码之家  ›  专栏  ›  技术社区  ›  Marcin Zablocki

libccv-如何从内存中的字节读取图像

  •  1
  • Marcin Zablocki  · 技术社区  · 8 年前

    1. 我想把这个图像(字节)传递给C函数,用SWIG为Python包装。
    2. 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 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;
    }
    

    我试过几种组合 type, rows, cols, scanline ccv_read 但每次我得到 西格夫 image 变量为 0 .

    我不想使用 ccv_读取 函数重载,需要文件路径,因为我不想引入将图像写入磁盘的开销。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Marcin Zablocki    8 年前

    我已经弄明白了, fmemopen() FILE* 指针。

    完整代码:

    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
        }
    }
    

    import ccvwrapper
    bytes = open("test_input.jpg", "rb").read()
    results = ccvwrapper.swt(bytes, len(bytes), 1024, 1360) # width:1024, height:1360
    

    http://zablo.net/blog/post/stroke-width-transform-swt-python