OpenCV 计算图像的平均梯度

// OpenCV 计算图像的平均梯度

double calcAvG(const cv::Mat& img)
{
 if(img.channels()!=1)
 {
  std::cout<<"输入必须是单通道图像!"<<std::endl;
  return 0.0;
 }


 img.convertTo(img,CV_64FC1);

 double tmp = 0;
 int rows = img.rows-1;
 int cols = img.cols-1;
 for(int i=0;i<rows;i++)
 {
  for(int j=0;j<cols;j++)
  {
   double dx = img.at<double>(i,j+1)-img.at<double>(i,j);
   double dy = img.at<double>(i+1,j)-img.at<double>(i,j);
   double ds = std::sqrt((dx*dx+dy*dy)/2);
   tmp = tmp+ds;
  }
 }
 double imageAvG = tmp/(rows*cols);
 return imageAvG;
}

猜你喜欢

转载自blog.csdn.net/windydreams/article/details/22691349