opencv读取图像输入到tensorflow模型中进行运算【cpp】

void TransformMatToTensor(const cv::Mat &image, Tensor &input_tensor, int input_width, int input_height)
{
    
    int Channel = image.channels();

    int Stride = input_width * Channel;
    float *source_data = (float *)image.data;

    auto input_tensor_mapped = input_tensor.tensor<float, 4>();

    LOG(INFO) << "input_tensorinput_tensorinput_tensor_shape_before : " << input_tensor.shape();
    for (int y = 0; y < input_height; ++y)
    {
        float *source_row = source_data + y * Stride;
        for (int x = 0; x < input_width; ++x)
        {
            float *source_pixel = source_row + (x * Channel);
            for (int ch = 0; ch < Channel; ++ch)
            {
                input_tensor_mapped(0, y, x, ch) = (float)*(source_pixel + ch);
            }
        }
    }
}

 

posted @ 2018-12-03 16:21  骑着单车滑翔  阅读(363)  评论(0编辑  收藏  举报