1. 程式人生 > >opencv3.0 均值 標準差 函式 mean meanStdDev

opencv3.0 均值 標準差 函式 mean meanStdDev

  //// opencv庫函式 sum 求和  mean  求均值
  Scalar Cbsum2 = sum(Cb);
  Scalar Crsum2 = sum(Cr);
  Scalar Cbmean2 = mean(Cb);
  Scalar Crmean2 = mean(Cr);
  cout << "Cbsum:" << Cbsum2[0] << "  Cbmean:" << Cbmean2[0] << endl;  //"  Cbvar:" << Cbvar << endl;
  cout << "Crsum:" << Crsum2[0] << "  Crmean:" << Crmean2[0] << endl;

  // opencv庫 求標準差函式 
  Mat b_m,b_sd,r_m,r_sd;
  double bm = 0, bsd = 0, rm = 0, rsd = 0;
  meanStdDev(Cb, b_m, b_sd);  // Cb 通道
  meanStdDev(Cr, r_m, r_sd);  // Cr 通道
  bm = b_m.at<double>(0, 0);  bsd = b_sd.at<double>(0, 0);
  rm = r_m.at<double>(0, 0);  rsd = r_sd.at<double>(0, 0);
  cout << "Cb--Mean: " << bm << " , StdDev: " << bsd << endl;
  cout << "Cr--Mean: " << rm << " , StdDev: " << rsd << endl;


mean, meanStdDev計算結果是double型別的。
mean返回的值是Scalar,就是vector型別的陣列。所以當要Scalar的元素,要用[n]方式訪問。
meanStdDev計算的均值和標準差都以Mat形式返回,這裡返回的是1X1的矩陣。所以訪問結果,要訪問Mat的元素。