關於圖像處理中的灰度重心法

概念

對於亮度不均勻的目標(如光斑,光條紋),灰度重心法可按

目標光強分佈求出光強權重質心座標作為跟蹤點,也叫密度質心算法。


將灰度值分佈中的質心記作光條紋的中心

關於圖像處理中的灰度重心法

對於M * N大小的圖像f,像素的灰度值凡是超過閾值T的均參與重心處理,於是重心座標為:

關於圖像處理中的灰度重心法

關於圖像處理中的灰度重心法

關於圖像處理中的灰度重心法

灰度重心法公式

型心法

只可用於二值圖像

關於圖像處理中的灰度重心法

灰度重心法version 1


關於圖像處理中的灰度重心法

灰度重心法version 2


關於圖像處理中的灰度重心法

使用Visual Studio 2019測試

根據灰度重心法version 1找光斑中心
代碼:

<code>#define T 20 //根據實際情況設定固定閾值
Point grayCenter(Mat& img)
{
Mat img_gray;
cvtColor(img, img_gray, COLOR_BGR2GRAY, 0);

Point Center; //中心點
int i, j;
double sumval = 0;
MatIterator_<uchar> it, end;
//獲取圖像各點灰度值總和
for (it = img_gray.begin<uchar>(), end = img_gray.end<uchar>(); it != end; it++)
{
((*it) > T) ? sumval += (*it) : NULL; //小於閾值,取0
}
Center.x = Center.y = 0;
double x = 0, y = 0;
for (int i = 0; i < img_gray.cols; i++)
{
for (int j = 0; j < img_gray.rows; j++)
{
double s = img_gray.at<uchar>(j, i); //取當前點灰度值
if (s < T)
s = 0;
x += i * s / sumval;
y += j * s / sumval;
}
}
Center.x = cvRound(x);
Center.y = cvRound(y);
cout << "rows=" << img_gray.rows << " cols=" << img_gray.cols << endl;
cout << "x=" << x << " y=" << y << endl;
return Center;
}
/<uchar>/<uchar>/<uchar>/<uchar>/<code>

運行結果:


成功找到光斑中心(綠點)


分享到:


相關文章: