Ask Your Question

Revision history [back]

In Visual Studio you have to create a new project, which is a Windows Forms Application. Main thing you should consider is that Windows has a weird way of creating GUI, by putting almost all code into the header file, just edit that one and keep the cpp file auto generated.

First step, create a layout in the visual form1.h interface, creating buttons and such like you want your GUI to look like. Be sure to give all your elements a correct name in the properties windows, so that you can easily call them from your software.

Next, do a right click on the header file and select, view code. At the top of your project you need to include the correct openCV modules by their header file, for example:

// includes for openCV functionality in the item handlers
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>

Which is the same as in your console application. Also do not forget to set your linker settings and include directories like in your console applications!

Then 1 big difference is that the header doesn't allow you to do a general 'include namespace cv' on top of your header file. This is not a problem, you should just remember to add cv:: in front of all openCV elements.

Now go back to the visual form and for example double click on a button. This will generate a button handler that connects functionality to that button as an operation. For example you could load a text from a field and show it using openCV using this code into the handler:

// Retrieve the protected string element from the input box
String^ temp = textbox_input->Text;
IntPtr pointer_temp = Marshal::StringToHGlobalAnsi(temp);       
const char* input_location = static_cast<const char*>(pointer_temp.ToPointer());

// Visualize the image using openCV functionality
cv::Mat image = cv::imread(input_location);
cv::imshow("test_window", image);

Now go start and play with this. If you do not get this, then first find yourself a book on winforms applications like this one.