Using OpenCV/Visual C++/CLR on .NetFramework
To use OpenCV in Visual C++/CLR, the following three setting must be made.
(1) Setting the name of the directory of DLL files at variable Path.
(2) Setting the name of the directory for header files of OpenCV.
(3) Setting the libraries of OpenCV.
In the following, it is assumed that the extracted files of OpenCV are copied into folder C:\opencv2410, which has been created by me.
For example, DLL files are in C:\opencv2410\build\x86\vc12\bin (Figure 1).

Figure 1
(1) Setting the name of the directory of DLL
files at variable Path
Select gControl Panel|System and Security|System|Advanced system settings|h, then System Properties dialog window will appear.
Click Environment Variable button, then Environment Variables dialog window will appear (Figure 2).

Figure 2
Select gPathh in System variables pane, then click Edit button. Edit System Variable dialog window will appear (Figure 3).

Figure 3
Set the string that show the directory of DLL files, gC:\opencv2410\build\x86\vc12\binh in case of Figure 1.
Then, click OK button.
Completing the setting of variable Path, restart the PC.
(2) Setting the
name of the directory for header files of OpenCV
The directory of header files and libraries, which will be linked in building the project, are set for properties of the project.
Construct a new project like Figure 4.

Figure 4
How to construct a new project is explained at this website.
How to prepare components of .NetFramework is explained at this website.
Click and mark the name of the project in Solution Explorer pane (Figure 5).

Figure 5
Select gPROJECT|Propertiesh (Figure 6).

Figure 6
Click the white triangle at the left of gConfiguration Propertiesh (Figure 7).

Figure 7
Click the cell at the right of gAdditional Include Directoriesh (Figure 8).

Figure 8
Click the button with a check mark (Figure 9).

Figure 9
Click the string gEditch (Figure 10).

Figure 10
Click the button with a folder icon on Additional Include Directories Window (Figure 11).

Figure 11
Click the button with string gch (Figure 12).

Figure 12
Select Directory window will appear (Figure 13).

Figure 13
Select folder gC:\opencv2410\build\includeh (This choice depends on the PC used), then click Select folder button (Figure 14).

Figure 14
Click OK button (Figure 15).

Figure 15
Click Apply button (Figure 16).
2
Figure 16
(3) Setting the
libraries of OpenCV
Libraries, which will be linked in building the project, are stored in folder \lib, which include two types of libraries. One type has a file name like g*d.libh, the other without edf at the end(Figure 17).

Figure 17
Our project is of gDebugh configuration (Figure 19), so only libraries with file names g*d.libh will be used. All libraries with name g*d.libh are copied into folder g\libdh, which is created by me (Figure 18).

Figure 18
In the following, this folder will be used.
Click and mark the project name in Solution Explorer pane (Figure 19).

Figure 19
Select gPROJECT|Add Existing Itemch (Figure 20).

Figure 20
Add Existing Items window appears (Figure 21).

Figure 21
Select all the libraries in folder \libd, then click Add button (Figure 22).

Figure 22
Added items to the project are displayed in Solution Explorer pane (Figure 23).

Figure 23
The codes of form.h of the project (Figure 4) are as follows:
#pragma once
#include <stdlib.h> // rand()
#include "common.h" // IplImage * cv_img;
namespace CLRSample {
.
.
.
#pragma endregion
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
cv_img = cvLoadImage("TestImg.bmp", CV_LOAD_IMAGE_COLOR);
if (cv_img == NULL){
MessageBox::Show("cvLoadImage error !");
return;
}
Bitmap ^ bm = gcnew Bitmap(cv_img->width, cv_img->height, cv_img->widthStep, Imaging::PixelFormat::Format24bppRgb, IntPtr(cv_img->imageData));
pictureBox1->Left = 0;
pictureBox1->Top = 0;
pictureBox1->Width = bm->Width;
pictureBox1->Height = bm->Height;
pictureBox1->Image = bm;
this->AutoSize = true;
srand(0);
}
double urand(){
return rand() / double(RAND_MAX);
}
private: System::Void MyForm_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e) {
if (cv_img != NULL)
cvReleaseImage(&cv_img);
}
private: System::Void pictureBox1_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
int r = int(50.0 + 100.0 * urand());
int cr = int(255.0 * urand()), cg = int(255.0 * urand()), cb = int(255.0 * urand());
cvCircle(cv_img, cvPoint(e->X, e->Y), r, CV_RGB(cr, cg, cb), CV_FILLED, 8);
Bitmap ^ bm = gcnew Bitmap(cv_img->width, cv_img->height, cv_img->widthStep, Imaging::PixelFormat::Format24bppRgb, IntPtr(cv_img->imageData));
pictureBox1->Image = bm;
}
};
}
A picture created by OpenCV is stored in IplImage * cv_img, which is defined and declared in common.cpp and common.h, other files than the form file. Defining or declaring out of the area of the declaration of a ref class form in the form file may cause a trouble. To avoid this type of trouble, separate files are prepared to define or declare a global variable IplImage * cv_img. Listings of common.h and common.cpp are as follows:
============== common.h ==================
#ifndef COMMON_CK
#define COMMON_CK
#include <opencv2/opencv.hpp>
extern IplImage * cv_img;
#endif
==========================================
=============== common.cpp ==============
#include "common.h"
IplImage * cv_img;
==========================================
To display the picture created by OpenCV, the graphic data cv_img->imageData is set a Bitmap object Bitmap ^ bm, then bm is set at pictureBox1->Image. The pictureBox1 displays the picture.
Build and run the project, the following window will appear (Figure 24).

Figure 24
Click on the picture, then a circle with random length of diameter and color will be plotted (Figure 25).

Figure 25
The program (project) files are zipped into CLRSample.zip. The extracted project from the down loaded zipped file can be opened by Visual Studio 2013.
If errors are displayed when opening the project, these errors can be ignored.
About errors in opening a project, see this website.
(4) How to find the header file
To find the header file in which a function, e.g. cvLoadImage, is declared, open a folder and View page (Figure A1).

Figure A1
Click Options icon, then Folder Options window will be appear.
Check gAlways searchch in Search page, then click Apply button.
Open gC:\opencv2410\build\includeh folder (Figure A2).

Figure A2
Set string gcvLoadImageh as that to be searched, then the file names that include string gcvLoadImageh will be shown (Figure A3).

Figure A3
In Fugure A3, two file names are shown. Double click the first name gcore.hpph, then choice of the program will be required (Figure A4).

Figure A4
Select Visual Studio, then the file will be shown (Figure A5).

Figure A5
Search string gcvLoadImageh. String gcvLoadImageh is in the comment, so this file may not be the file to be included.
Double click the next file name ghighgui_c.hh in Figure A3, then search string gcvLoadImageh (Figure A6).

Figure A6
String gcvLoadImageh is appears in function declaration, so this file is to be included.
In the sample file gcommon.hh, opencv.hpp is include, in which gopencv2/highgui/highgui_c.hh is included (Figure A7).

Figure A7
Explanations about functions of OpenCV are prepared by *.pdf files in folder g\doch (Figure A8).

Figure A8