C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,503 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,889 people online right now. Registration is fast and FREE... Join Now!




how to work with webcam?

 

how to work with webcam?

alexax

29 Jun, 2009 - 01:51 AM
Post #1

New D.I.C Head
*

Joined: 28 Jun, 2009
Posts: 12

Hi all, i've been looking for codes that works with webcam using c++ and openCV.. I like to understand how to create an header file. Now my objective is to have the webcam to video left and right images..

Now my code is only capturing. How do i make it into video like talking to someone else and also can enlarge the images?

Something like an webcam which have the magnifier function.

This is my code.
CODE
#include <stdafx.h>
#include <iostream>
#include <cv.h>
#include <cvaux.h>
#include <highgui.h>
#include <cvcam.h>


using namespace std;

int main()

{
    
    cout << "Hello EveryOne!!!\n";        // print to screen


    //getchar();            // wait for a key to be pressed

    CvCapture* capture1 = cvCaptureFromCAM(0);
    CvCapture* capture2 = cvCaptureFromCAM(0);



    IplImage* img1 = 0;
    IplImage* img2 = 0;
    if(!cvGrabFrame(capture1)){              // capture a frame
        printf("Could not grab a frame from camera 1\n\7");
        exit(0);
    }
    if(!cvGrabFrame(capture2)){              // capture a frame
        printf("Could not grab a frame from camera 2\n\7");
        exit(0);
    }

    img1=cvRetrieveFrame(capture1);           // retrieve the captured frame
    img2=cvRetrieveFrame(capture2);

    
    // create a window
    cvNamedWindow("Left");
    cvMoveWindow("Left", 100, 300);

    cvNamedWindow("Right");
    cvMoveWindow("Right", 800, 300);

    // show the image
    cvShowImage("Left", img1 );
    cvShowImage("Right", img2 );

    // wait for a key
    cvWaitKey(0);

    //Release the image
    cvReleaseCapture(&capture1);
    cvReleaseCapture(&capture2);
    return 0;    // the main function returns zero

}


User is offlineProfile CardPM
+Quote Post


c.user

RE: How To Work With Webcam?

29 Jun, 2009 - 04:25 PM
Post #2

D.I.C Regular
***

Joined: 14 Jun, 2009
Posts: 299



Thanked: 24 times
My Contributions
CODE

    for (int i = 0; i < 100; i++) {
        img1 = cvRetrieveFrame(capture1);  // retrieve the frame
        img2 = cvRetrieveFrame(capture2);
  
        // create a window
        cvNamedWindow("Left");
        cvMoveWindow("Left", 100, 300);

        cvNamedWindow("Right");
        cvMoveWindow("Right", 800, 300);

        // show the image
        cvShowImage("Left", img1 );
        cvShowImage("Right", img2);
    }

try this

perhaps
CODE

        //Release the image
        cvReleaseCapture(&capture1);
        cvReleaseCapture(&capture2);

must be in the loop too
User is offlineProfile CardPM
+Quote Post

alexax

RE: How To Work With Webcam?

29 Jun, 2009 - 08:13 PM
Post #3

New D.I.C Head
*

Joined: 28 Jun, 2009
Posts: 12

hey what does the code(the one you provided) do? It seems like the same as mine. And is very slow when building it..

I want to make it into video(meaning can move around) rather than just capture images..

This post has been edited by alexax: 29 Jun, 2009 - 08:23 PM
User is offlineProfile CardPM
+Quote Post

c.user

RE: How To Work With Webcam?

30 Jun, 2009 - 12:15 AM
Post #4

D.I.C Regular
***

Joined: 14 Jun, 2009
Posts: 299



Thanked: 24 times
My Contributions
try to capture few frames, then show them all in queue, then repeat (with release frames or without release)
and only for one window

This post has been edited by c.user: 30 Jun, 2009 - 12:15 AM
User is offlineProfile CardPM
+Quote Post

alexax

RE: How To Work With Webcam?

30 Jun, 2009 - 01:00 AM
Post #5

New D.I.C Head
*

Joined: 28 Jun, 2009
Posts: 12

How do i show them all in queue?
User is offlineProfile CardPM
+Quote Post

nzrock95

RE: How To Work With Webcam?

30 Jun, 2009 - 01:57 PM
Post #6

New D.I.C Head
*

Joined: 25 Jun, 2009
Posts: 4

Well let's think in plain English for a moment.
A video is just a bunch of pictures played back to represent motion, right? So what you want to do is get a picture, show it in a single window (not sure what you were planning on doing with two, but keep it if it's important), and repeat. The code he gave gets a picture, shows it in a window, and repeats that 100 times, making a "video". It may run too fast for you to see what is happening though, so some kind of wait would help. And you can just create your window once instead of 100 times in the loop.

Good luck!
User is offlineProfile CardPM
+Quote Post

c.user

RE: How To Work With Webcam?

30 Jun, 2009 - 04:51 PM
Post #7

D.I.C Regular
***

Joined: 14 Jun, 2009
Posts: 299



Thanked: 24 times
My Contributions
CODE

    CvCapture *capture;
    IplImage *img;

    capture = cvCaptureFromCAM(0);
    cvNamedWindow("main");
    for (int i = 0; i < 100; i++) {
        img = cvRetrieveFrame(capture);
        cvShowImage("main", img);
    }
    cvReleaseCapture(&capture);

try this

This post has been edited by c.user: 30 Jun, 2009 - 04:52 PM
User is offlineProfile CardPM
+Quote Post

c.user

RE: How To Work With Webcam?

1 Jul, 2009 - 10:57 PM
Post #8

D.I.C Regular
***

Joined: 14 Jun, 2009
Posts: 299



Thanked: 24 times
My Contributions
you have no 10 messages, hence
QUOTE

Is it possible to count number of very small fishes if i use webcam with openCV?

it needs capture every frame, make an image from it and process this image, I think
so it joins to camera just in the capture of a picture, all work will be with this picture even if it is something greater than small fishes
User is offlineProfile CardPM
+Quote Post

alexax

RE: How To Work With Webcam?

2 Jul, 2009 - 01:43 AM
Post #9

New D.I.C Head
*

Joined: 28 Jun, 2009
Posts: 12

hmm... I don't really get what you mean.. It needs to capture every frame and from there process it.
User is offlineProfile CardPM
+Quote Post

c.user

RE: How To Work With Webcam?

2 Jul, 2009 - 07:28 PM
Post #10

D.I.C Regular
***

Joined: 14 Jun, 2009
Posts: 299



Thanked: 24 times
My Contributions
you get the frame, then translate it to a picture, then processing this picture: get objects, compare them for concurrences, count percent and probability of equality
all your work with camera is only capture the frame
User is offlineProfile CardPM
+Quote Post

alexax

RE: How To Work With Webcam?

2 Jul, 2009 - 07:58 PM
Post #11

New D.I.C Head
*

Joined: 28 Jun, 2009
Posts: 12

How am i going to translate it to a picture and processing it?

This post has been edited by alexax: 2 Jul, 2009 - 07:59 PM
User is offlineProfile CardPM
+Quote Post

c.user

RE: How To Work With Webcam?

2 Jul, 2009 - 11:05 PM
Post #12

D.I.C Regular
***

Joined: 14 Jun, 2009
Posts: 299



Thanked: 24 times
My Contributions
I think you should make a program for comparing two simple pictures, first picture has circle second has square and your program can compare them and return 1 if they are equal and 0 if they are not
try it, if you will have could then your fishes will be a groups of figures

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/8/09 05:04AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month