求取激光光斑质心

一阶矩求取光斑质心:

  第一步,计算图像中所有像素的灰度值之和

      

  第二步,计算图像中每个像素与其对应的x坐标的乘积之和;每个像素与其对应的y坐标的乘积之和

      

       

  第三步,分别计算质心坐标的x,y

      

         

 二阶矩求取光斑质心:

  第一步,和一阶矩求解过程一样,计算图像中所有像素的灰度值之和

      

  第二步,计算图像中每个像素与其对应的x坐标的平方的乘积之和;每个像素与其对应的y坐标的平方的乘积之和

      

       

  第三步,分别计算质心坐标的x,y

       

       

 完整代码:

 1 // 质心检测.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
 2 //
 3 #include<opencv2/opencv.hpp>
 4 #include <iostream>
 5  
 6 using namespace cv;
 7 using namespace std;
 8  
 9 Point2d First_moment_calculation_centroid(Mat Img);
10 Point2d Second_moment_calculation_centroid(Mat Img);
11  
12 int main()
13 {
14     Point2d point1;
15     Mat Img = imread("高斯光斑2.tif");
16     //point1 = First_moment_calculation_centroid(Img);
17     point1 = Second_moment_calculation_centroid(Img);
18     circle(Img, point1, 1, Scalar(0, 255, 0), -1); //第五个参数设为-1,表明这是个实点。
19     imshow("Img", Img);
20     cout << "质心坐标:"<< point1 << endl;
21     waitKey();
22 }
23  
24 //一阶矩检测高斯光斑质心
25 Point2d First_moment_calculation_centroid(Mat Img)
26 {
27     if (Img.channels() != 1)    //判断输入图像是否为灰度图,若不是转成灰度图
28         cvtColor(Img, Img, COLOR_BGR2GRAY);
29     
30     int AllPointValue = 0;
31     int x_AllPointValue = 0;
32     int y_AllPointValue = 0;
33     Point2d point;
34     
35     for (int i = 0; i < Img.rows; i++)    //计算图像所有点的的灰度值之和
36     {
37         for (int j = 0; j < Img.cols; j++)
38         {
39             AllPointValue += Img.at<uchar>(i, j);
40         }
41     }
42     for (int i = 0; i < Img.rows; i++)    //计算图像点所有点灰度值与其坐标的乘积之和
43     {
44         for (int j = 0; j < Img.cols; j++)
45         {
46             x_AllPointValue += Img.at<uchar>(i, j) * j;
47             y_AllPointValue += Img.at<uchar>(i, j) * i;
48         }
49     }
50  
51     //计算质心坐标并存储
52     point.x = static_cast<double>(x_AllPointValue) / AllPointValue;
53     point.y = static_cast<double>(y_AllPointValue) / AllPointValue;
54  
55     return point;
56 }
57  
58 //二阶矩检测高斯光斑质心
59 Point2d Second_moment_calculation_centroid(Mat Img)
60 {
61     if (Img.channels() != 1)    //判断输入图像是否为灰度图,若不是转成灰度图
62         cvtColor(Img, Img, COLOR_BGR2GRAY);
63  
64     unsigned long long int AllPointValue = 0;
65     unsigned long long int x_AllPointValue = 0;
66     unsigned long long int y_AllPointValue = 0;
67     Point2d point;
68  
69     for (int i = 0; i < Img.rows; i++)    //计算图像所有点的的灰度值之和
70     {
71         for (int j = 0; j < Img.cols; j++)
72         {
73             AllPointValue += Img.at<uchar>(i, j);
74         }
75     }
76     for (int i = 0; i < Img.rows; i++)    //计算图像点所有点灰度值与其坐标的乘积之和
77     {
78         for (int j = 0; j < Img.cols; j++)
79         {
80             x_AllPointValue += static_cast<long long int>(Img.at<uchar>(i, j)) * j * j;
81             y_AllPointValue += static_cast<long long int>(Img.at<uchar>(i, j)) * i * i;
82         }
83     }
84  
85     //计算质心坐标并存储
86     point.x = sqrt(static_cast<double>(x_AllPointValue) / AllPointValue);
87     point.y = sqrt(static_cast<double>(y_AllPointValue) / AllPointValue);
88  
89     return point;
90 }


————————————————
版权声明:本文为CSDN博主「星尘亦星辰」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Mrweng1996/article/details/103015683

原文地址:https://www.cnblogs.com/IAMSailorMoon/p/15078854.html