demo主要功能
- 由于前期的分类器正样本是包含大部分环境的图片,但是包含了正样本的具体位置,都写在了txt内,需要通过读txt文件把roi截取出来,本demo就是为此而做的。
- 保存图片到指定的文件夹
程序展示
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
using namespace cv;
int get_space_number_in_string(string str);
bool is_have_space_in_string(string str);
int get_first_space_pos(string& str);
bool is_delete_pos_in_string(string& str,string::size_type pos);
int get_iNo_space_pos(string& str,int iNo);
int main()
{
ifstream in_file;
in_file.open("info.txt");
ofstream out_file_pic_name;
out_file_pic_name.open("info_pic_name.txt");
if(!in_file.is_open())
cout<<"没有打开"<<endl;
string str_line;
while(getline(in_file,str_line))
{
static int i = 0;
i++;
cout<<"第"<<i<<"条记录是:"<<str_line<<endl;
int iNo_space = get_iNo_space_pos(str_line,1);
cout<<iNo_space<<endl;
int iLength = str_line.size();
int delete_len = iLength - iNo_space+1;
string str_num_and_pos = str_line.substr(iNo_space,delete_len);
string str_pos = str_line.substr(iNo_space+1,delete_len);
Rect rect_roi;
int x,y,width,height;
istringstream s(str_pos);
s>>x;
s>>y;
s>>width;
s>>height;
str_line.erase(iNo_space,delete_len);
Mat mat_temp;
mat_temp = imread(str_line.c_str());
if(x+width <= mat_temp.cols && y+height <= mat_temp.rows)
{
Mat mat_roi = mat_temp(Rect(x,y,width,height));
imshow("mat_roi",mat_roi);
waitKey(1);
int _pos = str_line.find_first_of("/",0);
iLength = str_line.size();
str_line.erase(0,_pos+1);
string str_temp;
str_temp.append("rawdata_new_only_car/").append(str_line);
imwrite(str_temp.c_str(),mat_roi);
}
}
out_file_pic_name.close();
system("pause");
return 0;
}
int get_space_number_in_string(string str)
{
string::size_type pos = 0;
int space_count = 0;
while((pos = (str.find_first_of(" ",pos))) != string::npos)
{
pos++;
space_count++;
}
return space_count;
}
bool is_have_space_in_string(string str)
{
string::size_type pos = str.find(" ");
if(pos == string::npos)
return false;
else
return true;
}
int get_first_space_pos(string& str)
{
string::size_type pos = 0;
pos = str.find_first_of(" ",pos);
return pos;
}
bool is_delete_pos_in_string(string& str,string::size_type pos)
{
if(pos >= string::npos)
return false;
str.erase(pos,1);
return true;
}
int get_iNo_space_pos(string& str,int iNo)
{
string::size_type pos = 0;
for(int i = 0; ((pos = (str.find_first_of(" ",pos))) != string::npos) && i < iNo;)
{
pos++;
i++;
}
return pos-1;
}