如何将图片文件里的图片名转换为TXT文档

**

如何将图片文件里的图片名转换为TXT文档

**
随着PASCAL VOC, ImageNet等数据集的逐渐扩充,国际上的大神对目标检测的研究已经达到出神入化的境界。我自己也是研究目标检测这一个方向,主要攻克其中的行人检测模块,所以经常需要使用大量数据集。有很多需要把图片名称转换为对应的txt文档的地方,所以在此把我用过的方法贴出来供大家参考,有的是借助大神的思想,我会在后面贴出链接~

CODE1:将文件夹里的图片一一转换为对应的文档格式保存:

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>

using namespace std;

int main(){
    ofstream myfile;

    myfile.open("D:\\opencv\\manhole\\OpenBOW1\\OpenBOW\\BOWDataSet2\\Manhole\\cutcut.txt"); //*这里是你要保存txt文档的路径,自己先新建一个txt文档;*
    if (!myfile){
        cout << "unable to open myfile" << endl;
        exit(1);
    }

    /*int i;*/
    /*for (int i =0; i <=143; i++){

        myfile << i  << ".jpg" << endl;
    }*/
    for (int i = 1; i <= 8; i++){
        myfile << "08010137" << "(" << i << ")" << ".jpg" << endl;
    }
    for (int i = 1; i <= 8; i++){
        myfile << "08111132" << "(" << i << ")" << ".jpg" << endl;
    }
    for (int i = 1; i <= 8; i++){
        myfile << "08111143" << "(" << i << ")" << ".jpg" << endl;
    }
    for (int i = 1; i <=8;i++){
        myfile <<"08111145"<<"("<<i<<")"<<".jpg"<< endl;
    }

    for (int i = 1; i <=8; i++){
        myfile << "08111175" << "(" << i << ")" << ".jpg" << endl;
    }
    for (int i = 1; i <= 8; i++){
        myfile << "08111178" << "(" << i << ")" << ".jpg" << endl;
    }
    for (int i = 1; i <= 8; i++){
        myfile << "08111179" << "(" << i << ")" << ".jpg" << endl;
    }
    for (int i = 1; i <= 8; i++){
        myfile << "08111217" << "(" << i << ")" << ".jpg" << endl;
    }
    for (int i = 1; i <= 8; i++){
        myfile << "08111233" << "(" << i << ")" << ".jpg" << endl;
    }


    cout << "it has been worked" << endl;
}

举个栗子哈~
这里写图片描述

转换之后:
这里写图片描述

这个方法不好的地方在于,你的图片名必须是一系列连续的,所以局限性很大,后来我在研究其他训练方法的时候发现有个博主的转换方法更简单也适用,在这里也贴出来:
CODE2:

#include<iostream>
#include <fstream>
#include <stdio.h>
#include<io.h>  
using namespace std;
#define F_path "D:\\opencv\\INRIAPerson\\Train\\neg\\*.*"//要读取的图片文件夹路径
#define F_info "D:\\opencv\\VOCdevkit\\negdata.txt"//存放目标文档的路径
int main()
{
    _finddata_t file;
    long lf;
    //创建文件流
    fstream output_stream;

    //输入文件夹路径  
    if ((lf = _findfirst(F_path, &file)) == -1)
        cout << "Not Found!" << endl;
    else{

        //cout << "file name list:" << endl;

        //打开文件, ios::out表示输出,ios::app表示输出到末尾;
        output_stream.open(F_info, ios::out | ios::app);
        //输出文件名
        do
        {
            //cout << file.name << endl;
            output_stream << file.name << endl;
        } while (_findnext(lf, &file) == 0);
    }
    _findclose(lf);
    //system("pause");
    return 0;
}

该程序不限制原文件中的图片名,任何名字都会被读取并保存为相应的txt文档。非常好用!

参考链接:http://blog.csdn.net/ppp036/article/details/51236425

如果你希望将一个文本文件内容转换为图像,可以使用OpenCV库来实现。以下是一个示例代码,演示了如何读取文本文件并将其转换为图像: ```cpp #include <opencv2/opencv.hpp> #include <fstream> void txtToJpg(const std::string& filename, const std::string& outputFilename) { std::ifstream file(filename); if (!file.is_open()) { std::cout << "Failed to open file: " << filename << std::endl; return; } std::string line; std::vector<std::string> lines; while (std::getline(file, line)) { lines.push_back(line); } cv::Mat img(lines.size(), lines[0].length(), CV_8UC1); for (int i = 0; i < lines.size(); ++i) { for (int j = 0; j < lines[i].length(); ++j) { img.at<uchar>(i, j) = static_cast<uchar>(lines[i][j]); } } cv::imwrite(outputFilename, img); } int main() { std::string filename = "input.txt"; // 替换为你的文本文件 std::string outputFilename = "output.jpg"; // 输出的图像文件 // 调用函数将文本文件转换为图像 txtToJpg(filename, outputFilename); return 0; } ``` 在这个示例中,我们首先打开文本文件并逐行读取其内容。然后,我们根据文本的行数和每行的字符数创建一个`CV_8UC1`类型的图像。接下来,我们将每个字符转换为`uchar`类型,并将其放置在图像的对应位置上。最后,我们使用`imwrite`函数将图像保存为JPG文件。 请确保将`input.txt`替换为你实际的文本文件,并根据需要修改输出的图像文件。 希望这对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值