我在我的代码中使用了 tensorflow c++ API。我发现当我从文件加载图像时图像为 NULL。然后我写了一个测试代码来查找原因。
这是我的测试代码:
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
//#include "tensorflow/core/public/session.h"
//#include "tensorflow/core/protobuf/meta_graph.pb.h"
int main()
{
Mat imgtry = imread("lena.jpg");
printf("%dx%d", imgtry.cols, imgtry.rows );
return 0;
}
当我注释tensorflow的header时,输出值为255x255,但是一旦我取消注释header,输出值为0x0。为什么 ???
在我修改链接库的顺序后,问题似乎有所改变。首先,我链接了 tensorflow_cc
和 tensorflow_frameworok
,然后链接了 opencv
的库。现在,我将tensorflow的库放在opencv和let对应的include目录之后,顺序相同。然后我可以正常读取图像甚至取消上面代码区域中代码的注释。但是新的问题出现了。
#include "opencv2/opencv.hpp"
#include "opencv2/imgcodecs.hpp"
//it's ok.
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/protobuf/meta_graph.pb.h"
using namespace std;
int main()
{
cv::Mat img;
img = cv::imread("lena.jpg");
if(img.empty() == true) {
cout << "Error!" << endl;
exit(1);
}
cout << "ok!" << endl;
//uncomment this, the img is always emtpy!!!
// tensorflow::SessionOptions sessOptions;
// sessOptions.config.mutable_gpu_options()->set_allow_growth(true);
// auto session = tensorflow::NewSession(sessOptions);
// if(session == nullptr) {
// cout << "Could not create Tensorflow session." << endl;
// exit(1);
// }
return 0;
}
最佳答案
关于c++ - 为什么 cv::imread 在包含 tensorflow 的 header 后返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47746125/