So here is the code that i used to detect the contours:
IplImage* DetectAndDrawQuads(IplImage* img)
{
CvSeq* contours;
CvSeq* result;
CvMemStorage *storage = cvCreateMemStorage(0);
IplImage* ret = cvCreateImage(cvGetSize(img), 8, 3);
IplImage* temp = cvCreateImage(cvGetSize(img), 8, 1);
cvCvtColor(img, temp, CV_BGR2GRAY);
cvFindContours(temp, storage, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
while(contours)
{
result = cvApproxPoly(contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.10, 0); //*0.2
if((result->total) == 4)
{
CvPoint *pt[4];
for(int i=0;i<4;i++)
pt[i] = (CvPoint*)cvGetSeqElem(result, i);
cvLine(ret, *pt[0], *pt[1], cvScalar(255));
cvLine(ret, *pt[1], *pt[2], cvScalar(255));
cvLine(ret, *pt[2], *pt[3], cvScalar(255));
cvLine(ret, *pt[3], *pt[0], cvScalar(255));
}
contours = contours->h_next;
}
cvReleaseImage(&temp);
cvReleaseMemStorage(&storage);
return ret;
}
int main()
{
IplImage* img = cvLoadImage("D:\\Database\\eye2.jpg");
IplImage* contourDrawn = 0;
cvNamedWindow("original");
cvShowImage("original", img);
contourDrawn = DetectAndDrawQuads(img);
cvNamedWindow("contours");
cvShowImage("contours", contourDrawn);
cvWaitKey(0);
return 0;
}
And this is the Pic that I used to test the program: Input
I am trying to get the contours as a preliminary step in finding the facial expression of an inputted face. And this is the Result when i tried to run the program (Original [left] and Output [right]): Result
As you can see there seems to be some noise left in the binary image (Which I actually preprocessed before it is being inputted in my find contours program (the codes above)).
My Question is:
- How to find the points in the contours (e.g, top, bottom, center, leftmost, and rightmost --> essential points to make geometrical calculations to determine facial expression).
Thank you very much if you will help me. So far, this is the best output i could generate regarding finding contours. Also if you can help me extract the contours more accurately then that will be very much appreciated. Thank you. :)