C++ opencv4 批量彩色图转灰度

#include<io.h>
#include<iostream>
#include<vector>
#include<opencv2/opencv.hpp>
#include <opencv2\imgproc\types_c.h>
using namespace std;
using namespace cv;



void get_image_names(std::string file_path,std::string root_path,::string tar_path, std::vector<std::string>& file_names)
{
	
	intptr_t hFile = 0;
	_finddata_t fileInfo;
	cv::Mat img;
	
	hFile = _findfirst(file_path.c_str(), &fileInfo);
	
	if (hFile != -1) {
		do {
			//如果为文件夹,可以通过递归继续遍历,此处我们不需要
			if ((fileInfo.attrib &  _A_SUBDIR)) {
				continue;
			}
			//如果为单个文件直接push_back
			else {
				file_names.push_back(fileInfo.name);
				cout << fileInfo.name << endl; 
				string name = fileInfo.name;
				
				img = cv::imread(root_path+name);
				cv::cvtColor(img, img, CV_BGR2GRAY);
				cv::imwrite(tar_path + name, img);
				
			    
			}

		} while (_findnext(hFile, &fileInfo) == 0);

		_findclose(hFile);
	}
}



int main()
{
	std::vector<std::string> file_names;
	//图片文件夹以及图片类型
	std::string path = "E:/test/7/*.png";
    //图片根目录
	std::string root_path = "E:/test/7/";
	//图片转换后放置目录
	std::string tar_path = "E:/test/";
	get_image_names(path,root_path,tar_path, file_names);

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值