歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Linux C讀取並處理文件夾下的所有文件

Linux C讀取並處理文件夾下的所有文件,定義傳遞參數使用的結構體:頭文件<param.h>

Linux C程序內存洩露檢測 http://www.linuxidc.com/Linux/2014-03/98740.htm

Linux C中如何定義可變參數的宏 http://www.linuxidc.com/Linux/2014-03/98663.htm

#ifndef _PARAM_H_
#define _PARAM_H_

typedef struct str_1_param
{
    char *str;
    void *param;
} STR_1_PARAM, *pSTR_1_PARAM;

#endif

使用方法:遍歷文件夾下圖片,提起hog特征:

#include <stdio.h>
#include <getopt.h>
#include "param.h"
#include "procdir.h"
#include "cv.h"
#include "opencv2/opencv.hpp"

using namespace cv;
 
typedef struct MY_PARAM
{
    IplImage *img;
    IplImage *dst;
    HOGDescriptor *hog;
    FILE *pout;
    vector<float> descriptors;
} MY_PARAM, *pMY_PARAM;

void extr_hog(void *s1p)
{
    STR_1_PARAM *str1param = (STR_1_PARAM*)s1p;
    MY_PARAM *my_param = (MY_PARAM*)str1param->param;
    my_param->img = cvLoadImage(str1param->str,0);
    cvSmooth(my_param->img, my_param->dst, CV_GAUSSIAN, 3, 3);
    my_param->hog->compute(my_param->dst, my_param->descriptors, Size(1,1), Size(0,0));
    for(int i=0;i<my_param->descriptors.size();i++)
    {
        fprintf(my_param->pout, "%f ", my_param->descriptors[i]);
    }
    fprintf(my_param->pout, "\n");
    cvReleaseImage(&my_param->img);
}

int main(int argc, char **argv)
{
    if(argc != 5 )
    {
        printf("Usage:\n%s [-d InputBaseDir] [-o OutputFile]\n", argv[0]);
        return -1;
    }

    char tmp, *base, *output;
    while((tmp=getopt(argc,argv,"d:o:"))!=-1)
    {
        switch(tmp)
        {
            case 'd':
                base = optarg;
                break;
            case 'o':
                output = optarg;
                break;
        }
    }

    FILE *pOutFile;
    pOutFile =  fopen(output,"w");
    if(!pOutFile)
    {
        printf("Open/create file %s failed!\n", output);
        return -1;
    }

    MY_PARAM my_param;
    my_param.dst = cvCreateImage(cvSize(64,64),8,1);
    my_param.pout = pOutFile;
    my_param.hog = new HOGDescriptor(cvSize(64,64),cvSize(16,16),cvSize(8,8),cvSize(8,8),9);
    STR_1_PARAM str1param;
    str1param.param = &my_param;
   
&nbsp;&nbsp;  int rval = procdir(base, extr_hog, &str1param, 1);

    fclose(pOutFile);

    return 0;
}

Copyright © Linux教程網 All Rights Reserved