c++ - 在轮廓中寻找点

标签 c++ opencv expression contour

下面是我用来检测轮廓的代码:

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

这是我用来测试程序的图片:Input

我正在尝试获取轮廓作为查找输入面部表情的初步步骤。这是我尝试运行程序时的 Result(Original [左] 和 Output [右] ): Result

如您所见,二进制图像中似乎存在一些噪声(在将其输入到我的查找轮廓程序(上面的代码)中之前,我实际上对其进行了预处理)。

我的问题是:



  1. 如何找到轮廓中的点(例如,顶部、底部、中心、最左边和最右边 --> 进行几何计算以确定面部表情的基本点)。

如果你能帮助我,非常感谢。到目前为止,这是我可以生成的关于查找轮廓的最佳输出。另外,如果您能帮助我更准确地提取轮廓,那将不胜感激。谢谢。 :)

最佳答案

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

我还没有尝试过这段代码,但我认为它会很有帮助!

关于c++ - 在轮廓中寻找点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8998816/

相关文章:

c++ - 如何从 std::wstring _TCHAR [] 转换?

python - 如何在 opencv 2.4.11 python 中调整轮廓大小? (目标 : Object extraction)

c# - 如何重构linq表达式树

c++ - make_unique : Why is f(new T) exception safe 的异常安全

c# - 如何在 C# 和非托管 C++ 库之间传递对指针的引用?

c++ - pow(num,2) 的效率?

opencv - 对静态相机的背景图像进行建模以检测移动物体

c++ - 为什么用QT编译OpenCV?

postgresql - 让 LIKE 搜索使用 citext 表达式上的索引

c# - 获取 Linq 表达式的别名