Monthly Archives: March 2014

Read Frame from uEye camera with uEye SDK, OpenCv

#include <QtCore/QCoreApplication>


#include <uEye.h>
#include <uEye_tools.h>
#include <ueye_deprecated.h>
#include <wchar.h>
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include <opencv2/highgui/highgui.hpp>
#include <QDebug>



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

     HIDS hCam = 0;
     char* imgMem;
     int memId;
     if(is_InitCamera (&hCam, NULL)!= IS_SUCCESS){
       return 0;
     }

     int img_width=640, img_height=360, img_bpp=24, img_step, img_data_size;
     is_AllocImageMem(hCam, img_width, img_height, img_bpp, &imgMem, &memId);
     is_SetImageMem (hCam, imgMem, memId);
     is_SetDisplayMode (hCam, IS_SET_DM_DIB);
     is_SetColorMode (hCam, IS_CM_RGB8_PACKED);
     is_SetImageSize (hCam, img_width, img_height);


     double enable = 1;
     double disable = 0;
     is_SetAutoParameter (hCam, IS_SET_ENABLE_AUTO_GAIN, &enable, 0);
     is_SetAutoParameter (hCam, IS_SET_ENABLE_AUTO_WHITEBALANCE, &enable, 0);
     is_SetAutoParameter (hCam, IS_SET_ENABLE_AUTO_FRAMERATE, &disable, 0);
     is_SetAutoParameter (hCam, IS_SET_ENABLE_AUTO_SHUTTER, &disable, 0);
     is_SetAutoParameter (hCam, IS_SET_ENABLE_AUTO_SENSOR_GAIN, &enable, 0);
     is_SetAutoParameter (hCam, IS_SET_ENABLE_AUTO_SENSOR_WHITEBALANCE,&enable,0);
     is_SetAutoParameter (hCam, IS_SET_ENABLE_AUTO_SENSOR_SHUTTER, &disable, 0);

double FPS,NEWFPS;
FPS = 15;
double fps;
is_SetFrameRate(hCam,FPS,&NEWFPS);

             double parameter = 50;
             int error = is_Exposure(hCam, IS_EXPOSURE_CMD_SET_EXPOSURE, (void*) &parameter, sizeof(parameter));
             if (error != IS_SUCCESS) {
                     qDebug() << "): failed" <<parameter;
             }

error = is_Exposure(hCam, IS_EXPOSURE_CMD_GET_EXPOSURE, (void*) &parameter, sizeof(parameter));

UINT uiCaps = 0;

  INT nRet = is_Focus (hCam, FDT_CMD_GET_CAPABILITIES, &uiCaps, sizeof (uiCaps) );

     if (nRet == IS_SUCCESS && (uiCaps & FOC_CAP_AUTOFOCUS_SUPPORTED))
     {
     qDebug() << "If supported, enable auto focus";
     nRet = is_Focus (hCam, FOC_CMD_SET_DISABLE_AUTOFOCUS, NULL, 0);
     }

     nRet = is_SetGamma(hCam,600);
     enable = 2;
char filename[512];

for(int ii=0; ii<100; ii++)
{
       if(is_FreezeVideo(hCam, IS_WAIT) == IS_SUCCESS){
            void *pMemVoid; //pointer to where the image is stored
            is_GetImageMem (hCam, &pMemVoid);
            IplImage * img;
            img=cvCreateImage(cvSize(img_width, img_height), IPL_DEPTH_8U, 3); 
            img->nSize=112;
            img->ID=0;
            img->nChannels=3;
            img->alphaChannel=0;
            img->depth=8;
            img->dataOrder=0;
            img->origin=0;
            img->align=4;
            img->width=img_width;
            img->height=img_height;
            img->roi=NULL;
            img->maskROI=NULL;
            img->imageId=NULL;
            img->tileInfo=NULL;
            img->imageSize=3*img_width*img_height;
            img->imageData=(char*)pMemVoid;  //the pointer to imagaData
            img->widthStep=3*img_width;
            img->imageDataOrigin=(char*)pMemVoid; //and again
            //now you can use your img just like a normal OpenCV image
            cvNamedWindow( "A", 1 );
            cvShowImage("A",img);
            cv::waitKey(1);
       }
       else{
            qDebug() << "ERROR FREEZE";
       }
}

    is_ExitCamera(hCam);
    return a.exec();
}

Share and Enjoy

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

Capture opencv frame from Ximea MU9PC camera

 

#include <QtCore/QCoreApplication>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <QDebug>
#include <string>

#include <xiApi.h>
#include <xiExt.h>


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


    XI_IMG image;
            image.size = sizeof(XI_IMG);
            image.bp = NULL;
            image.bp_size = 0;





            XI_RETURN stat = XI_OK;

            DWORD dwNumberOfDevices = 0;
            stat = xiGetNumberDevices(&dwNumberOfDevices);

            HANDLE handle=NULL;
            // open cameras

            stat = xiOpenDevice(0, &handle);

            char serial_number1[100]="";
            xiGetParamString(handle, XI_PRM_DEVICE_SN, serial_number1, sizeof(serial_number1));





            //Setting parameters for camera
            stat = xiSetParamInt(handle, XI_PRM_TRG_SOURCE, XI_TRG_SOFTWARE);

            stat = xiSetParamInt(handle, XI_PRM_GPO_SELECTOR, 2);

            stat = xiSetParamInt(handle, XI_PRM_GPO_MODE,  XI_GPO_FRAME_ACTIVE_NEG);


            // Setting "exposure" parameter

            stat = xiSetParamInt(handle, XI_PRM_EXPOSURE, 0.04);



            stat = xiSetParamInt(handle, XI_PRM_GAIN, 5);




            stat = xiStartAcquisition(handle);





            int ival;
            xiGetParamInt( handle, XI_PRM_EXPOSURE, &ival);




            int isColor = 0;

            stat = xiGetParamInt(handle, XI_PRM_IMAGE_IS_COLOR, &isColor);


            if(isColor)	// for color cameras
            {
                    // default image format RGB24
                    stat = xiSetParamInt( handle, XI_PRM_IMAGE_DATA_FORMAT, XI_RGB24);
                    stat = xiSetParamInt( handle, XI_PRM_AUTO_WB, 1);
            }



            stat = xiSetParamInt(handle, XI_PRM_DOWNSAMPLING_TYPE, XI_SKIPPING);
            stat = xiSetParamInt(handle, XI_PRM_DOWNSAMPLING, 4);



           int w;
           xiGetParamInt( handle, XI_PRM_WIDTH, &w);
           int h;
           xiGetParamInt( handle, XI_PRM_HEIGHT, &h);



            stat = xiSetParamInt( handle, XI_PRM_WIDTH, w);
            stat = xiSetParamInt( handle, XI_PRM_HEIGHT, h);




            Sleep(123); // wait for right moment to trigger the exposure

            // trigger acquisition on Master camera
            int retry=300;
            while (retry--)
            {
                    xiSetParamInt(handle, XI_PRM_TRG_SOFTWARE, 0);
                    stat = xiGetImage(handle, 10, &image);
                    if (stat!=XI_OK)
                            printf("Error after xiGetimage (%d)\n",stat);
                    else
                    {
                        IplImage * frame;
                        frame = cvCreateImage(cvSize( image.width, image.height), IPL_DEPTH_8U,3);
                        memcpy( frame->imageData, image.bp, image.width*image.height*3);
                        
                        cvShowImage("top",frame);
                        cvReleaseImage(&frame);
                        cvWaitKey(1);
                    }
            }

            if (handle) xiCloseDevice(handle);




    return a.exec();
}

 

 

 

Share and Enjoy

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

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