0

I'm currently reading images using ReadTensorFromImageFile() function. But I want to read using OpenCV function.

tensorflow image reading:

 Status read_tensor_status =
  ReadTensorFromImageFile(image_path, input_height, input_width, input_mean,
                          input_std, &resized_tensors);
  if (!read_tensor_status.ok()) {
      LOG(ERROR) << read_tensor_status;
      return -1;
  }
  // @resized_tensor: the tensor storing the image
  const Tensor &resized_tensor = resized_tensors[0];
  auto resized_tensor_height = resized_tensor.shape().dim_sizes()[1];
  auto resized_tensor_width = resized_tensor.shape().dim_sizes()[2];
  std::cout << "resized_tensor_height:\t" << resized_tensor_height
            << "\nresized_tensor_width:\t" << resized_tensor_width << std::endl;

out

resized_tensor_height:  416
resized_tensor_width:   416

The same reading want to do it using OpenCV function. After converting OpenCV format, want to pass the resized_tensor into this session

  Status run_status = session->Run({{input_layer, resized_tensor}},
                                   output_layer, {}, &outputs);
Dada
  • 6,313
  • 7
  • 24
  • 43
W3 heap
  • 1
  • 3
  • Probably cv::blobFromImage is what you will need after cv::imread. Read docs if the TF function and opencv what exactly they do. – Micka Nov 25 '21 at 09:15

1 Answers1

1

If you have tensor t, and want to create cv::Mat that points to the memory of t (and does not own the memory itself), you can do:

cv::Mat m(height, width, CV_32FC1, t.flat<float>().data());

Need to make sure the type is correct, not sure what format the ReadTensorFromImageFile returns. If you now want to resize it to another tensor, you can do:

// Create the target tensor, again - use correct types
tensorflow::Tensor tResized(tensorflow::DT_FLOAT, tensorflow::TensorShape({ 1, w, h, 1 }));

// Wrap it with cv::Mat
cv::Mat mResized(height, width, CV_32FC1, tResized.flat<float>().data());

// resize with cv
resize(m, mResized, newSize);

Notes:

  • Did not compile or run this code, so it's more of a pseudocode
  • Why not loading the image with cv::imread at the first place?