Ask Your Question
0

How to do copy cv::Mat by pointer into tensor of type float?

asked 2018-07-16 08:11:07 -0600

dima00782 gravatar image

updated 2018-07-16 08:28:36 -0600

Hi, I want to use opencv with tensorflow and try to copy my cv::Mat into tensor by get assertion from opencv.

I have following code:

tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT, {1, 160, 160, 3});
// get pointer to memory for that Tensor
float* ptr = input_tensor.flat<float>().data();
cv::Mat tensor_image(160, 160, CV_32FC3, ptr);
image.copyTo(tensor_image, CV_32FC3);

image is my initial image, it has size 160 x 160. I make it with next procedure:

  cv::Mat opencv_formatted_face;
  cv::resize(face_in_opencv_format, opencv_formatted_face, cv::Size(160, 160));

  cv::Mat image;
  opencv_formatted_face.copyTo(image);
  std::cerr << "SIZE OF IMAGE " << image.size << std::endl;
  cv::Mat tmp1 = image.reshape(1, image.rows * 3);
  cv::Mat mean3;
  cv::Mat stddev3;
  cv::meanStdDev(temp, mean3, stddev3);
  double mean_pxl = mean3.at<double>(0);
  double stddev_pxl = stddev3.at<double>(0);
  cv::Mat image2;
  image.convertTo(image2, CV_64FC1);
  image = image2;
  image = image - cv::Vec3d(mean_pxl, mean_pxl, mean_pxl);
  image = image / stddev_pxl;

I'm getting the error here:

image.copyTo(tensor_image, CV_32FC3);

error: OpenCV(3.4.1) Error: Assertion failed (mask.depth() == 0 && (mcn == 1 || mcn == cn)) in copyTo, file /home/dmitry/code/opencv-3.4.1/modules/core/src/copy.cpp, line 382

terminate called after throwing an instance of 'cv::Exception'

what(): OpenCV(3.4.1) /home/dmitry/code/opencv-3.4.1/modules/core/src/copy.cpp:382: error: (-215) mask.depth() == 0 && (mcn == 1 || mcn == cn) in function copyTo

Please help.

edit retag flag offensive close merge delete

Comments

and the error is ?

berak gravatar imageberak ( 2018-07-16 08:16:20 -0600 )edit
1

I've added the description of error.

dima00782 gravatar imagedima00782 ( 2018-07-16 08:29:00 -0600 )edit

Did you checked if the mat has any data? I usually get this error when trying to read from invalid path location. Check with if(mat.data)

holger gravatar imageholger ( 2018-07-16 08:36:28 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-07-16 08:31:52 -0600

berak gravatar image

copyTo() does not take any flags, you probably wanted a conversion to float:

image.convertTo(tensor_image, CV_32F);

(note, that it only takes the "depth", not the number of channels here (those are unchanged).)

edit flag offensive delete link more

Comments

thank you very much!

dima00782 gravatar imagedima00782 ( 2018-07-16 08:53:37 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-07-16 08:11:07 -0600

Seen: 2,971 times

Last updated: Jul 16 '18