2016-03-31 60 views
1

こんにちは私は3つのヒストグラム(rgb)を.xmlファイルにエクスポートし、次に.xmlから読み込んで別のマトリックスに保存したいと思います。OpenCV filestorage error

コード:私はそれを実行しようとすると

int main(int argc, char** argv) 
{ 
    Mat org,cmp, dst; 

    /// Load image 
    org = imread("arrowr.png"); 
    cmp = imread("cat.jpg"); 

    if (!org.data) 
    { 
     return -1; 
    } 
    /// Separate the image in 3 places (B, G and R) 
    Mat bgr_planes_org[3] = {};   //zmena z vector<Mat> bgr_planes; 
    split(org, bgr_planes_org); 

    /// Establish the number of bins 
    int histSize = 256; 

    /// Set the ranges (for B,G,R)) 
    float range[] = { 0, 256 }; 
    const float* histRange = { range }; 

    bool uniform = true; bool accumulate = false; 

    Mat b_hist_org, g_hist_org, r_hist_org; 


    /// Compute the histograms: 
    calcHist(&bgr_planes_org[0], 1, 0, Mat(), b_hist_org, 1, &histSize, &histRange, uniform, accumulate); 
    calcHist(&bgr_planes_org[1], 1, 0, Mat(), g_hist_org, 1, &histSize, &histRange, uniform, accumulate); 
    calcHist(&bgr_planes_org[2], 1, 0, Mat(), r_hist_org, 1, &histSize, &histRange, uniform, accumulate); 

    // Draw the histograms for B, G and R 
    int hist_w = 512; int hist_h = 400; 
    int bin_w = cvRound((double)hist_w/histSize); 

    Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0)); 

    /// Normalize the result to [ 0, histImage.rows ] 
    normalize(b_hist_org, b_hist_org, 0, histImage.rows, NORM_MINMAX, -1, Mat()); 
    normalize(g_hist_org, g_hist_org, 0, histImage.rows, NORM_MINMAX, -1, Mat()); 
    normalize(r_hist_org, r_hist_org, 0, histImage.rows, NORM_MINMAX, -1, Mat()); 

    Mat mat_r, mat_g, mat_b; 
    string filename = "test.xml"; 
    FileStorage fs(filename, FileStorage::WRITE); 
    fs << "blu" << b_hist_org; 
    fs << "grn" << g_hist_org; 
    fs << "red" << r_hist_org; 
    fs.release(); 

    FileStorage fs(filename, FileStorage::READ); 
    fs["blu"] >> mat_b; 
    fs["red"] >> mat_r; 
    fs["grn"] >> mat_g; 
    fs.release(); 

     cout << mat_r << endl; 
     cout << mat_g << endl; 
     cout << mat_b << endl; 

     system("pause"); 

    return 0; 
} 

それはこの私を投げる ":マイクロソフトC++の例外:ConsoleApplication2.exeで0x00007FF96ECA8A5Cで未処理の例外。メモリ位置0x000000A4D911BF40でのcv ::例外"

FileStorage::READと思っています。私が.xmlから読み込んだセクションにコメントすると、エラーなく動作します。また、私は1つだけの行列を入れて、それを読むときに動作します。

私はまたの.xmlファイルに私はこのようなエクスポートされたデータを持っていることに気づい:

<blu type_id="opencv-matrix"> 
<rows>256</rows> 
<cols>1</cols> 
<dt>f</dt> 
<data> 
"blue matrix data" 
</data></blu> 
<blu>grn</blu> 
<blu type_id="opencv-matrix"> 
<rows>256</rows> 
<cols>1</cols> 
<dt>f</dt> 
<data> 
"green matrix data"  
</data></blu> 
<red type_id="opencv-matrix"> 
<rows>256</rows> 
<cols>1</cols> 
<dt>f</dt> 
<data> 
"red matrix data" 
</data></red> 
</opencv_storage> 

なぜこれが<blu>grn</blu>ありますか?私は、ヘッダが<grn>の私の緑色のマトリックスが欲しいです。 <blu>ヘッダーの下にあるgrnはなぜですか?

openCVによると、各マトリックスには3つのヘッダーが必要です。 http://docs.opencv.org/2.4/doc/tutorials/core/file_input_output_with_xml_yml/file_input_output_with_xml_yml.html

解決策への助力/リンクはすばらしくなるでしょう。

+0

これは私にとってはうまくいきます(ただし、2度目に 'fs'の名前を変更する必要があることは例外です)。エラーが発生した正確なコードを投稿してください。 – Miki

+0

[mcve]の実行方法も参照してください。また、通常、このようなエラーは、releaseまたはviceversaでdebug libsを使用しているためです。または異なるコンパイラでコンパイルされたlibs。あなたのlibパスを表示してください。そしてあなたのVSバージョン – Miki

+0

Wow Miki、あなたは正しいです!私はデバッグとリリースの両方のlibsを持っていた。 (Visual Studio 2015) エラーは発生せず、.xmlファイルも正常に生成されます。解決方法エクスプローラ>プロパティ>リンカ>入力>アディティブ依存関係、リリースiではopencv_world310.libのみがデバッグopencv_world310d.libに残されました。 〜 –

答えて

0

コードは正しいです(ただし、FileStorage fs...を再定義する場合は、名前を変更してください)。

通常、OpenCVでUnhandled exceptionエラーが発生した場合は、リリースでデバッグライブラリを使用しているか、またはviceversaであることを意味します。


コメントから、これは実際問題であることが判明しました。リリースではopencv_world310.libに、デバッグではopencv_world310d.libにのみリンクしてください。

関連する問題