c++ 用 opencv 读 txt 文件里图片的坑

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/hfq0219/article/details/100878111

在Linux里,按下面这样写,可能图片读不出来,img为空,但是在Windows里是可以的。

#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <string>

using namespace std;
using namespace cv;

int main()
{
	fstream file("name.txt",ios::in);
	string name;
	while(getline(file,name))
	{
		Mat img=imread(name,0);
		imshow("img",img);
		waitKey();
	}
	return 0;
}

需要对name进行转换,然后可以正常读取图片,不清楚是为什么。

#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <string>

using namespace std;
using namespace cv;

int main()
{
	fstream file("name.txt",ios::in);
	string name;
	while(getline(file,name))
	{
		string n;
		istringstream ss(name);
		ss>>n;                                         //转换
		Mat img=imread(n,0);
		imshow("img",img);
		waitKey();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/hfq0219/article/details/100878111