OpenCV实现resize和crop当前文件夹下所有图片

来源:互联网 发布:非洲网络制式 编辑:程序博客网 时间:2024/06/01 10:00

#include "opencv2/imgproc/imgproc.hpp"#include "opencv2/highgui/highgui.hpp"#include <string>#include <fstream>#include <iostream>using namespace cv;using namespace std;int main(int argc, char* argv[]){//创建文件名列表文件,若存在则清空文件fstream file_list("name.txt", std::ios::out);file_list.close();//写入文件名列表到file_list.txtsystem("dir /a /b >> name.txt");file_list.open("name.txt", std::ios::in);string filename;string extension;while(!file_list.eof()){getline(file_list, filename);int location = filename.find(".", 0);if(location != string::npos){extension = filename.substr(location);if(0 == extension.compare(".jpg") || 0 == extension.compare(".png") ){// do something with file Mat src, dst;src = imread(filename);resize(src, dst, Size(32,32));Mat faceROI = dst(Rect(4,4,24,24));char image_name[128];sprintf(image_name, "resize_crop_%s", filename.c_str());imwrite(image_name, faceROI);}}}return 0;}


2 0