如何在OpenCV中将多个图像作为命令行参数加载到sprintf()和cvLoadImage()中?

时间:2015-03-24 05:58:42

标签: c visual-studio opencv image-processing

这是我的代码。它将检测图像中的面部并在脸部周围绘制矩形。

CvHaarClassifierCascade *cascade;
CvMemStorage *storage;
void detectFaces (IplImage *newframe);
int key;
int iImg;
int Num_Images=20;

int main( int argc, char** argv )
{
string filename = M:/Trend/FaceDetection/MyCascadeFolder/haarcascade_frontalface_alt2.xml";
storage = cvCreateMemStorage(0) ;   
cascade = ( CvHaarClassifierCascade* )cvLoad( filename.c_str());

**//to load a single image**
**IplImage* inImage = cvLoadImage("M:/Trend/FaceDetection775/MyImages/face1.jpg",1);**

//IplImage* inImage = cvLoadImage(argv[1],1); >> It also works fine through command line

detectFaces( inImage );
cvShowImage("FaceDetection2",inImage);
cvSaveImage("M:/FaceDetection/MyImages/OpImages/faces.jpg",inImage);
cvReleaseImage( &inImage);              

// **to load no. of images or a complete folder**
char buf[20];       
for(iImg=0; iImg<=Num_Images; iImg++)  
{
**sprintf(buf, "M:/FaceDetection/ImagesFolder/P%04d.jpg", iImg);**                      
*// sprintf(buf, argv[2], iImg); // I Tried with this., but its not working*

 printf("\n");      
inImage = cvLoadImage(buf, CV_LOAD_IMAGE_UNCHANGED);  
printf("Input Image = P%04d.jpg\n", iImg);              
detectFaces( inImage );
cvShowImage("FaceDetection",inImage);
cvReleaseImage( &inImage );                 
key= cvWaitKey(0);
if ( key == 'q' || key == 'Q' )
return(0);
}

cvReleaseHaarClassifierCascade( &cascade );
cvReleaseMemStorage( &storage );
return 0;
}

**// the actual detectfaces() function goes like this**
void detectFaces( IplImage *newframe)
{
CvSeq *faces = cvHaarDetectObjects( newframe, cascade, storage, 1.15, 5, 0,   cvSize( 30, 30 ) );
    for( int i = 0 ; i < ( faces ? faces->total : 0 ) ; i++ )
    {
    CvRect *r = ( CvRect *)cvGetSeqElem( faces, i );
    cvRectangle(newframe,cvPoint( r->x, r->y ),cvPoint( r->x + r->width, r->y + r->height ),CV_RGB( 0, 255, 0 ), 2, 8, 0 );  
    }
}

这里,sprintf()方法静态地获取多个图像(在程序本身内)。但我想通过命令行参数将这些多个图像作为单个文件夹传递。

我尝试使用cvLoadImage()函数,但它也只需要一个图像作为输入而不是文件夹。

此外,我无法计算它为图像中每个面部绘制的矩形数量。我怎么能这样做......?

请在这方面帮助我。

我正在为我的学者们开展面部检测项目&#34; www.MahdiRezaei.com&#34; 。我正在使用带有C,C ++和OpenCV的visual studio-2010。< / p>

提前谢谢。

1 个答案:

答案 0 :(得分:-1)

您可以更改以下代码以将其与参数一起使用。

#include<Windows.h>
#include<iostream>
#include<opencv2\highgui\highgui.hpp>

using namespace cv;
using namespace std;

int main()
{
WIN32_FIND_DATA data;

HANDLE h = FindFirstFile(L"c:\\Images\\*.*", &data); // first file is the current directory
FindNextFile(h, &data);                                 //second file is the parent directory
vector<Mat> Images;
if (h != INVALID_HANDLE_VALUE)
{
    while (FindNextFile(h, &data))
    {
        string filename = data.cFileName;
        sprintf(filename, "%s%s","C:\\Images\\", filename.c_str());
        Images.push_back(imread(filename, 1));
        delete[] nPtr;

    } 
}
else
    cout << "Error: No such folder." << endl;

FindClose(h);
return 0;

}