C++ 在等高线中寻找点
下面是我用来检测轮廓的代码:C++ 在等高线中寻找点,c++,opencv,expression,contour,C++,Opencv,Expression,Contour,下面是我用来检测轮廓的代码: IplImage*DetectAndDrawQuads(IplImage*img) { CvSeq*等高线; CvSeq*结果; CvMemStorage*storage=cvCreateMemStorage(0); IplImage*ret=cvCreateImage(cvGetSize(img),8,3); IplImage*temp=cvCreateImage(cvGetSize(img),8,1); CVT颜色(img、temp、CV_bgr2灰色); cv
IplImage*DetectAndDrawQuads(IplImage*img)
{
CvSeq*等高线;
CvSeq*结果;
CvMemStorage*storage=cvCreateMemStorage(0);
IplImage*ret=cvCreateImage(cvGetSize(img),8,3);
IplImage*temp=cvCreateImage(cvGetSize(img),8,1);
CVT颜色(img、temp、CV_bgr2灰色);
cvFindContours(温度、存储和轮廓、尺寸(CvContourt)、CV_RETR_列表、CV_链近似值、cvPoint(0,0));
while(等高线)
{
结果=cvApproxPoly(等高线,尺寸(CvContour),存储,CV_POLY_近似值(DP),CVContourp周长(等高线)0.10,0)//*0.2
如果((结果->总计)==4)
{
CvPoint*pt[4];
对于(int i=0;ih_next;
}
cvReleaseImage(&temp);
cvReleaseMemStorage(&storage);
返回ret;
}
int main()
{
IplImage*img=cvLoadImage(“D:\\Database\\eye2.jpg”);
IplImage*ContourDrawed=0;
cvNamedWindow(“原件”);
cvShowImage(“原件”,img);
轮廓绘制=检测和绘制四边形(img);
CVD(“等高线”);
cvShowImage(“轮廓”,轮廓绘制);
cvWaitKey(0);
返回0;
}
这是我用来测试程序的图片:
我尝试获取轮廓,作为查找输入人脸面部表情的初步步骤。这是我尝试运行程序(原始[左]和输出[右])时的结果:
正如你所看到的,在二值图像中似乎留下了一些噪声(在我的查找轮廓程序(上面的代码)中输入之前,我实际上对其进行了预处理)
我的问题是:
cvPoint rightmest=(0,0);
cvPoint最左边=(灰色->宽度,0);
cvPoint bottom=(0,灰色->高度);
cvPoint top=(0,0);;
用于(CvSeq*current=轮廓;current!=NULL;current=current->h_next)
{
对于(int i=0;itotal;i++)
{
CvPoint*pt=(CvPoint*)cvGetSeqElem(电流,i);
int pixVal=(int)(灰色->图像数据+pt->x*灰色->宽度步长)[pt->y];
//找到坐标X最大的点
如果(pt->x>最右边)
{
最右边->x=pt->x;
最右侧->y=pt->y;
}
//找到坐标X最小的点
如果(pt->x<最左边)
{
最左边->x=pt->x;
最左侧->y=pt->y;
}
//找到坐标Y最大的点
如果(pt->y>顶部)
{
顶部->x=pt->x;
顶部->y=pt->y;
}
//找到坐标Y最小的点
如果(pt->x<底部)
{
底部->x=pt->x;
底部->y=pt->y;
}
}
}
cvPoint ptCenter(cvRound((最右边+最左边)/2),(顶部+底部)/2);
我还没有尝试过这段代码,但我认为它会很有帮助!您找到了上述问题的答案吗??
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 );