截取车辆正样本

demo主要功能

  1. 由于前期的分类器正样本是包含大部分环境的图片,但是包含了正样本的具体位置,都写在了txt内,需要通过读txt文件把roi截取出来,本demo就是为此而做的。
  2. 保存图片到指定的文件夹

程序展示

#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);
////删除pos位置上的字符
bool is_delete_pos_in_string(string& str,string::size_type pos);
//////得到一个字符串的第i个空格的位置
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>>(rect_roi.tl().x);
        s>>(rect_roi.tl().y);
        s>>(rect_roi.width);
        s>>(rect_roi.Height);*/

        s>>x;
        s>>y;
        s>>width;
        s>>height;



        str_line.erase(iNo_space,delete_len);
        //out_file_pic_name<<str_line<<endl;



        ////////显示
        Mat mat_temp;
        mat_temp = imread(str_line.c_str());
        //imshow("mat_temp",mat_temp);
        //waitKey(1);
        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_temp);
            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++;
    }
    //cout<<"字符串的空格数量:"<<space_count<<endl;
    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;
}

////删除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;
}


//////得到一个字符串的第i个空格的位置
int get_iNo_space_pos(string& str,int iNo)
{

    //pos = str.find_first_of(" ",pos);
    string::size_type pos = 0;
    for(int i = 0; ((pos = (str.find_first_of(" ",pos))) != string::npos)  && i < iNo;)
    {
        pos++;
        i++;
    }
    return pos-1;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值