source: eviacam/trunk/fuentes/creavision/crvcamera.cpp @ 29

Last change on this file since 29 was 29, checked in by mabarracus, 4 years ago

added eviacam content

  • Property svn:executable set to *
File size: 2.8 KB
Line 
1/////////////////////////////////////////////////////////////////////////////
2// Name:        crvcamera.cpp
3// Purpose: 
4// Author:      Cesar Mauri Loba (cesar at crea-si dot com)
5// Modified by:
6// Created:     30/05/2008
7// Copyright:   (C) 2008-10 Cesar Mauri Loba - CREA Software Systems
8//
9//  This program is free software: you can redistribute it and/or modify
10//  it under the terms of the GNU General Public License as published by
11//  the Free Software Foundation, either version 3 of the License, or
12//  (at your option) any later version.
13//
14//  This program is distributed in the hope that it will be useful,
15//  but WITHOUT ANY WARRANTY; without even the implied warranty of
16//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17//  GNU General Public License for more details.
18//
19//  You should have received a copy of the GNU General Public License
20//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21/////////////////////////////////////////////////////////////////////////////
22#include "crvcamera.h"
23#include "crvimage.h"
24#include <cv.h>
25#include <sys/timeb.h>
26#include <sys/types.h>
27#include <cassert>
28
29// Return timestamp in ms
30static long long GetTime (void)
31{
32        struct timeb now;       
33        ftime(&now);
34        return now.time*1000 + now.millitm;
35}
36
37CCamera::CCamera() throw (camera_exception)
38{
39        m_RealWidth=0;
40        m_RealHeight= 0;
41        m_RealFrameRate= 0.0f;
42        m_LastRealFrameRate= 0.0f;
43        m_lastTimeStamp= GetTime();
44        m_horizontalFlip= false;
45}
46
47CCamera::~CCamera(void)
48{
49        // Should call Close but is not done here
50        // because DoClose is virtual. We rely on
51        // derived classes
52}
53
54bool CCamera::Open()
55{
56        bool retval= DoOpen();
57        if (retval) m_lastTimeStamp= GetTime();
58        return retval;
59}
60
61void CCamera::Close()
62{
63        DoClose();
64}
65
66bool CCamera::QueryFrame (CIplImage& image)
67{
68        if (!DoQueryFrame(image)) return false;
69
70        assert(image.Initialized());
71
72        PostQueryFrame(image.ptr());
73
74        return true;
75}
76
77IplImage* CCamera::QueryFrame()
78{
79        IplImage* pImage= DoQueryFrame();
80        if (!pImage) return NULL;
81
82        PostQueryFrame(pImage);
83
84        return pImage;
85}
86
87void CCamera::PostQueryFrame(IplImage* pImage)
88{
89        // Update real size
90        m_RealWidth= pImage->width;
91        m_RealHeight= pImage->height;
92
93        // Update real FPS
94        long long now= GetTime();
95        long long elapsedTime= now - m_lastTimeStamp;
96        m_lastTimeStamp= now;
97        m_LastRealFrameRate= m_RealFrameRate;
98
99        float weight= ((float) elapsedTime / 1000.0f) * 1.5f;
100        if (weight> 1.0f) weight= 1.0f;
101        if (elapsedTime> 0)
102                m_RealFrameRate= (1000.0f / (float) elapsedTime) * weight + m_LastRealFrameRate * (1.0f - weight);
103        else
104                m_RealFrameRate= 0;
105
106        // Flip image when needed to provide an image with top-left origin
107        if ( pImage->origin == 1 ) {
108                if (m_horizontalFlip) 
109                        cvFlip (pImage, NULL, -1);
110                else 
111                        cvFlip (pImage, NULL, 0);
112
113                pImage->origin= 0;
114        }
115        else 
116                if (m_horizontalFlip) 
117                        cvFlip (pImage, NULL, 1);
118}
Note: See TracBrowser for help on using the repository browser.