gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:12:54 27 4
gpt4 key购买 nike

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

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() 函数,但它也只需要一张图像作为输入,而不是一个文件夹。

此外,我无法计算它为图像中的每个人脸绘制的矩形数量。,我怎样才能做到这一点......?

请在这方面帮助我。

我正在为我的学者开发人脸检测项目“www.MahdiRezaei.com”,并且我正在使用带有 C、C++ 和 OpenCV 的 Visual Studio-2010。

提前致谢。

最佳答案

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

#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;

}

关于c - 如何在 OpenCV 中将多个图像作为命令行参数加载到 sprintf() 和 cvLoadImage()...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29225947/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com