zoukankan      html  css  js  c++  java
  • 求取激光光斑质心

    一阶矩求取光斑质心:

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

          

      第二步,计算图像中每个像素与其对应的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

  • 相关阅读:
    色彩空间RGB/CMYK/HSL/HSB/HSV/Lab/YUV基础理论及转换方法:RGB与YUV
    三色视者与四色视者身后的理论基础:色彩原理
    再谈设计原则—7种设计原则学习总结笔记
    sass安装:webpack sass编译失败,node-sass安装失败的终极解决方
    再谈Java数据结构—分析底层实现与应用注意事项
    再谈js对象数据结构底层实现原理-object array map set
    浮点数精度问题透析:小数计算不准确+浮点数精度丢失根源
    再谈编程范式—程序语言背后的思想
    再谈循环&迭代&回溯&递归&递推这些基本概念
    再谈MV*(MVVM MVP MVC)模式的设计原理—封装与解耦
  • 原文地址:https://www.cnblogs.com/IAMSailorMoon/p/15078854.html
Copyright © 2011-2022 走看看