C++遍历统计某个文件夹及其子文件夹下所有图像和像素的个数

104 篇文章 6 订阅
47 篇文章 2 订阅
#include <unistd.h>
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <fstream>
#include <dirent.h>
#include <iomanip>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#define BLOCK_SIZE 1024
#define MAX_DIR_LEN 1024
#define BYTES_PER_PIXEL 3



unsigned long int get_pixels_dir(std::string cur_dir){
	std::string src_dir_str(cur_dir+"/src");
	std::cout<<"Image source: "<<src_dir_str<<std::endl;
	std::string img_src_ent_str;

	// open the directory of source and enumarate the image files in the directory
	DIR *img_src = opendir(src_dir_str.c_str());

	// if source and target folder open failed
	if(img_src == NULL){
		std::cout<<"Can not to open the directory!!!"<<std::endl;
		return 0;
	}

	unsigned int total_pixel_cnt = 0;
	dirent *img_ent;
	cv::Mat img;
	unsigned int img_cnt = 0;
	// enumerate every image file in current directory and process
	while((img_ent = readdir(img_src))){
		if(strcmp(img_ent->d_name, ".") == 0 || strcmp(img_ent->d_name, "..")==0)
			continue;
		else{
			img_cnt++;
			img_src_ent_str = src_dir_str + "/" + img_ent->d_name;
			img = cv::imread(img_src_ent_str, CV_LOAD_IMAGE_COLOR);
			if(img.empty())
				continue;
			total_pixel_cnt += img.total();
			img_src_ent_str.clear();
		}
	}
	std::cout<<"There are "<<img_cnt<<" images"<<std::endl<<std::endl;
	return total_pixel_cnt;
}


int main(){
	// get current working directory
	char cwd[MAX_DIR_LEN];
	if(getcwd(cwd, sizeof(cwd)) == NULL)
		//std::cout<<"Image source directory: "<<cwd<<std::endl;
	//else
		std::cout<<"Get image source directory FAILED!!!"<<std::endl;

	std::string cur_dir(cwd);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值