面是利用OpenCV从图像中截取一块,并作为图像输出的函数,是很简单的,也在多幅图像中循环使用过,没出过错。
void GetImageRect(IplImage* orgImage, CvRect rectInImage, IplImage* imgRect)
{
//从图像orgImage中提取一块(rectInImage)子图像imgRect
IplImage *result=imgRect;
CvSize size;
size.width=rectInImage.width;
size.height=rectInImage.height;
//result=cvCreateImage( size, orgImage->depth, orgImage->nChannels );
//从图像中提取子图像
cvSetImageROI(orgImage,rectInImage);
cvCopy(orgImage,result);
cvResetImageROI(orgImage);
}
为检验上述代码,我把它嵌入到OpenCv的人脸检测例程中调用,调用方法见红色字体部分。将检测出的人脸变成图像显示,但没有保存。
int faceDetect(char* cascade_name, IplImage* image , CvSize winSize)
{//cascade_name:人脸检测的模型文件;image:待检测的图像;winSize:检测窗口大小
CvMemStorage* storage = 0;
CvMemStorage* cascadeMem=0;
IplImage* im;
//im=cvCreateImage( cvGetSize(image), image->depth, image->nChannels );
//cvCopy(image,im,NULL);
CvHaarClassifierCascade* cascade = 0;
// Load the HaarClassifierCascade
cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, cascadeMem, 0, 0 );
// Check whether the cascade has loaded successfully. Else report and error and quit
if( !cascade )
{
fprintf( stderr, "ERROR: Could not load classifier cascade/n" );
return -1;
}
// Allocate the memory storage
storage = cvCreateMemStorage(0);
if( image )
{
// Detect and draw the face
CvSeq* faces;
faces=detect( image, storage, cascade, winSize);
im=draw(image, faces);
cvRelease((void**)&faces);
}
// Create a new named window with title: result
cvNamedWindow( "result", 1 );
// Show the image in the window named "result"
cvShowImage( "result", im );
// Wait for user input
cvWaitKey(0);
// Destroy the window previously created with filename: "result"
cvDestroyWindow("result");
cvReleaseImage( &im );
// return 1 to indicate successfull execution of the program
return 1;
}
// Function to detect and draw any faces that is present in an image
CvSeq* detect( IplImage* img, CvMemStorage* storage, CvHaarClassifierCascade* cascade, CvSize winSize)
{
// Create a new image based on the input image
CvSeq* tempFace;
// Clear the memory storage which was used before
cvClearMemStorage( storage );
// Find whether the cascade is loaded, to find the faces. If yes, then:
if( cascade )
{
// There can be more than one face in an image. So create a growable sequence of faces.
// Detect the objects and store them in the sequence
tempFace = cvHaarDetectObjects( img, cascade, storage,
1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
winSize );
}
// Release the temp image created.
return tempFace;
}
IplImage* draw(IplImage* img, CvSeq* faces)
{
// Create two points to represent the face locations
CvPoint pt1, pt2;
int i;
int scale = 1;
IplImage* temp = cvCreateImage( cvGetSize(img), img->depth, img->nChannels );
CvSize size;
cvCopy(img,temp,NULL);
CvRect* r;
// Loop the number of faces found.
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
// Create a new rectangle for drawing the face
r = (CvRect*)cvGetSeqElem( faces, i );
// Find the dimensions of the face,and scale it if necessary
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;
// Draw the rectangle in the input image
cvRectangle( temp, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
//从图像中把检测窗口转为图像
IplImage* detectFace;
size.width=r->width;
size.height=r->height;
detectFace=cvCreateImage( size, img->depth, img->nChannels );
GetImageRect(img, *r, detectFace);
cvNamedWindow( "face", 1 );
cvShowImage( "face", detectFace );
cvWaitKey(0);
cvDestroyWindow("face");
cvReleaseImage( &detectFace );
}
return temp;
}