寻找 HSV 转换视频提要的均值

Finding mean of HSV converted video feed

这是我的代码的预期行为:

"I am supposed to get a RGB2HSV converted video and original video from my main program and design a function that may find mean of all H, S and V values and generate 1*3 matrix for each frame. "

这实际上是使用PCA对物体进行火灾和非火灾分类。我在 MATLAB 中完成了特征提取,并在 visual studio 的 c++ 代码中标出了 PCA 系数。显然代码没有错误,但是当我调试到 运行 它时,它给出了可以在附加照片中看到的错误。

此外,其余代码已正确执行,没有错误。 问题出在哪里。附上我的代码

void pca_decide(Mat &Threshdimg , Mat &Original)
{
//declare master pca matrix

double  pca_data[9] = { -0.5398, -0.4189, 0.7302, -0.0365, 0.8782, 0.4768, 0.8410, -0.2307, 0.4893 };
Mat pca = Mat(3, 3, CV_32F, pca_data);

//declaring mean fire hsv values multiplied with pca in matlab
double fire_pca_data[3] = { 0.7375, -0.0747,0.6608 };
Mat fire_pca = Mat(1, 3, CV_32F, fire_pca_data);

//declaring mean non-fire hsv values multiplied with pca in matlab
double  nfire_pca_data[3] = { 0.4389,-0.0874, 0.6240 };
Mat nfire_pca = Mat(1, 3, CV_32F, nfire_pca_data);

//generating current image hsv values in euclidean space

Mat image_pca;
double  rows = Threshdimg.rows;
double  cols = Threshdimg.cols;

vector<Mat> hsv_planes;
split(Threshdimg, hsv_planes);
Mat h = hsv_planes[0]; // H channel h is a 2D matrix
Mat s = hsv_planes[1]; // S channel
Mat v = hsv_planes[2]; // V channel

Scalar h_mean_image = sum(h)/ (rows*cols); // here I need to sum all the rows and columns 
Scalar s_mean_image = sum(s)(rows*cols);
Scalar v_mean_image = sum(v)(rows*cols);
Scalar  HSV_mean_data[3] = { h_mean_image, s_mean_image, v_mean_image };
Mat HSV_mean = Mat(1, 3, CV_32F, HSV_mean_data);
multiply(pca, HSV_mean, image_pca);

//finding difference with fire_pca 
float diff_fire;
diff_fire = norm(image_pca, fire_pca, NORM_L2);


//finding differene with non_fire_pca
float diff_non_fire;
diff_non_fire = norm(image_pca, nfire_pca, NORM_L2);

if (diff_fire > diff_non_fire)
    putText(Original, "Fire Detected", Point(0, 50), 2, 1, Scalar(255, 0, 0), 2);
else
    putText(Original, "Fire Not Detected", Point(0, 50), 2, 1, Scalar(0, 255, 0), 2);
}

the error that i get when I debug

对话框报告正在抛出异常。现在的重点应该放在了解异常的来源、原因以及程序的真正问题是什么。它可能不在您发布的代码部分。

查看调用堆栈(另一个 window 您可以使用 Visual Studio 打开)并在堆栈上下双击以查看您的程序正在做什么以及为什么。

您还可以打开异常 window (Ctl+Alt+E) 并禁止某些异常中断(如果有异常处理程序)。 如果没有异常的处理程序,那么您可以捕获它并查看异常的详细信息。

以下面的简单程序为例:

#include <iostream>
#include <exception>

void function1()
{
    throw std::exception("My exeption\n");
}

void function2()
{
    function1();
}

int
main()
{
    try {
        function2();
    }
    catch (const std::exception& e) {
        std::cout << e.what();
    }
}

Visual studio 将显示:

现在在调用堆栈(左上角)中我们可以看到我们有 main() 调用 function2(),调用 function1() 抛出异常。

最后,如果程序继续,它将输出文本 My exeption,因为我们在 main().

中捕获了它

非常重要!

您不能将平均色调计算为线性平均值!

HSV是圆柱坐标系。极轴由角度(色调)和长度(饱和度)表示。纵轴是一个长度(Value)。

例如,如果您有 2 个像素。像素 1 的色调为 0.9。 Pixel 2 的色调为 0.9。这两种都是 "reddish" 颜色。 (在上面的色轮上,我们可以说是 20 度和 340 度)

线性平均值是 0.5 是青色,绝对不是红色。

正确的平均值是 0.0,恰好在色轮上介于 0.1 和 0.9 之间!

Saturation 和 Value 都是线性轴,因此,您可以非常简单地计算它们的平均值:

meanSaturation = sum ( s ) / ( rows * cols );
meanValue = sum ( v ) / ( rows * cols );

要计算平均色调,您必须使用一些三角函数:

#include <math.h>

#define PI 3.14159265

void YourFunction ( )
{
  // ... The beginning of the code ...

  // Calculate the sine and cosine of each hue value.
  hueSin = sin ( h / ( 2 * PI ) );
  hueCos = cos ( h / ( 2 * PI ) );

  // Calculate the mean sine and mean cosine.
  hueSinMean = sum ( hueSin ) / ( rows * cols );
  hueCosMean = sum ( hueCos ) / ( rows * cols );

  // Retrieve the mean hue by calculating the mean sine and cosine.
  // This will calculate the mean in radians, on a scale from 0 to 2.
  // Divide by 2 * PI to recover the original scale 
  hueMean = atan2 ( hueCosMean , hueSinMean );

  // Account for negative hue values
  if ( hueMean < 0 )
    hueMean = hueMean + 2 * PI;
  end

  // Convert back to range [ 0 , 1 ].
  hueMean = hueMean / ( 2 * PI );

  // ... The beginning of the code ...
}