How do I pass an OpenCV Mat into a C++ Tensorflow graph?

前端 未结 4 1851
天涯浪人
天涯浪人 2020-12-14 10:08

In Tensorflow C++ I can load an image file into the graph using

tensorflow::Node* file_reader =  tensorflow::ops::ReadFile(tensorflow::ops::Const(IMAGE_FILE_         


        
4条回答
  •  隐瞒了意图╮
    2020-12-14 10:09

    Here is complete example to read and feed:

    Mat image;
    image = imread("flowers.jpg", CV_LOAD_IMAGE_COLOR);
    cv::resize(image, image, cv::Size(input_height, input_width), 0, 0, CV_INTER_CUBIC);
    
    int depth = 3;
    tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT,
                                    tensorflow::TensorShape({1, image.rows, image.cols, depth}));
    
    for (int y = 0; y < image.rows; y++) {
        for (int x = 0; x < image.cols; x++) {
            Vec3b pixel = image.at(y, x);
    
            input_tensor_mapped(0, y, x, 0) = pixel.val[2]; //R
            input_tensor_mapped(0, y, x, 1) = pixel.val[1]; //G
            input_tensor_mapped(0, y, x, 2) = pixel.val[0]; //B
        }
    }
    
    auto result = Sub(root.WithOpName("subtract_mean"), input_tensor, {input_mean});
    ClientSession session(root);
    TF_CHECK_OK(session.Run({result}, out_tensors));
    

提交回复
热议问题