Opencv 根据照片名字进行分类

最近参加了中兴的算法大赛,然后选择的是人脸识别赛题。但是由于自己编程能力一般,理所当然的是掉坑了。一起组队的研二师兄分配了一些任务给我。由于我们采用的是深度学习进行人脸识别,通过和其他人换照片,采集了大量的照片集。我的任务就是将不同的照片划分到不同的文件夹。由于有一百多数据集,每个数据集有二十四张照片,所以人工分文件夹几乎不可能。

偶然间在《Opencv图像处理编程实例》看到 了图片的批量读取的实例,感觉稍加改进,就能进行批量地分类。因为数据集的命名都是很有规律的,例如男_无眼镜_白口罩_无帽子_面。这样的命名非常利于分类。要求是将男女分类,有无眼镜分类,有无口罩分类,有无帽子分类。所以我想到利用C++中的string类型里面有find函数。但是也正是这个函数让我栽了跟头。一开始想当然地以为find函数就是查找到指定字符串返回1,未查找到指定字符串则返回0。因为这个坑,一直没法实现想要的结果。后来通过各种折腾吧(后知后觉,sigh...)发现自己理解错了find函数,其实这个函数是未找到指定字符串就返回-1,找到返回指定字符串的位置。

后来经过一番改动,最初的按照照片名字进行 图片分类的程序完成了。

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include<string.h>
/*
WChar2Ansi,
readImgNamefromFile函数




来源:《OpenCV图像处理编程实例》
*/
using namespace std;
// LPCWSTR转string
std::string WChar2Ansi(LPCWSTR pwszSrc)
{
	int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);

	if (nLen <= 0) return std::string("");

	char* pszDst = new char[nLen];
	if (NULL == pszDst) return std::string("");

	WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
	pszDst[nLen - 1] = 0;

	std::string strTemp(pszDst);
	delete[] pszDst;

	return strTemp;
}

// 利用winWIN32_FIND_DATA读取文件下的文件名
void readImgNamefromFile(char* fileName, vector <string> &imgNames)
{
	// vector清零 参数设置
	imgNames.clear();
	WIN32_FIND_DATA file;
	int i = 0;
	char tempFilePath[MAX_PATH + 1];
	char tempFileName[50];
	// 转换输入文件名
	sprintf_s(tempFilePath, "%s/*", fileName);
	// 多字节转换
	WCHAR   wstr[MAX_PATH] = { 0 };
	MultiByteToWideChar(CP_ACP, 0, tempFilePath, -1, wstr, sizeof(wstr));
	// 查找该文件待操作文件的相关属性读取到WIN32_FIND_DATA
	HANDLE handle = FindFirstFile(wstr, &file);
	if (handle != INVALID_HANDLE_VALUE)
	{
		FindNextFile(handle, &file);
		FindNextFile(handle, &file);
		// 循环遍历得到文件夹的所有文件名	
		do
		{
			sprintf(tempFileName, "%s", fileName);
			imgNames.push_back(WChar2Ansi(file.cFileName));
			imgNames[i].insert(0, tempFileName);
			i++;
		} while (FindNextFile(handle, &file));
	}
	FindClose(handle);
}
int main()
{
	// 设置读入图像序列文件夹的路径
	char* fileName = "G:\\desktop\\1\\112-92\\";
	std::vector <string>  imgNames;
	// 获取对应文件夹下所有文件名
	readImgNamefromFile(fileName, imgNames);
	// 遍历对应文件夹下所有文件名
	for (int i = 0; i < imgNames.size(); i++)
	{
		cv::Mat img = cv::imread(imgNames[i]);
		if (!img.data)
			return -1;
		/* 可添加图像处理算法code*/
		string male="男";
		string noglass = "无眼镜";
		string nokouzhao = "无口罩";
		string nohat = "无帽子";
		//std::cout << imgNames[i];
		if (imgNames[i].find(male) != -1)
		{
			

			string temp;
			string filename = "G:\\desktop\\classify\\sex\\1\\";
			temp = std::to_string(i);
			string addname=temp+".jpg";
			filename.append(addname);
			cv::imwrite(filename,img);
			//存放到男生的文件夹
		}else
		{
			string temp;
			string filename = "G:\\desktop\\classify\\sex\\0\\";
			temp = std::to_string(i);
			string addname = temp + ".jpg";
			filename.append(addname);
			cv::imwrite(filename, img);
			//cv::imwrite("G:\\desktop\\classify\\sex\\0\\", img);
			//存放到女生的文件夹
		}

		if (imgNames[i].find(noglass)!=-1)
		{
			
			string temp;
			string filename = "G:\\desktop\\classify\\glass\\0\\";
			temp = std::to_string(i);
			string addname = temp + ".jpg";
			filename.append(addname);
			cv::imwrite(filename, img);
			//存放到无眼镜的文件夹
		} else
		{
			cout << "       " << "    " << imgNames[i] << "         ";
			string temp;
			string filename = "G:\\desktop\\classify\\glass\\1\\";
			temp = std::to_string(i);
			string addname = temp + ".jpg";
			filename.append(addname);
			cv::imwrite(filename, img);
			//存放到有眼镜的文件夹
		}

		if (imgNames[i].find(nokouzhao) !=-1)
		{
			string temp;
			string filename = "G:\\desktop\\classify\\kouzhao\\0\\";
			temp = std::to_string(i);
			string addname = temp + ".jpg";
			filename.append(addname);
			cv::imwrite(filename, img);
			//存放到无口罩的文件夹
		}else
		{
			string temp;
			string filename = "G:\\desktop\\classify\\kouzhao\\1\\";
			temp = std::to_string(i);
			string addname = temp + ".jpg";
			filename.append(addname);
			cv::imwrite(filename, img);
			//存放到口罩的文件夹
		}


		if (imgNames[i].find(nohat) != -1)
		{
			string temp;
			string filename = "G:\\desktop\\classify\\hat\\0\\";
			temp = std::to_string(i);
			string addname = temp + ".jpg";
			filename.append(addname);
			cv::imwrite(filename, img);
			//存放到没帽子的文件夹中
		}else
		{
			string temp;
			string filename = "G:\\desktop\\classify\\hat\\1\\";
			temp = std::to_string(i);
			string addname = temp + ".jpg";
			filename.append(addname);
			cv::imwrite(filename, img);
			//存放到帽子的文件夹中
		}


		cv::imshow("im", img);
		cv::waitKey(0);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值