1)点与轮廓的距离及位置关系
计算点与轮廓的距离及位置关系–pointPolygonTest()
double pointPolygonTest(InputArray contour,Point2f pt,bool measureDist);
&&contour 所需检测的轮廓对象
&&pt Point2f类型的pt 待判定位置的点
&&measureDist :是否计算距离的标志,当为true时,计算点到轮廓的最短距离,当为false时,只判定轮廓和点的位置关系,具体关系如下:
&&返回值为-1,表示点在轮廓外部
&&返回值为0 ,表示点在轮廓上
&&返回值为1 表示点在轮廓的内部
注意:如果你不需要知道具体距离,建议将第三个参数设置为false,这样速度会提高2到3倍
2)轮廓矩
0阶矩(m-00)———-为目标区域的质量
1阶矩(m_01,m_10)———-表示目标区域的质心
2阶矩(m_02,m_11,m_20)——–表示旋转半径
3阶矩(m_03,m_12,m_30)———描述目标的方位与斜度,反映目标的扭曲程度
中心矩:构造平移不变性
归一化中心矩:构造尺度 不变性
hu矩:构造旋转不变性
矩的计算–moments()
Moments moments(InputArray array,bool binaryImage=false)
&&array :输入参数,可以是光栅图像或二维数组
&&binaryImage:默认值false,非零像素取其本身值,若为true,则非零像素取1
&&返回值:Moments类的对象,返回对应的轮廓的空间矩/中心矩/和归一化中矩
(最高三阶)
// !spatial moments
double m00,m10,m01,m20,m11,m02,m30,m21,m12,m03;
// !central moments
double mu20,mu11,mu02,mu30,mu21,mu12,mu03;
//!central normalized moments
double nu20,nu11,nu02,nu30,nu21,nu12,nu03;
ex1:
Moments momenr0=moments(contours[i],false);
cout<
//轮廓匹配
Mat srcImg=imread("1.jpg");//读取模板图像
imshow("src",srcImg);
cvtColor(srcImg,srcImg,CV_BGR2GRAY);
threshold(srcImg,srcImg,100,255,CV_THRESH_BINARY);
vector<vector<Point>>contours;
vector<Vec4i>hierarcy;
findCOntours(srcImg,contours,hierarcy,CV_RETR_EXTERNAL,CVCHAIN_APPROX_NONE);
Mat srcImg2=imread("2.jpg");
imshow("src2",srcImg2);
Mat dstImg=srcImg2.clone();
cvtColor(srcImg2,srcImg2,CV_BGR2GRAY);
threshold(srcImg2,srcImg2,100,255,CV_THRESH_BINARY);
vector<vector<Point>contours2;
vector<Vec4i>hierarcy;
findContours(srcImg2,contours2,hierarcy2,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
while(1)
{
for(int i=0;i<contours2.size();i++)
{
double matchRate=matchShapes(contours[0],contours2[i],CV_CONTOURS_MATCH_11,0,0);
cout<<"index="<<i<<"---"<<setiosflags(ios::fixed)<<matchRate<<endl;
if(matchRate<=0.1)
drawContours(dstImg,contours2,i,Scalar(0,255,0),2,8);
imshow("dst",dstImg);
char key=waitKey();
if(key==27)
break;
}
break;
}