Thread: OpenCV and detecting a person with a webcam

  1. #1
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288

    OpenCV and detecting a person with a webcam

    Code:
    #include "opencv2/video/tracking.hpp"
    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/imgproc/imgproc_c.h"
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define WIN_32_LEAN_AND_MEAN
    
    
    #include <windows.h>
    
    
    
    
    int main( )
    {
        CvCapture * webcam = cvCreateCameraCapture(0);
        
        if (!webcam)
            return 1;
        
        for ( ; ; ) 
        {
            IplImage* image1 = cvQueryFrame( webcam );
            
            Sleep(2000);
            
            IplImage* image2 = cvQueryFrame( webcam );
            
            if (image1) 
            {
                if (image2) 
                {
                } else break;
            
            } else break;
        }
        
        return 0;
    }

    The above code is all I have so far. I got confused when I got to the part where my book on C tells me I need to use the cvCalcOpticalFlowFarneback() function to compare 2 consecutive images from the webcam and create the optical flow, and then measure movement between the frames to detect whether or not a person is moving through the room. I have no idea how to do this, and the OpenCV wiki wasn't very descriptive on how to set it up for beginners. Honestly, the parameters just confused me and I didn't see a return value listed, or how you would get data from it. Please help me continue in the right direction.

  2. #2
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    No one?

  3. #3
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Firstly, find the documentation for cvCalcOpticalFlowFarneback, figure out how to call it correctly, then figure out how to determine whether a person is moving from the information that the function gives you.

    It seems like you're unfamiliar with C. I strongly suggest learning the language as well as its standard library before you use complicated libraries like this. You could start doing that by reading "The C Programming Language (second edition)" and trying its exercises.

  4. #4
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    I'm not unfamiliar with C. Far from it. I've been using it for over a year. I'm just new to OpenCV and I didn't understand the wiki documentation for that function. I've read close to 3 books on C, so I don't really think another one would help me anymore. I just wish it gave more details in the wiki on how to get the correct parameters, because it didn't mention how you know the values. As I said, it didn't give much details on the return information either.

    http://docs.opencv.org/modules/video..._tracking.html

    That was what I found, and frankly, MSDN is way better about explaining it then they are. I really just need someone to explain more about it before I can use what I need to.
    Last edited by HelpfulPerson; 07-03-2013 at 11:29 AM.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It would be helpful if you provided links to this wiki, and any resource material (library docs, etc) that might help others help you. I imagine there aren't many forum members familiar with this library, but there are plenty of forum members who could likely help you reason a solution to your problem, despite not knowing the library, by seeing the reference material.

  6. #6
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by Matticus View Post
    It would be helpful if you provided links to this wiki, and any resource material (library docs, etc) that might help others help you. I imagine there aren't many forum members familiar with this library, but there are plenty of forum members who could likely help you reason a solution to your problem, despite not knowing the library, by seeing the reference material.
    Probably, the best documentation I could find through google was the one above on the wiki, the rest of them were either for python or C++ mainly.

    I also found this sample code, but I don't quite understand it fully.

    Code:
    #include "opencv2/imgproc/imgproc_c.h"
    #include <stdio.h>
    
    
    static void help(void)
    {
        printf(
                "\n This program demonstrate dense \"Farneback\n optical flow\n"
                "It read from camera 0, and shows how to use and display dense Franeback optical flow\n"
                "Usage: \n"
                "./fback_c \n");
    
    
    }
    
    
    static void drawOptFlowMap(const CvMat* flow, CvMat* cflowmap, int step,
                        double scale, CvScalar color)
    {
        int x, y;
        (void)scale;
        for( y = 0; y < cflowmap->rows; y += step)
            for( x = 0; x < cflowmap->cols; x += step)
            {
                CvPoint2D32f fxy = CV_MAT_ELEM(*flow, CvPoint2D32f, y, x);
                cvLine(cflowmap, cvPoint(x,y), cvPoint(cvRound(x+fxy.x), cvRound(y+fxy.y)),
                     color, 1, 8, 0);
                cvCircle(cflowmap, cvPoint(x,y), 2, color, -1, 8, 0);
            }
    }
    
    
    int main( int argc, char** argv )
    {
        CvCapture* capture = cvCreateCameraCapture(0);
        CvMat* prevgray = 0, *gray = 0, *flow = 0, *cflow = 0;
        (void)argc; (void)argv;
    
    
        help();
    
    
        if( !capture )
            return -1;
    
    
        cvNamedWindow("flow", 1);
    
    
        for(;;)
        {
            int firstFrame = gray == 0;
            IplImage* frame = cvQueryFrame(capture);
            if(!frame)
                break;
            if(!gray)
            {
                gray = cvCreateMat(frame->height, frame->width, CV_8UC1);
                prevgray = cvCreateMat(gray->rows, gray->cols, gray->type);
                flow = cvCreateMat(gray->rows, gray->cols, CV_32FC2);
                cflow = cvCreateMat(gray->rows, gray->cols, CV_8UC3);
            }
            cvCvtColor(frame, gray, CV_BGR2GRAY);
    
    
            if( !firstFrame )
            {
                cvCalcOpticalFlowFarneback(prevgray, gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
                cvCvtColor(prevgray, cflow, CV_GRAY2BGR);
                drawOptFlowMap(flow, cflow, 16, 1.5, CV_RGB(0, 255, 0));
                cvShowImage("flow", cflow);
            }
            if(cvWaitKey(30)>=0)
                break;
            {
            CvMat* temp;
            CV_SWAP(prevgray, gray, temp);
            }
        }
        cvReleaseCapture(&capture);
        return 0;
    }

  7. #7
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    I've read close to 3 books on C, so I don't really think another one would help me anymore.
    I think you'd be surprised.

    I just wish it gave more details in the wiki on how to get the correct parameters, because it didn't mention how you know the values. As I said, it didn't give much details on the return information either.
    Assuming your trouble is with the first three parameters, it looks like you'll need to find a function that will convert the IplImages received from the webcam to CvArrs (OpenCV should provide functions for doing this). I believe pointers to those should be passed as the first two arguments, and the third argument should be a pointer to another CvArr object that will provide storage for the output array.
    Last edited by Barney McGrew; 07-04-2013 at 04:40 AM. Reason: fixed a quote

  8. #8
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Sorry, but that page linked to looks just like any other API documentation I've ever seen (if you ignore the horrendous font sizing/colouring) - it specified parameters, types and returns quite simply (and the function you mention, returns void in C). Hell, it's even being nice and providing direct C++/C/Python prototypes for its functions. The example code you posted shows how it's used in a real program. Beyond that, I can't see what there is that we can help you with specifically - you have to understand the library you are using, what a Farneback flow is and why you need it, and the ins-and-outs of precisely what you're trying to achieve. The actual nuts and bolts of putting it together once you know that - well, that's just a matter of plugging the right things into the right variables and shoving it to the right functions.

    OpenCV is a very powerful and complex library. Basically you just found the control panel to a nuclear reactor and are now asking why they don't just have "start/stop" buttons on it and have to make things complicated. But you've "read three books on how to press buttons and have been pressing buttons for over a year now".

    Welcome to the world of the programmer. You have a library. You have a language that you can interface with that library in. You have an API specification. Off you go and make it work. If you don't know what you're making work, maybe you should be learning that before you jump into a library hoping it'll just do everything for you. This is the real learning part here, whether you're an experienced programmer or not (hint: I've been programming for 20 years since I was a kid, I've been through a degree in Mathematics and Computer Science, but though I could probably cobble something together here, I'd have to go off and read up on the subject and the library before I started just bashing on it - I have absolutely no idea what a Farneback flow is, for instance).

    That library is quite nicely documented and I know of lots of people in the computer vision field that use it every day. You think this is bad? Try writing an app using OpenSSL using just the official documentation. That will open your eyes.

    Hint: Go through the parameters of the first function that does the things you need to do. Find out what types they are. Find out what functions return or create those types for you. Follow the chain back until it leads to something that you have all the information to supply. When you have all the parameters and something to plug into them, give it a shot. And then that's where the real work begins of what to plug in to actually make it work how you'd like.

    Welcome to programming against external libraries. Did your books cover that yet?

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  9. #9
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by ledow View Post
    Sorry, but that page linked to looks just like any other API documentation I've ever seen (if you ignore the horrendous font sizing/colouring) - it specified parameters, types and returns quite simply (and the function you mention, returns void in C). Hell, it's even being nice and providing direct C++/C/Python prototypes for its functions. The example code you posted shows how it's used in a real program. Beyond that, I can't see what there is that we can help you with specifically - you have to understand the library you are using, what a Farneback flow is and why you need it, and the ins-and-outs of precisely what you're trying to achieve. The actual nuts and bolts of putting it together once you know that - well, that's just a matter of plugging the right things into the right variables and shoving it to the right functions.

    OpenCV is a very powerful and complex library. Basically you just found the control panel to a nuclear reactor and are now asking why they don't just have "start/stop" buttons on it and have to make things complicated. But you've "read three books on how to press buttons and have been pressing buttons for over a year now".

    Welcome to the world of the programmer. You have a library. You have a language that you can interface with that library in. You have an API specification. Off you go and make it work. If you don't know what you're making work, maybe you should be learning that before you jump into a library hoping it'll just do everything for you. This is the real learning part here, whether you're an experienced programmer or not (hint: I've been programming for 20 years since I was a kid, I've been through a degree in Mathematics and Computer Science, but though I could probably cobble something together here, I'd have to go off and read up on the subject and the library before I started just bashing on it - I have absolutely no idea what a Farneback flow is, for instance).

    That library is quite nicely documented and I know of lots of people in the computer vision field that use it every day. You think this is bad? Try writing an app using OpenSSL using just the official documentation. That will open your eyes.

    Hint: Go through the parameters of the first function that does the things you need to do. Find out what types they are. Find out what functions return or create those types for you. Follow the chain back until it leads to something that you have all the information to supply. When you have all the parameters and something to plug into them, give it a shot. And then that's where the real work begins of what to plug in to actually make it work how you'd like.

    Welcome to programming against external libraries. Did your books cover that yet?
    No, none of my books covered external libraries. And sadly, most of my books never helped on the Windows API either ( I taught myself a little on it though ). Nevertheless, my point was that I know the basics. Whether I know what would be considered intermediate, I don't know, but I am sure that I know all the basics that I will ever need to use. Such as, linked lists, ternary operator, some of the standard functions ( I have a reference for that, so I don't memorize all of them ), arrays, structures, unions, bit-fields, etc. You're right though, I have not used a complex external library before, but I assure you that I know C well enough to try.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. USB webcam mac opencv
    By averma007 in forum C Programming
    Replies: 1
    Last Post: 06-21-2012, 07:47 AM
  2. c with opencv
    By smuthukumarbsc in forum C++ Programming
    Replies: 1
    Last Post: 09-01-2011, 10:31 PM
  3. opencv
    By Dibyayan Chakra in forum C Programming
    Replies: 1
    Last Post: 05-02-2011, 09:47 AM
  4. OpenCV .....
    By ejohns85 in forum C++ Programming
    Replies: 0
    Last Post: 05-05-2010, 07:41 AM
  5. OpenCV C++ interface
    By cph in forum C++ Programming
    Replies: 0
    Last Post: 11-07-2009, 03:34 AM

Tags for this Thread