在轮廓中找到点 [英] Finding Points in Contours
问题描述
所以这里是我用来检测轮廓的代码:
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;
}
这是 Pic / i>,用于测试该程序:输入
And this is the Pic that I used to test the program: Input
我试图获得轮廓作为找到输入的面部的面部表情的初步步骤。这是当我试图运行程序(原始 [left]和输出 [right])时的 Result ):结果
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)).
- 如何在轮廓中找到点(例如,顶部,底部,中心,最左边和最右边 - >基本点以进行几何计算以确定面部表情)。
非常感谢你,如果你会帮助我。到目前为止,这是我能找到轮廓的最好的输出。此外,如果你可以帮助我更准确地提取轮廓,那将是非常感谢。谢谢。 :)
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. :)
推荐答案
cvPoint rightMost = (0, 0);
cvPoint leftMost = (gray->width, 0);
cvPoint bottom = (0, gray->height);
cvPoint top = (0, 0);;
for( CvSeq* current = contours; current != NULL; current = current->h_next )
{
for( int i = 0; i < current->total; i++ )
{
CvPoint* pt = (CvPoint*)cvGetSeqElem( current, i );
int pixVal = (int)(gray->imageData + pt->x * gray->widthStep)[pt->y];
//find the point, which coordinate X is the biggest
if( pt->x > rightMost )
{
rightMost->x = pt->x;
rightMost->y = pt->y;
}
//find the point, which coordinate X is the smallest
if( pt->x < leftMost )
{
leftMost->x = pt->x;
leftMost->y = pt->y;
}
//find the point, which coordinate Y is the biggest
if( pt->y > top )
{
top->x = pt->x;
top->y = pt->y;
}
//find the point, which coordinate Y is the smallest
if( pt->x < bottom )
{
bottom->x = pt->x;
bottom->y = pt->y;
}
}
}
cvPoint ptCenter(cvRound(( rightMost + leftMost ) / 2), ( top + bottom ) / 2 );
我没有尝试这个代码,但我认为这将是有用的!
I haven't tried this code, but I think it will be helpfull!
这篇关于在轮廓中找到点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!