2016-05-12 61 views
0

這是預期的我的代碼的行爲:查找平均轉換視頻輸入

「我應該得到一個RGB2HSV轉換後的視頻和原始視頻從我的主要程序,並設計出可以找到一個函數的所有H的指,S和V值,併爲每個幀生成1 * 3矩陣。「

這實際上是使用PCA對火與非火之間的對象進行分類。我已經完成了MATLAB中的特徵提取,並且在visual studio中使用C++代碼對PCA係數進行了decalred。顯然,代碼是無錯誤的,但是當我調試運行它時,它會給出附加照片中可以看到的錯誤。

此外,其餘代碼已正確執行,沒有錯誤。 問題在哪裏。附上我的代碼

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

+0

請閱讀http://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-errors – AdrianHHH

回答

0

該對話框報告說,一個異常被拋出。目前,重點應放在瞭解異常來自何處,爲什麼以及程序的真正問題。它可能不在您發佈的代碼部分。

查看調用堆棧(您可以使用Visual Studio打開的另一個窗口),然後雙擊堆棧以查看程序正在執行的操作以及原因。

您也可以打開一個例外窗口(Ctl + Alt + E)並禁用某些異常中斷(如果存在異常處理程序)。 enter image description here 如果沒有異常的處理程序,那麼您可以捕捉它並查看異常的詳細信息。

採取下列簡單的程序,例如:

#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將顯示此: example of exception being thrown

現在在調用堆棧(左上),我們可以看到,我們有main()調用function2(),呼叫function1()這是拋出異常。

最後,如果程序繼續,它將輸出文本My exeption,因爲我們在main()中捕獲它。

2

非常重要!

您無法將平均色調計算爲線性均值!

HSV是一個圓柱座標系統。極軸由角度(Hue)和長度(飽和度)表示。縱軸是長度(值)。

Hue is the angle of a cylindrical coordinate system.

舉例來說,如果你有2個像素。像素1的色調爲0.9。像素2的色調爲0.9。這兩種都是「微紅」的顏色。 (在上面的色輪上,我們可以說20和340度)

線性平均值爲0.5是青色,這絕對不是紅色。

正確的意思是0。0,這正好在色輪上的0.1和0.9之間!

度及值都是線性軸,這樣,就可以計算出它們的平均值很乾脆:

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 ... 
} 
+1

相關維基百科文章:[平均數量](https://en.wikipedia.org /維基/ Mean_of_circular_quantities)。 – horchler

+0

謝謝@horchler。絕對要在我自己的代碼庫中添加它作爲參考。 – Juderb