Monday, May 2, 2011

Episode_4_Little_Tricks_Capture_Camera_in_WinForm

After you can capture camera with OpenCV in console application (Episode 3). Now I give you some tricks, how to capture camera in windows forms application. This is very important if you want your program to be more complex.
Before I give you the tricks . you must configure OpenCV directories and include OpenCV library to the Linker input (Episode 2). After that, you must change "common language runtime support" to common language runtime support (/clr).
  • General->common language runtime support->common language runtime support(/clr).
picture 0_change_common_language_runtime_support
Now design your form like this (there are button, picture box, and timer). You can get it all in toolbox option, and you just drop it in your windows forms. in picture box, change its size to 320x240 in properties and size mode "Normal" to "StrecthImage". Why? because it need to fit in camera frame size with your picture box so we can get better image.

picture 1_form_application

Double click "Start Button" to start your listing program and modify your listing program such as;

initialize component on top listing program
#pragma once
#include 
#include 

//initialize component here to get access public class
CvCapture* capture;
IplImage* frame;

namespace Tutorial {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

the main program
 private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
capture = cvCaptureFromCAM(0);
timer1->Start();
}

private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
frame = cvQueryFrame(capture);

pictureBox1->Image  = gcnew    //replacement of cvShowImage
System::Drawing::Bitmap(frame->width,frame->height,frame->widthStep,
System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) frame->imageData);
pictureBox1->Refresh();
}
Now you can capture camera in form application, so what do you think?

picture 2_the_result


Heeii !! I have a good news, I have got a new trick
How to convert 1-channel image to 3-channel image

picture 3_grayscale_convert

just add the source code with this "cvMerge".

the main program


private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
 capture = cvCaptureFromCAM(0);
 frame = cvQueryFrame(capture);
 img = cvCreateImage( cvGetSize(frame), 8,1 ); // Allocate a 1-channel byte image
 img2 = cvCreateImage( cvGetSize(frame), 8,3 ); // Allocate a 3-channel float image
}

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
 timer1->Start();
}

private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
 frame = cvQueryFrame(capture);
 cvCvtColor(frame,img,CV_BGR2GRAY);
 cvMerge(img , img, img, NULL, img2);  //Composes a multi-channel array from several single-channel arrays or inserts a single channel into the array.

 pictureBox1->Image  = gcnew    //replacement of cvShowImage
 System::Drawing::Bitmap(img2->width,img2->height,img2->widthStep,
 System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) img2->imageData);
 pictureBox1->Refresh();
}

The Downloads

35 comments:

anonimo said...

AMAZING!!!!

I spent three days searching in google how to do this (show the webcam in "windows form" and not in a separate window using "cvShowImage".) Thank you very much, this is very useful for the work I'm doing in the University. Thanks

Greetings from Chile ;-)

Tomás

Anonymous said...

Hi,
This is great. Thanks for posting.
Did you try to display a video on pictureBox, then using mouse to choose a part of image on pictureBox for tracking? Please share with me.

Unknown said...

Hehe Thx,
I am also trying how to learn using mouse handler on pictureBox. if I know, I will post it again.

Unknown said...

To initialize img & img2, you must put in under "initilize component" maybe like this:

//initialize component here to get access public class
CvCapture* capture;
IplImage* frame;
IplImage* img, *img2;

Unknown said...

How to get Form1 after changing "common language runtime support" to common language runtime support (/clr)? And how to get my toolbox icon to be active because i can't select the icon in the toolbox. All of it are inactive?

Unknown said...

First, I use Windows Form Application for Visual C++. Not using Console or MFC.
http://i42.tinypic.com/10qgsw4.png

And how to active toolbox..
http://i44.tinypic.com/2wfmx6q.png

Archit Sureja said...

I got Errors Like

Error 2 error LNK2028: unresolved token (0A0007B9) "extern "C" void __cdecl cvReleaseCapture(struct CvCapture * *)" (?cvReleaseCapture@@$$J0YAXPAPAUCvCapture@@@Z) referenced in function "private: void __clrcall Tutorial::Form1::exitToolStripMenuItem_Click(class System::Object ^,class System::EventArgs ^)" (?exitToolStripMenuItem_Click@Form1@Tutorial@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z) E:\Document\Visual Studio 2010\Projects\Tutorial\Tutorial\Tutorial.obj Tutorial

I got this type of 23 unresolved token error.

Plz help me

Thank you

Unknown said...

Sorry late..
Sir, what's the version of opencv u use ?
I use opencv 2.1 so u must install it before...

Thank you for visiting my blog :)

Unknown said...

thanks so much..it's very useful for me ^_^

Unknown said...

i got error ::

cannot open " tbb_debug.lib "
please help me ........

Unknown said...

tbb_debug.lib?
havu u tried to download my tutorial's project?

sry, i dont know dani. i nver used with those library in my project..

http://postimg.org/image/7h4767gmf/

Ardy Erdiyanto said...

Sory, maybe too late for Dani, I have same error before but I was fix it..
Dani, you need to download threading building block (http://threadingbuildingblocks.org), extract it, and copy tbb.dll and tbb_debug.dll in opencv directory (..vc10/bin) and also tbb.lib and tbb_debug.dll in lib(..vc10/lib/) directory

Maybe it can help....

ferry said...

I'm sorry but i just got black screen when running your program

Unknown said...

have u tried using console before ?
there are some bugs in opencv2.1 that opencv cant recognize your camera..

i also had some problem like u when i used a new type of camera, but it has already fixed using cmake... and i found that solution in google.

Unknown said...

oh ya.. if you are using new opencv version..
u can try this.. u just ned coverting method.
http://opencvproject.wordpress.com/projects-files/winform/

Derry said...

Nice info, keep doing this :)
Best regard

Mzk said...

Hi, nice post you have there. I have a question though. It seems that the capturing is a bit lag in windows form than in the opencv standard GUI. I have tested it with a video too. It really lags. Do you know how to overcome this?

Regards.

Unknown said...

really ? have u tried my listing program ?
it's run very smooth in my old laptop.
plz check your memory in task manager when your program running, maybe some of your listing program made it overleak memory.

Unknown said...

This is a great tutorial . It is possible to use Mat instead of IplImage?

Unknown said...

yes it can, check my second blog.. but sory i used my native language.

http://opencvproject.wordpress.com/projects-files/winform/

Unknown said...

Do you have any tutorial how to code some functions in opencv to windows form? I mean tutorial for converting console project to windows form.

Unknown said...

i get a error like

prueba.obj: error LNK2028: refers to the symbol (token) unsolved (0A00082A) "public: void __ thiscall cv :: Mat :: copySize (class cv :: Mat const &)" (? copySize @ Mat @ cv @ @ $ $ FQAEXABV12 @ @ Z) in function "public: __ thiscall cv :: Mat :: Mat (class cv :: Mat const &)" (?? 0Mat @ cv @ @ $ ​​$ FQAE @ ABV01 @ @ Z)
1> prueba.obj: error LNK2028: refers to the symbol (token) unsolved (0A00082B) "int __ cdecl cv :: _interlockedExchangeAdd (int *, int)" (? _interlockedExchangeAdd @ Cv @ @ $ ​​$ FYAHPAHH @ Z) in function "public: void __ thiscall cv :: SparseMat :: AddRef (void)" (? AddRef @ SparseMat @ cv @ @ $ ​​$ FQAEXXZ)
1> prueba.obj: error LNK2019: external symbol "int __ cdecl cv :: _interlockedExchangeAdd (int *, int)" (? _interlockedExchangeAdd @ Cv @ @ $ ​​$ FYAHPAHH @ Z) unresolved at that referenced in function "public : void __ thiscall cv :: SparseMat :: AddRef (void) "(? AddRef @ SparseMat @ cv @ @ $ ​​$ FQAEXXZ)
1> prueba.obj: error LNK2019: external symbol "public: void __ thiscall cv :: Mat :: copySize (class cv :: Mat const &)" (? CopySize @ Mat @ cv @ @ $ ​​$ FQAEXABV12 @ @ Z) without address referred to in function "public: __ thiscall cv :: Mat :: Mat (class cv :: Mat const &)" (?? 0Mat @ cv @ @ $ ​​$ FQAE @ ABV01 @ @ Z)

Unknown said...

I got an error when I add other form and put an
" CvCapture* capture; " What should I do ? In this first form it run successfully.

Unknown said...

@jose Bravo try to using opencv2.1 or 2.2 broo..

@Mark villamayor like this ?
http://i.imgur.com/NTGTniH.jpg

listing program for Form2
http://pastebin.com/XGNRwSCt

Unknown said...

Thanks for the responds . I need something like that but when I used cv::Mat Image in form 2 I've got an error.

Unknown said...

for "cv::Mat"
http://pastebin.com/ww04g2qP

Unknown said...

the code doesn't work. the errors in every line is

"error C4430: missing type specifier - int assumed. Note: C++ does not support default-int"

Unknown said...

great work, it work for me

so awesome

Unknown said...

@Mark villamayor dont forget to put in
using namespace cv;
using namespace std;
in your form2

my program,
https://www.dropbox.com/s/pp6isie2jnrd8qi/Camera.rar

Unknown said...
This comment has been removed by the author.
Jefferson said...

Companheiros, parabéns pelo tutorial. Muito obrigado, me ajudou muito em um trabalho da faculdade. Abraços.

nitish said...

Thanks Bro

jpablo said...

Hi Maulana,
thanks for your post, really nice. I was just wondering about something that happen to me and I was curious if the same happen to you as well. When I close the winform the process remains alive (i can see that using task manager), does the same happen to you as well?
thanks.
Pablo

Unknown said...

Thanks for visiting my blog and sorry for my late response.
I have tried in vs2013 + opencv2.43 (windows 8.1) and it didn't happen to me.
have you tried the program with your friends ? i think the problem that your visual studio.. because winform is visual studio application, its not associated with OpenCV exactly,

Unknown said...

Hi maulana, i wanna resize camera screen on my winform, what coding should i add?

Post a Comment