#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;
}
C++遍历统计某个文件夹及其子文件夹下所有图像和像素的个数
最新推荐文章于 2023-03-29 11:51:47 发布