CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: RTP and opencv

  1. #1
    Join Date
    Jul 2014
    Posts
    2

    RTP and opencv

    Hi everyone,

    I have never submitted to forums before so please bare with me as I am fairly new to C++ and I have an issue with some code that is really stopping me. I am working on some code (opencv) that captures video from a web cam and (hopefully) inserts the video data into an RTP packet which is sent over a network to a server where it is displayed. The code I have compiles and runs up to the point where the video data is to be inserted into the RTP packet, then an exception is thrown. I can see no way to sort this out and I was hoping that someone could see where I was going wrong with this snippet of code and how to fix it.
    Any help or pointers would be greatly appreciated. The piece of code where I think the problem is, is below:



    for ( ; ; )

    {

    int width, height; //, nchannels, step, offset;
    width = 0;
    height = 0;
    int i, j;

    double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
    double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

    std::cout << "Frame size : " << dWidth << " x " << dHeight << std::endl;

    Mat frame;

    bool bSuccess = cap.read(frame); // read a new frame from video
    std::cout << frame.rows << " " << frame.cols << std::endl;


    if (!bSuccess) //if not success, break loop
    {
    std::cout << "Cannot read a frame from video stream" << std::endl;
    break;
    }

    if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
    {
    cout << "esc key is pressed by user" << endl;
    break;
    }

    CvCapture* capture;// Defines capture
    capture = cvCreateCameraCapture(0);// Captures from the webcam
    if(!capture){
    return -1;
    }
    IplImage* bgr_frame = cvQueryFrame(capture);// Ths initializes the camera read

    int nchannels, step, offset;
    int r_ch, b_ch, g_ch;

    bgr_frame = cvQueryFrame( capture );


    width = bgr_frame->width;
    height = bgr_frame->height;
    nchannels = bgr_frame->nChannels;
    step = bgr_frame->widthStep;

    for(i = 0 ; i < height ; i++)
    {
    uchar* data = (uchar*)(bgr_frame->imageData + i*step);
    for( j = 0 ; j < width ; j++)
    {
    offset = j * nchannels;
    b_ch = data[offset];
    g_ch = data[offset + 1];
    r_ch = data[offset + 2];
    }
    }
    for(i=0 ; i < 640 ; i++)
    {
    for(j=0 ; j < 480 ; j++)
    {
    v = frame.at<uchar> (i,j);
    }
    }

    printf("\nSending packet %d/%d\n");
    //printf("\nSending packet %d/%d\n",i,num);

    // send the packet
    status = sess.SendPacket((void *)v,10000,10000,false,10000); // The video data is inserted here (v)

    checkerror(status);
    imshow("MyVideo", frame); //show the frame in "MyVideo" window
    sess.BeginDataAccess();




    The exception that is thrown is in the mat.hpp file and is:



    template<typename _Tp> inline _Tp& Mat::at(int i0, int i1)
    {
    CV_DbgAssert( dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] &&
    (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) &&
    CV_ELEM_SIZE1(DataType<_Tp>:epth) == elemSize1());
    return ((_Tp*)(data + step.p[0]*i0))[i1];
    }





    Any help or pointers greatly appreciated.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: RTP and opencv

    The piece of code where I think the problem is, is below:
    When posting code, please use code tags. Go Advanced, select the code and click '#'.

    Code:
    v = frame.at<uchar> (i,j);
    I suspect this is the line causing the problem. You are determining the height and width of the read video frame (into variable frame). Yet when you come to access frame using .at you assume a size of 640 x 480. From using the debugger, what condition in the assert is causing the issue?

    You also have some code that refers to camera capture (variable capture) yet I don't see where the info obtained regarding capture is used - or indeed the capture data itself apart from determining some info?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jul 2014
    Posts
    2

    Re: RTP and opencv

    Quote Originally Posted by 2kaud View Post
    When posting code, please use code tags. Go Advanced, select the code and click '#'.

    Code:
    v = frame.at<uchar> (i,j);
    I suspect this is the line causing the problem. You are determining the height and width of the read video frame (into variable frame). Yet when you come to access frame using .at you assume a size of 640 x 480. From using the debugger, what condition in the assert is causing the issue?

    You also have some code that refers to camera capture (variable capture) yet I don't see where the info obtained regarding capture is used - or indeed the capture data itself apart from determining some info?

    Hi 2Kaud,

    Many thanks for answering my post, its really appreciated. Using the debugger the condition in the assert that is causing the issue is:
    Code:
    CV_DbgAssert( dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] &&
    I see what you mean regarding the variable capture. Maybe I should have put:

    Code:
    frame = bgr_frame;
    and inserted it into the code as:

    Code:
    bgr_frame = cvQueryFrame( capture );
    Please let me know what you think I should do

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured