Before you start to read this article, you should have read VS2013 Build OpenCV2.4.9 with CUDA.

Some prerequisites are needed:

1.  Visual C++ Redistributable Packages for Visual Studio 2013

2.  CUDA  cuda_6.5.14_windows_general_64

3. OpenCV 2.4.9

4. Intel TBB  tbb43_20150316oss

 

Here are two sample codes, one is written in C++ dll project(CVision) and the other one is a C# windows form program(CVisionClient)

Now I am going to create a new Visual C++ dll project

Choose DLL

Remember to build with x64 platform for this project, otherwise there will be an error something like “external function” cannot be found

CVision.h

CVision.cpp

Let’s take a look in imread, which passes an image filename to the subroutine

   1: bool ImageProc::imread(wchar_t* txt)
   2: {        
   3:     wstring ws(txt);
   4:     filename = WstringToString(ws);
   5:     image = cv::imread( filename );
   6:     if( !image.data)
   7:     {
   8:         return false;
   9:     }
  10:     else
  11:     {
  12:         return true;
  13:     }        
  14:     return true;
  15: }

One thing should be take care is wide string to string conversion, which is done by WstringToString function.

   1: std::string WstringToString(const std::wstring str)
   2: {   
   3:     unsigned len = str.size() * 4;
   4:     setlocale(LC_CTYPE, "cht");
   5:     char *p = new char[len];
   6:     wcstombs(p,str.c_str(),len);
   7:     std::string str1(p);
   8:     delete[] p;
   9:     return str1;
  10: }

And we display image by another function named showImage

   1: void ImageProc::showImage()
   2: {
   3:     namedWindow( filename, CV_WINDOW_NORMAL );
   4:     imshow(filename, image );
   5: }

Finally don’t forget to select CLR supported, and the C++ dll can be called by C# applications

CVision.dll will be created in the directory shown below if your compiled successfully.

G:\CSharp\MyToolkitd\Release

=======================================================

Now we are ready to create a C# Windows form application

Now we’re going to add a reference to CVision.dll

and using CVision namespace also

   1: private void ToolStripMenuItem_Click(object sender, EventArgs e)
   2:         {
   3:             OpenFileDialog openFileDialog1 = new OpenFileDialog();
   4:             openFileDialog1.InitialDirectory = "c:\\";
   5:             openFileDialog1.Filter = "jpg files (*.jpg)|*.jpg|bmp files (*.bmp)|*.bmp|png files (*.png)|*.png";
   6:             openFileDialog1.FilterIndex = 0;
   7:             openFileDialog1.RestoreDirectory = true;
   8:             if (openFileDialog1.ShowDialog() == DialogResult.OK)
   9:             {
  10:                 try
  11:                 {
  12:                     ImageProc ip = new ImageProc();
  13:                     unsafe
  14:                     {
  15:                         byte[] buf = Encoding.Unicode.GetBytes(openFileDialog1.FileName);
  16:                         
  17:                         fixed (byte* p = &buf[0] )
  18:                         {                                                        
  19:                             if( ip.imread((char *)p))
  20:                                 ip.showImage();                            
  21:                         }            
  22:                                              
  23:                     }                             
  24:                 }
  25:                 catch (Exception ex)
  26:                 {
  27:                     MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
  28:                 }

29: }

  30:         }

In the following picture, you can see that the path of input image including traditional Chinese characters that does not cause any problem.

 

 

References:

C# and the char* mess

wstring与string相互转换

Conversion of wchar_t* to string [duplicate]

文章標籤
全站熱搜
創作者介紹
創作者 me1237guy 的頭像
me1237guy

天天向上

me1237guy 發表在 痞客邦 留言(0) 人氣(1,111)