如何使用带有opencv struct

时间:2020-08-10 07:04:05

标签: python c++ opencv-python

c ++结构

typedef struct {
  unsigned char *data;          
  pixel_format_t pixel_format;  
  int width;                    
  int height;                   
  int stride;                   
  time_t time_stamp;  
} image_t;

c ++

// load image
string image_path = image_dir + file->d_name;
Mat image= imread(image_path);
if (!image.data) {
    printf("fail to read %s\n", image_path.c_str());
    continue;
}

image_t s_image = {image.data, PIX_FMT_BGR888, image.cols, image.rows, image.step};

一些函数,例如f(&image_t)

我在python中定义了ctypes:

class ST_IMAGE(Structure):
    pass

c_ubyte_p = POINTER(c_ubyte)
ST_IMAGE._fields_ = [
        ('data', c_ubyte_p),                
        ('pixel_format', c_int),            
        ('width', c_int),                   
        ('height', c_int),                  
        ('stride', c_int),                  
        ('time_stamp', St_Time),           
    ]  
def get_image(self, image_path):
        src = cv2.imread(img_path) 
        cvmat = cv.fromarray(src)
        
        cols = src.shape[1]
        rows = src.shape[0]
        channels = 0
        if 3==len(src.shape):
            channels = 3    
        src = np.asarray(src, dtype=np.uint8) 
        src_data = src.ctypes.data_as(ctypes.c_char_p)
        stride = 3 * cols
    
        return St_Image(src_data, Pixel_Format.ST_PIX_FMT_BGR888, cols, rows, stride)

然后我加载* .so和f()。 遇到错误分段错误(核心已转储)

似乎ndarray与Mat.data不匹配??? 如何使用严格数据?

0 个答案:

没有答案
相关问题