How to do copy cv::Mat by pointer into tensor of type float?
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.
and the error is ?
I've added the description of error.
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)