C++ fstream流批量处理txt文件

一、用fstream流批量读取wider face标注txt文件,实现将一个图片标注写成一行,写入txt文件中(显示相应人脸框)。


#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>  
using namespace cv;
using namespace std;



int main()
{
	int bbox_num;
	ifstream infile;
	ofstream outfile;
	string image_name;
	string Imagedir = "D:/mtcnn/train/widerface/WIDER_train/images/";
	infile.open("D:/mtcnn/train/widerface/wider_face_split/wider_face_train_bbx_gt.txt");
	outfile.open("D:/mtcnn/train/widerface/wider_face_split/train_bbx.txt");
	while (infile)
	{
		infile >> image_name;
		outfile << image_name << " ";
		string Imagedir_name = Imagedir + image_name;
		cout << image_name << endl;

		//Mat image = imread(Imagedir_name, -1);    //读取原图

		infile >> bbox_num;

		for (int i = 0; i < bbox_num; i++)
		{
			int x1, y1, w, h, blur, exp, ill, occ, pose, inv;
			infile >> x1 >> y1 >> w >> h >> blur >> exp >> ill >> occ >> pose >> inv;
			outfile << x1 << " " << y1 << " " << (x1 + w) << " " << (y1 + h) << " ";
			//Rect box(x1, y1, w, h);
			//rectangle(image, box, cv::Scalar(255, 0, 0), 2);
		}

		outfile << endl;
		//imshow("landmark", image);
		//waitKey(1000);

	}
	infile.close();
	outfile.close();

	return 0;
}

原txt文件效果:

0--Parade/0_Parade_marchingband_1_849.jpg
1
449 330 122 149 0 0 0 0 0 0 
0--Parade/0_Parade_Parade_0_904.jpg
1
361 98 263 339 0 0 0 0 0 0 
0--Parade/0_Parade_marchingband_1_799.jpg
21
78 221 7 8 2 0 0 0 0 0 
78 238 14 17 2 0 0 0 0 0 
113 212 11 15 2 0 0 0 0 0 
134 260 15 15 2 0 0 0 0 0 
163 250 14 17 2 0 0 0 0 0 
201 218 10 12 2 0 0 0 0 0 
182 266 15 17 2 0 0 0 0 0 
245 279 18 15 2 0 0 0 0 0 
304 265 16 17 2 0 0 0 2 1 
328 295 16 20 2 0 0 0 0 0 
389 281 17 19 2 0 0 0 2 0 
406 293 21 21 2 0 1 0 0 0 
436 290 22 17 2 0 0 0 0 0 
522 328 21 18 2 0 1 0 0 0 
643 320 23 22 2 0 0 0 0 0 
653 224 17 25 2 0 0 0 0 0 
793 337 23 30 2 0 0 0 0 0 
535 311 16 17 2 0 0 0 1 0 
29 220 11 15 2 0 0 0 0 0 
3 232 11 15 2 0 0 0 2 0 
20 215 12 16 2 0 0 0 2 0 

处理后txt文件效果:

0--Parade/0_Parade_marchingband_1_849.jpg 449 330 571 479 
0--Parade/0_Parade_Parade_0_904.jpg 361 98 624 437 
0--Parade/0_Parade_marchingband_1_799.jpg 78 221 85 229 78 238 92 255 113 212 124 227 134 260 149 275 163 250 177 267 201 218 211 230 182 266 197 283 245 279 263 294 304 265 320 282 328 295 344 315 389 281 406 300 406 293 427 314 436 290 458 307 522 328 543 346 643 320 666 342 653 224 670 249 793 337 816 367 535 311 551 328 29 220 40 235 3 232 14 247 20 215 32 231 


二、在txt文件中批量读取标注(每个图片为一行),并在原图上显示标注效果。

知识点: fstream流处理,split字符串分割,str2int string类型转化为int类型。


#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>  
using namespace cv;
using namespace std;


//字符串分割函数 
std::vector<std::string> split(std::string& str, std::string& pattern) 
{ 
	std::string::size_type pos;    
	std::vector<std::string> result;
	//str += pattern;//扩展字符串以方便操作   
	int size = str.size();

	for (int i = 0; i<size; i++)
	{
		pos = str.find(pattern, i);
		if (pos<size) {
			std::string s = str.substr(i, pos - i);
			result.push_back(s);
			i = pos + pattern.size() - 1;
		}
	}
	return result;
}

void str2int(int &int_temp, const string &string_temp)
{
	stringstream stream(string_temp);
	stream >> int_temp;
}


int main()
{
	int bbox_num;
	ifstream infile;
	string image_name;
	string Imagedir = "D:/mtcnn/train/widerface/WIDER_train/images/";
	infile.open("D:/mtcnn/train/widerface/wider_face_split/train_bbx.txt");

	string line;
	string s = " ";
	while (getline(infile,line))
	{
		vector<string> result = split(line, s);
		string image_name = result[0];
		string Imagedir_name = Imagedir + image_name;
		cout << image_name << endl;
		
		Mat image = imread(Imagedir_name, -1);    //读取原图

		int num = result.size() - 1;
		for (int i = 0; i < num / 4; i++)
		{
			int x1, y1, x2, y2, w, h;
			str2int(x1, result[i*4 + 1]);
			str2int(y1, result[i*4 + 2]);
			str2int(x2,  result[i*4 + 3]);
			str2int(y2,  result[i*4 + 4]);
			w = x2 - x1;
			h = y2 - y1;
			Rect box(x1, y1, w, h);
			rectangle(image, box, cv::Scalar(255, 0, 0), 2);
		}
	
		imshow("landmark", image);
		waitKey(1000);

	}
	infile.close();

	return 0;
}


猜你喜欢

转载自blog.csdn.net/xzzppp/article/details/75220657