2017-04-12 4 views
1

ユニコード名で画像ファイルを読む必要がありますが、openCV関数imreadの画像名の引数は文字列のみをサポートしています。どのように文字列オブジェクトにUnicodeパスを保存することができます。これにはどんな解決策がありますか?imreadを使用してユニコード名を持つ画像ファイルを開きます

+0

あり、適切な解決策になるかもしれませんが、回避策として、あなたは純粋なASCIIファイル名がの外でUnicodeのファイル名を指しているとのシンボリックリンクを作成することができますOpenCVはOSレベルでのインデックス作成中にOpenCVに対処します。これは 'ln -s UnicodeName.jpg ASCIIName.jpg'であり、プログラムで' ASCIIName.jpg'を処理します。 –

答えて

2

次のことが可能です。

  1. ifstreamでファイルを開くcv::imdecodeでそれをデコード
  2. std::vector<uchar>でそれをすべてお読みください。

その下の例は、ifstreamを使用してUnicodeのファイル名でimg2にイメージをロードを参照してください:

#include <opencv2\opencv.hpp> 
#include <vector> 
#include <fstream> 

using namespace cv; 
using namespace std; 

int main() 
{ 
    // This doesn't work with Unicode characters 

    Mat img = imread("D:\\SO\\img\\æbärnɃ.jpg"); 
    if (img.empty()) { 
     cout << "Doesn't work with Unicode filenames\n"; 
    } 
    else { 
     cout << "Work with Unicode filenames\n"; 
     imshow("Unicode with imread", img); 
    } 

    // This WORKS with Unicode characters 

    // This is a wide string!!! 
    wstring name = L"D:\\SO\\img\\æbärnɃ.jpg"; 

    // Open the file with Unicode name 
    ifstream f(name, iostream::binary); 

    // Get its size 
    filebuf* pbuf = f.rdbuf(); 
    size_t size = pbuf->pubseekoff(0, f.end, f.in); 
    pbuf->pubseekpos(0, f.in); 

    // Put it in a vector 
    vector<uchar> buffer(size); 
    pbuf->sgetn((char*)buffer.data(), size); 

    // Decode the vector 
    Mat img2 = imdecode(buffer, IMREAD_COLOR); 

    if (img2.empty()) { 
     cout << "Doesn't work with Unicode filenames\n"; 
    } 
    else { 
     cout << "Work with Unicode filenames\n"; 
     imshow("Unicode with fstream", img2); 
    } 

    waitKey(); 
    return 0; 
} 

あなたはQtのを使用している場合、あなたはQFileでもう少し便利にこれを行うことができますQStringQStringがネイティブにUnicode文字を処理するため、QFileはファイルサイズに簡単にアクセスできます。

0123完全のために

hereあなたはPythonでこれを行う方法を見ることができます

+1

クールなソリューション - 天才! –

+0

ああ、それは動作している! – ahamid555

+0

@ahamid喜んで助けました;) – Miki

関連する問題