求二值映像的形心

來源:互聯網
上載者:User

  對於一張二值映像而言,假如白色像素地區為目標像素地區,黑色像素為背景,用1表示白色,0表示黑色像素,則映像目標的形心求取公式如下:

其中x0,y0是質心座標,f(x,y)是二值化映像在點(x,y)處的像素值

根據上面的公式,通過像素的遍曆,利用Opencv可以寫出一個函數,來求形心代碼如下:

CvPoint GetCenterPoint(IplImage *src)
{
 int i,j;
 int x0=0,y0=0,sum=0;
 CvPoint center;
 CvScalar pixel;
 for (i=0;i<src->width;i++)
  for(j=0;j<src->height;j++)
  {
   pixel=cvGet2D(src,j,i);
   if(pixel.val[0]==1)
   {
    x0=x0+i;
    y0=y0+j;
    sum=sum+1;
   }
  }
 center.x=x0/sum;
 center.y=y0/sum;
 return center;
}

經測試效果如下:

完整代碼下載:http://download.csdn.net/detail/gxiaob/5149306

參考資料:

http://blog.stevenwang.name/image-centroid-217001.html

 http://bbs.sciencenet.cn/thread-531905-1-1.html

http://dongtingyueh.blog.163.com/blog/static/461945320126238303158/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.