在等高线中查找点
Finding Points in 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:输入
我正试图获得轮廓,作为找到输入人脸面部表情的初步步骤。这是我尝试运行程序时的结果(原始[左]和输出[右]):结果
正如你所看到的,二进制图像中似乎留下了一些噪声(在输入到我的find contour程序(上面的代码)之前,我实际上对其进行了预处理)。
我的问题是:
- 如何在轮廓中找到点(例如,顶部、底部、中心、最左侧和最右侧-->进行几何计算以确定面部表情的关键点)
如果你能帮助我,非常感谢。到目前为止,这是我在寻找轮廓方面能产生的最好的输出。此外,如果你能帮助我更准确地提取轮廓,我们将不胜感激。谢谢。:)
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 );
我还没有尝试过这个代码,但我认为它会很有用!
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 在C++中查找文件
- 模板元程序查找相似的连续类型名称
- 在UNIX系统中使用DIR查找文件的字节大小
- 查找最接近的大于当前数字的数字的索引
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 查找后更改类变量
- 使用正则表达式regex_search在字符串中查找字符串
- 使用gcc从静态链接的文件中查找可选符号
- 在C++中查找范围的长度
- 算法问题:查找从堆栈中弹出的所有序列
- 在Windows中查找扬声器输出的当前音量级别
- 如何在C++中使用X509证书模在令牌中查找私钥
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 在 for 循环中查找问题时遇到困难
- 如何在文件中查找字节序列
- 使用OpenCV C++查找等高线中的极值点
- 正在查找父等高线
- 在等高线中查找点