opencv中批量读取图片并保存

之前一直在写python,突然要用C++,因为涉及到图像处理,需要批量读取并存储图片,这里对图片的批量处理代码做一下汇总:

方法1:比较简单,唯一的要求就是文件夹下的图片名称是有规律的,比如1,2,3。

#include "opencv2/opencv.hpp"  
#include "iostream"  

using namespace std;  
using namespace cv;  

#define  NUM  100     //读取image的个数  
int main()  
{  
    Mat image;  
    string ImgName;  
    int n=1;  
    while(n<=NUM)   //100  
    {   
        ImgName="woman";  
        //int 转换string  
        stringstream ss;  
        string str;  
        ss<<n;  
        ss>>str;  

        ImgName=ImgName+" ("+str+")";    //图像文件明格式:ImgName(n)  
        ImgName = "D:\\Mycode\\imagebank\\woman\\" + ImgName+".png";  
        cout<<"处理:"<<ImgName<<endl;  
        image= imread(ImgName);//读取图片  
        if(image.data ==0)  
        { printf("[error] 没有图片\n");}  
        n++;  
   }  

    waitKey(0);  
    system("pause");  
    return 4;  
}  

方法二:需要一个 .txt文件存放文件中待读取图像的名称,每行为一条图像名。

#include "opencv2/opencv.hpp"  
#include "iostream"  
#include <fstream>  
using namespace std;  
using namespace cv;  

int main()  
{  
    Mat image;  
    string ImgName;  
    ifstream fin("woman.txt");//打开原始样本图片文件列表  
    while(getline(fin,ImgName)) //一行一行读取文件列表  
    {    
      cout<<"处理:"<<ImgName<<endl;  
      ImgName = "D:\\Mycode\\woman\\" + ImgName+".png";  
      image= imread(ImgName);//读取图片  

     if(image.data ==0)  
      {
  printf("[error] 没有图片\n");return -5;}  
    }  
        waitKey(0);   
return 4;  
}  

方法三:最简单,只需要一个“dirent.h”头文件便可以遍历指定文件夹的所有文件。

#include "opencv2/opencv.hpp"  
#include <fstream>  
#include <iostream>  
#include <string>  
#include "dirent.h"  

using namespace std;  
using namespace cv;  

int main()  
{  
    DIR *dir;   
    struct dirent *entry;   
    if((dir=opendir("D:\\Mycode\\VS2010work\\10Image_cut\\Image_cut\\noperson"))==NULL)   
        printf( "Error opening \n ");   
    else {   
        while((entry=readdir(dir))!=NULL) {   
          cout<<entry->d_name<<endl;  
        }   
    }  
    closedir(dir);    
    system("pause");  
    return 0;  
} 

下面给出dirent.h头文件(文件要放到VS安装目录中的include文件夹下,我的路径为C:\vs2015\VC\include)

/* 
 * Dirent interface for Microsoft Visual Studio 
 * Version 1.21 
 * 
 * Copyright (C) 2006-2012 Toni Ronkko 
 * This file is part of dirent.  Dirent may be freely distributed 
 * under the MIT license.  For all details and documentation, see 
 * https://github.com/tronkko/dirent 
 * http://www.softagalleria.net/dirent.php 
 * http://blog.csdn.net/thefinals/article/details/7174854 
 * https://zhidao.baidu.com/question/939784399849992372.html 
 * http://blog.chinaunix.net/uid-27213819-id-3810699.html 
 */  
#ifndef DIRENT_H  
#define DIRENT_H  

/* 
 * Define architecture flags so we don't need to include windows.h. 
 * Avoiding windows.h makes it simpler to use windows sockets in conjunction 
 * with dirent.h. 
 */  
#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && defined(_M_IX86)  
#   define _X86_  
#endif  
#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && defined(_M_AMD64)  
#define _AMD64_  
#endif  

#include <stdio.h>  
#include <stdarg.h>  
#include <windef.h>  
#include <winbase.h>  
#include <wchar.h>  
#include <string.h>  
#include <stdlib.h>  
#include <malloc.h>  
#include <sys/types.h>  
#include <sys/stat.h>  
#include <errno.h>  

/* Indicates that d_type field is available in dirent structure */  
#define _DIRENT_HAVE_D_TYPE  

/* Indicates that d_namlen field is available in dirent structure */  
#define _DIRENT_HAVE_D_NAMLEN  

/* Entries missing from MSVC 6.0 */  
#if !defined(FILE_ATTRIBUTE_DEVICE)  
#   define FILE_ATTRIBUTE_DEVICE 0x40  
#endif  

/* File type and permission flags for stat(), general mask */  
#if !defined(S_IFMT)  
#   define S_IFMT _S_IFMT  
#endif  

/* Directory bit */  
#if !defined(S_IFDIR)  
#   define S_IFDIR _S_IFDIR  
#endif  

/* Character device bit */  
#if !defined(S_IFCHR)  
#   define S_IFCHR _S_IFCHR  
#endif  

/* Pipe bit */  
#if !defined(S_IFFIFO)  
#   define S_IFFIFO _S_IFFIFO  
#endif  

/* Regular file bit */  
#if !defined(S_IFREG)  
#   define S_IFREG _S_IFREG  
#endif  

/* Read permission */  
#if !defined(S_IREAD)  
#   define S_IREAD _S_IREAD  
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用OpenCV进行图像批量分块并保存,需要以下步骤: 1. 首先,导入OpenCV库,并读取图像文件夹的所有图像。 2. 创建一个循环,用于遍历每个图像。 3. 对于每个图像,使用OpenCV的函数将其加载为一个Mat对象。 4. 定义每个块的大小,可以根据需要调整。 5. 使用嵌套循环,根据块的大小,将图像分割为多个块。 6. 对于每个块,使用OpenCV的函数将其保存为图像文件。 7. 重复上述步骤,直到遍历完所有图像。 下面是示例代码: ```python import cv2 import os # 图像文件夹路径 img_folder = "your_image_folder" # 获取文件夹的所有图像文件 image_files = os.listdir(img_folder) # 遍历每个图像文件 for image_file in image_files: # 加载图像 image = cv2.imread(os.path.join(img_folder, image_file)) # 定义块的大小 block_size = 100 # 循环分割图像为块,并保存 for r in range(0, image.shape[0], block_size): for c in range(0, image.shape[1], block_size): # 提取当前块 block = image[r:r+block_size, c:c+block_size] # 保存当前块为图像文件 block_file_name = os.path.splitext(image_file)[0] + "_block_" + str(r) + "_" + str(c) + ".png" cv2.imwrite(os.path.join(img_folder, block_file_name), block) ``` 这段代码假设图像文件夹路径为“your_image_folder”,并且文件夹包含要处理的图像文件。代码将循环遍历每个图像文件,将每个图像文件加载为一个Mat对象,然后使用嵌套循环将图像划分为多个块,每个块的大小由变量“block_size”定义。最后,保存每个块为一个图像文件,文件名包括原图像文件名、块的行和列索引。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值