C++结合OpenCV遍历所有图片文件,并进行裁剪和保存到原地址中

#include<iostream>
#include<stdio.h>
#include <fstream>
#include <io.h>
#include<algorithm>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
 
using namespace cv;
using namespace  std;
 //用于根据指定的路径显示图片
void showimage( const char * path, const char * bmpname){
	char newimgpath[200];
	char imgpath[200];
	
	strcpy_s(imgpath, path);
    strcat_s(imgpath, "\\"); //因为斜杠是转义符号,所以要多写一个
	strcat_s(imgpath, bmpname); 
	for (int i = 0; i<strlen(imgpath); i++)//将 \ 替换成 /
	{
		if (imgpath[i] == '\\')
		{
			imgpath[i] = '/';
		}	
	}
	cout << imgpath << endl;
	// 读入一张图片(游戏原画)
	Mat img=imread(imgpath);
	// 创建一个名为 "游戏原画"窗口
	namedWindow("游戏原画");
    // 在窗口中显示游戏原画
	imshow("游戏原画",img);
	namedWindow("output image");
        Rect rect(240, 60, 680, 700);
	Mat ROI = img(rect);
	imshow("output image",ROI);
	// 等待2000 ms后窗口自动关闭
	waitKey(1000);
    imwrite(imgpath,ROI);
	
}
//findAllFile用于遍历寻找文件
void findAllFile(const char * path,const char * format)
{
    char newpath[200];
    strcpy_s(newpath, path);
    strcat_s(newpath, "\\*.*");    // 在目录后面加上"\\*.*"进行第一次搜索
    int handle;
    _finddata_t findData;
    handle = _findfirst(newpath, &findData);
    if (handle == -1)        // 检查是否成功
        return;
     while (_findnext(handle, &findData) == 0){
        if (findData.attrib & _A_SUBDIR){
            if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
                continue;
            strcpy_s(newpath, path);
            strcat_s(newpath, "\\");
            strcat_s(newpath, findData.name);
            findAllFile(newpath,format);//进行递归
        }
        else{
            if(strstr( findData.name,format)){     //判断是不是txt文件
                //cout << findData.name << "\t" <<path<<"\t"<< findData.size << " bytes.\n";
				showimage(path,findData.name);
            }
        }
    }
    _findclose(handle);    // 关闭搜索句柄
}
int main()
{
	findAllFile("F:\\guanzhu0",".bmp");	
}

类似这样的文件结构,利用C++遍历出所有bmp图片的路径,然后对所有图片进行处理。可以用作机器学习的预处理阶段。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值