批处理图片:文件夹中的图片命名杂乱,实现一次性读取图片

批处理图片:文件夹中的图片命名杂乱,不是按一定的次序,实现一次性读取文件

1、获取文件夹里图片列表

  编写一个bat程序(get route.batbat是避免每次都需要去dos框输入,那里又不能复制又不能粘贴!),如下所示:


运行bat文件,就会生成txt文件:

2、编写程序顺序读取Img.txt文件中的名字,即可一次处理图片。

#include <opencv2/opencv.hpp>
#include <fstream>


#include <iostream>
#include <vector>
#include <string.h>
#include <time.h>
using namespace std;
using namespace cv;

int main()
{
	string list_file = "F://Img.txt";

	//检测测试样本
	ifstream fin(list_file); //打开记录样本名的文件
	//ifstream fin(person_neg_test_file); //打开记录样本名的文件

	char img_name[256]; //存储文件名
	string path; 
	int sample_count = 0;

	while(!fin.eof())
	{

		memset(img_name, '/0', 256*sizeof(char));  
		fin>>img_name;
		int len = strlen(img_name);
		if (len<=3)
		{
			break;
		}
		string tempStr = img_name;  
		path = list_file.substr(0,list_file.find_last_of("/")+1) + tempStr;
		//path = person_neg_test_file.substr(0,person_neg_test_file.find_last_of("/")+1) + tempStr;

		//cout<<path<<endl;
		//sample_count++;
		//char name[100];
		//sprintf(name,"%d",sample_count);
		//string save_name(name);
		//save_name.append(".jpg");

		//读取图像
		cv::Mat img = cv::imread(path);  
		Mat grayImg;

		if (img.data)
		{
			//resize(img,img,Size(32,32));
			cvtColor(img,grayImg,CV_BGR2GRAY);
			imwrite(path,grayImg);
			imshow("img",grayImg);
		}

	}	


	return 0; 
}







  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值