Ask Your Question

How to do OpenCV in Windows Application?

asked 2013-10-16 08:58:40 -0500

Jack Chung gravatar image

updated 2020-11-30 03:42:35 -0500

Hello Guys,

I was using the OpenCV in Console Application and it was working fine, now I have to use Windows Application because I am doing a GUI to work with OpenCV.

How can I do OpenCV work in a Windows Application or How can I do GUI program in a Console Application?

I would appreciate it if someone could help me to solve that.

Thank you

edit retag flag offensive close merge delete


The way I've followed to do the same thing you are asking here is adviced against by most programmers. Anyway I want to share my experience. I used interop, that is I divided my application in a Opencv/c++ module and a c# one. I'm much more productive with visual studio with c# and at the same time I want to keep the more delicate and efficient part completely written in c++, which is the original language of OpenCv. I have no problem because I have described clearly a few functions in the c++ library that allow me the needed level of interaction and there are not bottlenecks. Of course this approach can only work if you know in advance what are this few functions and you don't want a complete control over the framework. Otherwise you can go with a wrapper as Emgu.

Gino Strato gravatar imageGino Strato ( 2015-05-26 05:43:10 -0500 )edit

3 answers

Sort by ยป oldest newest most voted

answered 2013-10-17 06:45:38 -0500

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.

edit flag offensive delete link more



When I include opencv hpp-files I get the following error: ERROR: EMM intrinsics not supported in the pure mode!

Then I changed the Project properties according to a suggestion in another thread: Properties->General->Common Language Runtime Support->Common Language Runtime Support (/clr)

Now I get many other errors, e.g.; error LNK2028: unresolved token (0A000264) "public: void __cdecl cv::Mat::copySize(class cv::Mat const &)" ([email protected]@[email protected]@[email protected]@Z) referenced in function "public: __cdecl cv::Mat::Mat(class cv::Mat const &)" ([email protected]@@[email protected]@@Z)

What do I have to do differently?

Tobbe gravatar imageTobbe ( 2014-10-14 06:13:17 -0500 )edit

The second set of errors are due to the fact that you did not link your opencv libraries properly. Check out this Windows Visual Studio configuration tutorial.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-10-14 06:55:58 -0500 )edit

Hello. Sorry for necroposting, but I have the same issue. I have linked everything: screen1 and screen2 (also /include folder). Clean win32 console project works fine! But blank Windows Forms (CLR with .NET 4) project with only one additional line in stdafx.h "#include <opencv2/core/core.hpp>" has linker errors LNK2028 as if there are no links to /lib folder provided in properties sheet: screen. I'm using Visual Studio 2010 on WinXP.

Distorted gravatar imageDistorted ( 2015-03-03 18:51:15 -0500 )edit

answered 2015-05-24 09:53:34 -0500

pklab gravatar image

If you want to keep your code C++ standard you can use Qt, wxWidgets that are cross platforms or you can also use standard MFC Application and use imshow to show opencv images as separate windows.

With MFC, if you need to show cv::Mat images into your GUI you can draw it directly into DC of a static control of your GUI using StretchDIBits. You can see my own class that performs exactly this task. How to display an OpenCV image or video in your own MFC interface

Anyway, relevant task with GUI are threads in special case if you have to work with videos. The MFC GUI of the OpenCV Background Subtraction Library use a good implementation of worker threads and GUI integration

edit flag offensive delete link more

answered 2013-10-16 13:58:55 -0500

antonio gravatar image

goggle 'opencv with Qt'...there are some nice youtube videos out there...

edit flag offensive delete link more


That is not what the user has asked for, he wants support for openCV with windows application (which is winforms) ...

StevenPuttemans gravatar imageStevenPuttemans ( 2013-10-17 06:35:30 -0500 )edit

Are you a lawyer of the 'user'???...with Qt you can make WINDOWS APPLICATIONS and NOT winforms that you describe!!!! more carefully next time, post your answer and dont criticize with arrogance!!!!!!

antonio gravatar imageantonio ( 2013-10-18 17:05:14 -0500 )edit

Ok I will correct my remark, even if QT can make decent windows applications, your answer is still not conform to the FAQ guidelines for this forum... I didn't critisize with arogance rather with a remark. I downvoted because I think your answer is far from complete and doesn't provide a decent answer like we prefer on this forum. I am not a lawyer but i am a forum moderator which tries to do his best to keep this forum as clean as possible.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-10-20 12:53:48 -0500 )edit

Ok, I correct my remark too: "In the following link the user can find a nice step-by-step tutorial for creating a windows application using OpenCV with Qt: . I hope this helps the user"

antonio gravatar imageantonio ( 2013-10-20 15:22:27 -0500 )edit

Question Tools



Asked: 2013-10-16 08:58:40 -0500

Seen: 19,928 times

Last updated: May 24 '15