Ask Your Question

How to transfer Image from webcam to gray image ?

asked 2014-09-04 08:48:22 -0500

nhat.truong gravatar image

updated 2014-09-04 10:16:12 -0500

Hi everybody1 I obtain an image from my webcam and then transfer it to a gray image to process. Here is my code:

int main()
    CvCapture *capture = cvCreateCameraCapture(0);
    IplImage *src = cvCreateImage(cvSize(50,50),8,1);
    IplImage *src1 = cvCreateImage(cvSize(50,50),8,3);
    while (true)
        src1 = cvQueryFrame(capture);
        char c = cvWaitKey(33);
        if(c == 27)



When I compile my program it's no error but when I Run this program then It will terminate. I don't no Why but I think the error happen at line " cvCvtColor(src1,src,CV_BGR2GRAY) " because if I delete this line then I can obtain image from webcam normally. Everyone can help me correct this error ! Have a great day :D

edit retag flag offensive close merge delete



why not you try to use updated c++ style API?

Balaji R gravatar imageBalaji R ( 2014-09-04 10:21:02 -0500 )edit

Where did you create your display windows?

unxnut gravatar imageunxnut ( 2014-09-04 12:37:06 -0500 )edit

@unxnut they are created implicitly when calling cvShowImage. However I think he forgot to add a c5vWaitKey(1) to ensure the drawing method got its time to visualize.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-09-04 15:08:02 -0500 )edit

@StevenPuttemans I added line " char c = cvWaitKey(33) " but my program still suspend. I don't know why ?

nhat.truong gravatar imagenhat.truong ( 2014-09-04 22:41:31 -0500 )edit
  1. move the cvShowImage(.....); lines above cvWaitKey(33); line.
  2. Try not specifying cvCreateImage(cvSize(50,50),8,3); as IplImage *src1 ;

May be this may work

jamesnzt gravatar imagejamesnzt ( 2014-09-05 00:55:57 -0500 )edit

@jamesnzt I tried following your method but this program still suspended. When I debug, it appears the notification : Unhandled exception at 0x76b12eec in OpenCV3.exe: Microsoft C++ exception: cv::Exception at memory location 0x0062f94c.. Can you help me ? Thanks so much :D

nhat.truong gravatar imagenhat.truong ( 2014-09-05 03:24:53 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-09-05 03:36:37 -0500

updated 2014-09-05 03:37:27 -0500

Just simply change your complete code to the C++ API. It will make everything run just fine.

Try this:

 int main()
    VideoCapture capture (0);
    Mat src1, src2;
    while (true)
       capture >> scr1;
       imshow("TestWebCam", src1);
       imshow("TestWebCam2", src2);

       int key = waitKey(10);
       if(key == 27)
edit flag offensive delete link more


@StevenPuttemans Thanks so much :D but I still can't know why my program didn't run. I think " IplImage " is like with " Mat " . Can you tell me about this problem ?

nhat.truong gravatar imagenhat.truong ( 2014-09-05 04:18:36 -0500 )edit

IplImage is the old C-structure. It is the old API of OpenCV 1.x days. However still plenty of samples on the net available. The newer OpenCV 2.x API uses C++. Best guess is to check the docs to see what the C++ alternative is to a C item.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-09-05 04:51:54 -0500 )edit

thank you ! Have a great day

nhat.truong gravatar imagenhat.truong ( 2014-09-05 05:04:21 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2014-09-04 08:48:22 -0500

Seen: 529 times

Last updated: Sep 05 '14