Exemple #1
0
BOOL CSonicImage::Load(HGLOBAL hGlobal, DWORD dwSize)
{
	if(m_gif.LoadGif(hGlobal, dwSize) == 1)
	{		
		if(PrepareMemDC(m_gif.GetWidth(), m_gif.GetHeight()) == FALSE)
		{
			m_gif.Clear();
			return FALSE;
		}
		m_gif.Draw(m_Dib.GetSafeHdc());
	}
	else
	{
		BYTE * pData = (BYTE *)GlobalLock(hGlobal);
		CxImage img;
		img.Decode(pData, dwSize, 0);
		GlobalUnlock(hGlobal);
		if(PrepareMemDC(img.GetWidth(), img.GetHeight()) == FALSE)
		{
			img.Clear();
			return FALSE;
		}
		if(!img.AlphaIsValid())
		{
			img.Draw(m_Dib.GetSafeHdc());
			CSSE::DoOr(0xff000000, m_Dib.GetBits(), m_Dib.GetSize());
		}
		else
		{
			if(img.GetBpp() != 24)
			{
				img.Clear();
				return FALSE;
			}
			BYTE * pSrc = img.GetBits();
			BYTE * pAlpha = img.AlphaGetBits();
			BYTE * pMyBits = m_Dib.GetBits();
			int nLineTail = m_nWidth % 4;
			for(int i = 0; i < m_nHeight; i++)
			{
				for(int j = 0; j < m_nWidth; j++)
				{
					*pMyBits++ = *pSrc++;
					*pMyBits++ = *pSrc++;
					*pMyBits++ = *pSrc++;
					*pMyBits++ = *pAlpha++;
				}
				pSrc += nLineTail;
			}
			EnableAlphaChannel();
		}
		img.Clear();
	}	
	return TRUE;
}
// return number of faces and score
void detectFaces(const CxImage& img, std::list <Face>& lFaces, int nRot)
{
	if (cascade_el == NULL || cascade_er == NULL || cascade_f == NULL || cascade_m == NULL || cascade_n == NULL)
		init();
	CxImage imgRotated (img);
	imgRotated.Rotate180();
	IplImage *pIplImage = cvCreateImageHeader (cvSize (img.GetWidth(), img.GetHeight()), 8, img.GetBpp() / 8);
	pIplImage->imageDataOrigin = (char*)imgRotated.GetDIB();
	pIplImage->imageData = (char*)imgRotated.GetBits();

	CvMemStorage			*storage;

	/* setup memory storage, needed by the object detector */
	storage = cvCreateMemStorage(0);

	assert(cascade_f && cascade_el && cascade_er && cascade_m && cascade_n && storage);

	/* haar detection for faces */
	CvSeq *pFaces = cvHaarDetectObjects(
		pIplImage, cascade_f, storage,
		1.2, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize( 40, 40 ) );
	if (pFaces->total == 0)
		return;

	CvSeq seqFaces (*pFaces);
	list <CvRect> lrFaces;
	for(int idxFace = 0; idxFace < seqFaces.total; idxFace++ ) {
		CvRect *r = (CvRect*)cvGetSeqElem(&seqFaces, idxFace);
		if (r)
			lrFaces.push_back (*r);
	}

	/* haar detection for eyes */
	list<pair <RECT, RECT>> lpairEyes;
	detect_eyes (storage, pIplImage, lpairEyes);
	
	/* haar detection for mouths */
	list<RECT> lMouths;
	detect_mouths (storage, pIplImage, lMouths);
	
	/* haar detection for noses */
	list<RECT> lNoses;
	detect_noses (storage, pIplImage, lNoses);
	
	for(list <CvRect>::iterator itFace = lrFaces.begin(); itFace != lrFaces.end(); ++itFace) {
		CvRect r (*itFace);
		CvRect rFace (*itFace);
		rFace.x = img.GetWidth() - r.x - r.width;
		rFace.y = img.GetHeight() - r.y - r.height;
		RECT rcFace; 
		rcFace.left = img.GetWidth() - r.x - r.width - 10; rcFace.right = img.GetWidth() - r.x + 10;
		rcFace.top = img.GetHeight() - r.y - r.height - 10; rcFace.bottom = img.GetHeight() - r.y + 10;

		/* detect current eyes */
		pair <RECT, RECT> pairEyes;
		match_eyes_with_face (lpairEyes, rFace, pairEyes);

		/* detect current mouth */
		RECT rcMouth;
		match_mouths_with_face (lMouths, rFace, pairEyes, rcMouth);

		/* detect current nose */
		RECT rcNose;
		match_noses_with_face (lNoses, rFace, pairEyes, rcMouth, rcNose);

		lFaces.push_back (Face (rcFace, pairEyes, rcMouth, rcNose, nRot));
	}
	Face::adjustRectFaces(lFaces);

	cvReleaseMemStorage (&storage);

	cvReleaseImageHeader (&pIplImage);
}