Tag Archives: opencv example

How to capture frame from RTSP Stream witg FFMPEG Api, OpenCV

 

#include <QtCore/QCoreApplication>
#include <QDebug>

#define __STDC_CONSTANT_MACROS
#define __STDC_LIMIT_MACROS
#define UINT64_C
#define WinMain@16

#include "fcntl.h"
int f_desw;

extern "C" {
#include <libavdevice/avdevice.h>
#include <libswscale/swscale.h>
}

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    av_register_all();
    avdevice_register_all();
    avcodec_register_all();
    avformat_network_init();

    const char  *filenameSrc = "rtsp://172.16.6.18:554";

    AVCodecContext  *pCodecCtx;
    AVFormatContext *pFormatCtx = avformat_alloc_context();

    AVCodec * pCodec;
    AVFrame *pFrame, *pFrameRGB;

    if(avformat_open_input(&pFormatCtx,filenameSrc,NULL,NULL) != 0) return -12;
    if(av_find_stream_info(pFormatCtx) < 0)   return -13;
    av_dump_format(pFormatCtx, 0, filenameSrc, 0);
    int videoStream = 1;
    for(int i=0; i < pFormatCtx->nb_streams; i++)
    {
        if(pFormatCtx->streams[i]->codec->coder_type==AVMEDIA_TYPE_VIDEO)
        {
            videoStream = i;
            break;
        }
    }

    if(videoStream == -1) return -14;
    pCodecCtx = pFormatCtx->streams[videoStream]->codec;

    pCodec =avcodec_find_decoder(pCodecCtx->codec_id);
    if(pCodec==NULL) return -15; //codec not found

    if(avcodec_open2(pCodecCtx,pCodec,NULL) < 0) return -16;

    pFrame    = avcodec_alloc_frame();
    pFrameRGB = avcodec_alloc_frame();

    uint8_t *buffer;
    int numBytes;

    AVPixelFormat  pFormat = AV_PIX_FMT_BGR24;
    numBytes = avpicture_get_size(pFormat,pCodecCtx->width,pCodecCtx->height) ; //AV_PIX_FMT_RGB24
    buffer = (uint8_t *) av_malloc(numBytes*sizeof(uint8_t));
    avpicture_fill((AVPicture *) pFrameRGB,buffer,pFormat,pCodecCtx->width,pCodecCtx->height);

    int res;
    int frameFinished;
    AVPacket packet;
    while(res = av_read_frame(pFormatCtx,&packet)>=0)
    {

        if(packet.stream_index == videoStream){

            avcodec_decode_video2(pCodecCtx,pFrame,&frameFinished,&packet);

            if(frameFinished){

                struct SwsContext * img_convert_ctx;
                img_convert_ctx = sws_getCachedContext(NULL,pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,   pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL,NULL);
                sws_scale(img_convert_ctx, ((AVPicture*)pFrame)->data, ((AVPicture*)pFrame)->linesize, 0, pCodecCtx->height, ((AVPicture *)pFrameRGB)->data, ((AVPicture *)pFrameRGB)->linesize);

                cv::Mat img(pFrame->height,pFrame->width,CV_8UC3,pFrameRGB->data[0]); //dst->data[0]);
                cv::imshow("display",img);
                cvWaitKey(1);

                av_free_packet(&packet);
                sws_freeContext(img_convert_ctx);

            }

        }

    }

    av_free_packet(&packet);
    close(f_desw);
    avcodec_close(pCodecCtx);
    av_free(pFrame);
    av_free(pFrameRGB);
    avformat_close_input(&pFormatCtx);

    return a.exec();
}

 

 

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

How to capture frame from webcam witg FFMPEG Api and display image with OpenCV

I will show, how get frame from webcam with FFmpeg Api and processing frame with OpenCV. Capture
We need the name of the device. To get devices name in pc run following code on command line.

C:\Users\Hasanaga-PC>ffmpeg -list_devices true -f dshow -i dummy
......
[dshow @ 02119800] DirectShow video devices
[dshow @ 02119800]  "A4TECH USB2.0 PC Camera"
[dshow @ 02119800]  "MJPEG Camera"
[dshow @ 02119800] DirectShow audio devices
[dshow @ 02119800]  "Microphone (A4TECH USB2.0 Camer"

Continue reading

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Flappy Bird PC version – with QT 4.8 & OpenCV Source code

 

It is a simple game like “Flappy Bird”. Written with OpenCV and QT 4.8 (C++) is only for training.


flappy_bird

 

Continue reading

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

OpenCV – cvHoughLines2 example on QT

#include <QtCore/QCoreApplication>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv/cv.h>


IplImage *image, *image1, *image2;


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);



    image  = cvLoadImage("C:\\Temp\\99.jpg");

    IplImage* image1 = cvCreateImage( cvGetSize(image), 8, 1 );

    IplImage* image2 = cvCreateImage( cvGetSize(image), 8, 3 );

    CvMemStorage* storage = cvCreateMemStorage(0);

    CvSeq* lines = 0;


    cvCanny( image, image1, 50, 200, 3 );

    cvCvtColor( image1, image2, CV_GRAY2BGR );


    lines = cvHoughLines2( image1, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 50, 50, 10 );

    for( int i = 0; i < lines->total; i++ )
    {
        CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
        cvLine( image2, line[0], line[1], CV_RGB(255,0,0), 3, 8 );
    }




    cvShowImage( "Original", image );
    cvShowImage( "cvHoughLines2", image2 );


    return a.exec();

}


 

3

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

OpenCV – cvFilter2D example on QT

#include <QtCore/QCoreApplication>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv/cv.h>

IplImage *image, *image1, *image2;;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    image  = cvLoadImage("C:\\Temp\\99.jpg");

    image1 = cvCreateImage(cvSize(image->width,image->height),8,1);
    image2 = cvCreateImage(cvSize(image->width,image->height),8,1);

    float kernel_vertical[]  = { -1,  0, 1,
                                   -2,  0, 2,
                                   -1,  0, 1};

    float kernel_horizontal[]  = { 1,  2, 1,
                                 0,  0, 0,
                                -1, -2,-1};

    //Vertical filter

    CvMat kernel_ver=cvMat(3,3,CV_32FC1,kernel_vertical);

    cvConvertImage(image,image1,CV_GRAY2BGR);

    cvFilter2D(image1, image1, &kernel_ver, cvPoint(-1,-1));

    cvShowImage("Vertical",image1);

    //Horizontal filter

    CvMat kernel_hor=cvMat(3,3,CV_32FC1,kernel_horizontal);

    cvConvertImage(image,image2,CV_GRAY2BGR);

    cvFilter2D(image2, image2, &kernel_hor, cvPoint(-1,-1));

    cvShowImage("Horizontal",image2);

    return a.exec();

}

 

1

2

3

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS