划分数据集代码(按照4:1的比例)以及根据各自文件名写入txt文件

会将图片分到两个文件夹中:

#include <opencv2/opencv.hpp>
#include "opencv2/features2d.hpp"
#include <vector>
#include <algorithm>
#include <iostream>
#include "windows.h"
#include <stdio.h>
#include <time.h>
#include <math.h>  
#include <fstream>
#include <fstream>
#include <iostream>
#include <filesystem>
#include <vector>
#include <ctime>
#define WINDOW_NAME "【程序窗口】"	using namespace cv;using namespace std;using std::ifstream;
using std::ofstream;
using std::cout;
using std::endl;
namespace fs = std::filesystem;int main()
{cv::String path = "D:/一些文件/python code/AI/新数据集/归一化数据集";        //待处理图片文件夹地址cv::String dest_train = "D:/一些文件/python code/AI/新数据集/测试集";    //处理后图片的保存地址cv::String dest_test = "D:/一些文件/python code/AI/新数据集/训练集";    //处理后图片的保存地址cout << "获取地址成功" << endl;cv::String savedfilename;std::vector<cv::String> filenames;int len = path.length();cv::Mat srcImg;cv::glob(path, filenames);                 //opencv里面用来读取指定路径下文件名的一个很好用的函数	//初始化一个随机vector,值为1的序号作为测试集,值为0的序号作为训练集srand((unsigned)time(NULL));vector<int> Test(filenames.size(), 0);for (int i = 0; i < Test.size() * 0.2; i++){int num = rand() % filenames.size();//如果已经存在了,重新初始化一个numwhile (Test[num] == 1){num = rand() % Test.size();}//如果不存在的话Test[num] = 1;}//根据vector的值划分数据集for (int i = 0; i < filenames.size(); i++) {srcImg = cv::imread(filenames[i], 0);//*************************对图片的处理部分***************************/if (Test[i] == 0){savedfilename = dest_train + filenames[i].substr(len);}else{savedfilename = dest_test + filenames[i].substr(len);}std::cout << savedfilename << std::endl;cv::imwrite(savedfilename, srcImg);cout << "第" << i << "张完成" << endl;}waitKey(0);return 0;
}

然后生成两个txt文件,分别写入图片名字。

#include <fstream>//ifstream读文件,ofstream写文件,fstream读写文件#include <string>//文本对象,储存读取的内容
#include <iostream>//屏幕输出cout
#include <cstdlib>//调用system("pause");
#include <windows.h>//用于函数SetConsoleOutputCP(65001);更改cmd编码为utf8
using namespace std;int main()
{SetConsoleOutputCP(65001);ifstream in("D:/一些文件/python code/AI/新数据集/name.txt");ofstream out_test; //("D:/一些文件/python code/AI/新数据集/test.txt");ofstream out_train; //("D:/一些文件/python code/AI/新数据集/train.txt");string line;cv::String path_train = "D:/一些文件/python code/AI/新数据集/测试集";    //处理后图片的保存地址cv::String path_test = "D:/一些文件/python code/AI/新数据集/训练集";    //处理后图片的保存地址std::vector<cv::String> filenames_train;std::vector<cv::String> filenames_test;int len_train = path_train.length();int len_test  = path_test.length();cv::Mat srcImg;cv::glob(path_train, filenames_train);    cv::glob(path_test, filenames_test);if (in) // 有该文件{while (getline(in, line)) // line中不包括每行的换行符{cout << line << endl;string NameOfPic = "";//读到这一行后,对这一行的字符串进行分割,从而得到文件名for (int i = 2; i < line.size(); i++){if (line[i - 2] == ' ' && line[i - 1] == ' ' && line[i] == ' '){NameOfPic.assign(line, 0, i-2);break;}}cout << NameOfPic << endl;int flag = 0;//在测试集图片文件夹中寻找,是否能找到一样的名字的文件for (int i = 0; i < filenames_test.size(); i++) {string path = path_test;path = path + '\\' + NameOfPic;if (path == filenames_test[i]){flag = 1;//追加写入out_test.open("D:/一些文件/python code/AI/新数据集/test.txt", ios::out | ios::app);out_test << line << endl;out_test.close();break;}}//如果在测试集中没有找到,说明在训练集中if (flag == 0){for (int i = 0; i < filenames_train.size(); i++){string path = path_train;path = path + '\\' + NameOfPic;if (path == filenames_train[i]){//追加写入out_train.open("D:/一些文件/python code/AI/新数据集/train.txt", ios::out | ios::app);out_train << line << endl;out_train.close();break;}}}}}else // 没有该文件{cout << "no such file" << endl;}system("pause");return 0;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377363.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

bpo是什么意思_BPO的完整形式是什么?

bpo是什么意思BPO&#xff1a;业务流程外包 (BPO: Business Process Outsourcing) BPO is an abbreviation of Business process outsourcing. It is a convention of a company to another company which is an external provider of services or business operations process…

在进行 ASP.NET 开发时,有时候需要对页面输出的最终 HTML 源代码进行控制

在进行 ASP.NET 开发时&#xff0c;有时候需要对页面输出的最终 HTML 源代码进行控制&#xff0c;是页面的 render 方法中很容易实现这个功能。下面就是一个实现的方法&#xff0c;注释都在代码中。 [c-sharp] view plaincopy <% Page Language"C#" %> <% …

FireFox插件SQLite Manager的使用

最近几天开始高IOS数据库来着&#xff0c;一开始就CoreData的学习&#xff0c;结果高了一天没有一点进展。 没法&#xff0c;还是先老实代码着吧&#xff0c;不过用的火狐插件可视化数据库的操作也是不错的似乎。 网上搜了搜用法&#xff0c;还真没找到有什么的&#xff0c;最后…

简单的数据增强代码(C++与opencv)

包括了图片批量平移、旋转、以及像素变换 #include <opencv2/opencv.hpp> #include "opencv2/features2d.hpp" #include <vector> #include <algorithm> #include <iostream> #include "windows.h" #include <stdio.h> #in…

aes模式_AES的完整形式是什么?

aes模式AES&#xff1a;高级加密标准 (AES: Advanced Encryption Standard) AES is an abbreviation of Advanced Encryption Standard, also known by its original name Rijndael. It is an arrangement of standard for the encryption of electronic data set up by the U.…

IOS ----UIButton用法详解

这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举了.希望对大家有用. //这里创建一个圆角矩形的按钮UIButton *button1 [UIButton buttonWithType:UIButtonTypeRoundedRect];// 能够定义的button类型有以下6种&#xff0c;// typedef enum {// UIButtonTypeCusto…

针对一个class写出它的内存管理池以及总结出allocator类(三个版本)

目录示例版本1&#xff1a;per-class allocator,1示例版本2&#xff1a;per-class allocator,2最终版本&#xff1a;static allocator针对版本三进行macro如果我们不针对对象做内存管理&#xff0c;那么我们每次进行Foo* p new Foo(x);时总是会调用malloc函数。 尽管malloc函数…

kotlin 第一个程序_Kotlin程序添加两个矩阵

kotlin 第一个程序Given two matrices, we have to add them. 给定两个矩阵&#xff0c;我们必须将它们相加。 Example: 例&#xff1a; Input:matrix 1:[2, 3][4, 5][7, 1]matrix 2:[4, 6][9, 0][7, 6]Output:[6, 9][13, 5][14, 7] 在Kotlin中添加两个矩阵的程序 (Progra…

ubuntu 切换用户的命令[shell, linux]

使用ubuntu过程中免不了和shell(终端)打交道, 也不可避免在各种用户之间进行切换, 从而实现对各帐户的管理, 这个就涉及到了一个比较基础又很重要的工作,怎么样切换用户, 对于LINUX老鸟来说,这个根本不值不提的东东却让新手挠头不已, 现在给出普通用户和超级用户切换的命令(附图…

曲苑杂坛--修改数据库名和文件组名

/* 该脚本示例如何完整的修改一个数据库的名称. 数据库为原名称为DB_BEIJING&#xff0c;需要修改成DB_SHANGHAI nzperfect 2012.12.19 */--判断是否存在同名的数据库&#xff0c;以防止误删除 USE master GO IF EXISTS (SELECT name FROM sys.databases WHERE name NDB_BEIJI…

关于new handler与default、delete关键字

在https://blog.csdn.net/qq_42604176/article/details/111638568的operate_new源代码长啥样中谈到过new handler。 当operator new不能够分配出申请的内存时&#xff0c;会抛出bad_alloc 异常。有的编译器会返回0. 当定义成new(nothrow) Foo&#xff1b;就不会抛异常&#xff…

模式匹配运算符–Shell

转载&#xff1a;http://www.firefoxbug.net/?p722 Var/home/firefox/MyProgram/fire.login.name ${Variable#pattern}:如果模式匹配于变量值的开头处&#xff0c;则删除匹配的最短部分&#xff0c;并且返回剩下的部分 例子&#xff1a; [fire]$ echo ${Var#*/} [fire]$ home/…

河内塔问题_河内塔的Python程序

河内塔问题You are challenged for a challenge to find the number of moves required to move a stack of disks from one peg to another peg. Wait for a second, it sounds easy? Let’s find are what is going on and in this article, we are introducing a chapter o…

VC6、BC5、G2.9标准分配器一览

目录VC6标准分配器BC5标准分配器G2.9标准分配器VC6标准分配器 VCx中源码可以在电脑路径中找&#xff1a; [D:\Program Files\VisualStudio\Community\VC\Tools\MSVC\14.28.29333\include\xmemory] 不过太多了。大概在837行左右有关于allocator代码。还是先看侯捷PPT上的吧。 …

【转】shell 大括号、圆括号的使用

在这里我想说的是几种shell里的小括号,大括号结构和有括号的变量&#xff0c;命令的用法&#xff0c;如下&#xff1a; PHP 代码:1.${var} 2.$(cmd) 3.()和{} 4.${var:-string},${var:string},${var:string},${var:?string} 5.$((exp)) 6.$(var%pattern),$(var%%pattern),$(va…

css clear属性_CSS中的clear属性

css clear属性CSS | 清除财产 (CSS | clear Property) We know so much about float property and how it is used for styling our web pages. If you do not remember the float property, lets help jog your memory. The float property is used to set the elements in a …

linux find prune排除某目录或文件

http://blog.csdn.net/ysdaniel/article/details/7995681 查找cache目录下不是html的文件 find ./cache ! -name *.html -type f列出当前目录下的目录名,排除includes目录,后面的-print不能少 find . -path ./includes -prune -o -type d -maxdepth 1 -print排除多个目录,”(“…

嵌入式指针embedded pointer的概念以及用法

目录前言概念用法参考前言 在针对一个class写出它的内存管理池以及总结出allocator类(三个版本)中内存管理池的第二个版本中涉及到了一个非常重要的概念&#xff1a;嵌入式指针。同时嵌入式指针也在G2.9版本的alloc中出现。现在整理一下网上的一些用法和概念 概念 嵌入式指针…

CLI配置和编址

实施基本编址方案&#xff1a; 在设计新网络或规划现有网络时&#xff0c;至少要绘制一幅指示物理连接的拓扑图&#xff0c;以及一张列出以下信息的地址表&#xff1a; l 设备名称 l 设计中用到的接口 l IP 地址和子网掩码 l 终端设备&#xff08;如 PC&#xff09;的默…

sql语句中的in用法示例_PHP中的循环语句和示例

sql语句中的in用法示例循环 (Loops) Imagine that we need a program that says "hello world" 100 times. Its quite stressful and boring to write the statement -- echo "hello world" — 100 times in PHP. This is where loop statement facilitate…