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);
            }
        }
    }
}
相關文章
相關標籤/搜索