CVCamCapture.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: CVCamCapture.cpp
37 // Author: Pedram Azad
38 // Date: 2005
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include "CVCamCapture.h"
47 
48 #include "Image/ByteImage.h"
49 #include "Image/IplImageAdaptor.h"
50 #include "Image/ImageProcessor.h"
51 #include "Threading/Mutex.h"
52 
53 #include <cvcam.h>
54 #include <highgui.h>
55 #include <cv.h>
56 
57 #include <memory.h>
58 
59 
60 
61 // ****************************************************************************
62 // Global and static variables
63 // ****************************************************************************
64 
67 static bool bFirstFrame = false;
68 
69 
70 // ****************************************************************************
71 // Static methods
72 // ****************************************************************************
73 
74 static void callback(IplImage *pIplImage)
75 {
76  if (!pIplImage)
77  return;
78 
79  CByteImage *pImage = IplImageAdaptor::Adapt(pIplImage);
80 
81 
82  cvcam_critical_section.Lock();
83 
84  if (pCapturedImage && (pCapturedImage->width != pImage->width || pCapturedImage->height != pImage->height || pCapturedImage->type != pImage->type))
85  {
86  delete pCapturedImage;
87  pCapturedImage = 0;
88  }
89 
90  if (!pCapturedImage)
91  pCapturedImage = new CByteImage(pImage);
92 
93  unsigned char *input = pImage->pixels;
94  unsigned char *output = pCapturedImage->pixels;
95  const int nBytes = pImage->width * pImage->height * pImage->bytesPerPixel;
96 
97  if (pImage->type == CByteImage::eRGB24)
98  {
99  for (int i = 0; i < nBytes; i += 3)
100  {
101  output[i] = input[i + 2];
102  output[i + 1] = input[i + 1];
103  output[i + 2] = input[i];
104  }
105  }
106  else if (pImage->type == CByteImage::eGrayScale)
107  {
108  memcpy(output, input, nBytes);
109  }
110 
111  #ifdef WIN32
112  ImageProcessor::FlipY(pCapturedImage, pCapturedImage);
113  #endif
114 
115  cvcam_critical_section.UnLock();
116 
117  bFirstFrame = true;
118 
119  delete pImage;
120 }
121 
122 
123 // ****************************************************************************
124 // Constructor / Destructor
125 // ****************************************************************************
126 
128 {
129  m_bStarted = false;
130 
131  m_pImage = 0;
132  pCapturedImage = 0;
133 }
134 
136 {
137  CloseCamera();
138 
139  if (m_pImage)
140  {
141  delete m_pImage;
142  delete pCapturedImage;
143  }
144 }
145 
146 
147 // ****************************************************************************
148 // Methods
149 // ****************************************************************************
150 
152 {
153  if (m_bStarted)
154  return true;
155 
156  m_bStarted = false;
157 
158  if (cvcamGetCamerasCount() <= 0)
159  return false;
160 
161  // selects the 1st found camera
162  cvcamSetProperty(0, CVCAM_PROP_ENABLE, CVCAMTRUE);
163  cvcamSetProperty(0, CVCAM_PROP_RENDER, CVCAMTRUE);
164  // set up a callback to process the frames
165  cvcamSetProperty(0, CVCAM_PROP_CALLBACK, (void*) callback);
166  // start the camera
167  cvcamInit();
168  cvcamStart();
169 
170  m_bStarted = true;
171 
172  while (!bFirstFrame)
173  cvWaitKey(10);
174 
175  return true;
176 }
177 
179 {
180  cvcamStop();
181  cvcamExit();
182  m_bStarted = false;
183  bFirstFrame = false;
184 }
185 
187 {
188  if (!ppImages || !ppImages[0])
189  return false;
190 
191  CByteImage *pImage = ppImages[0];
192 
193  cvcam_critical_section.Lock();
194 
195  if (!pCapturedImage)
196  {
197  cvcam_critical_section.UnLock();
198  return false;
199  }
200 
201  // check if input image matches format
202  if (pCapturedImage->width != pImage->width || pCapturedImage->height != pImage->height || pCapturedImage->type != pImage->type)
203  {
204  cvcam_critical_section.UnLock();
205  return false;
206  }
207 
208  ImageProcessor::CopyImage(pCapturedImage, pImage);
209 
210  cvcam_critical_section.UnLock();
211 
212  return true;
213 }
214 
216 {
217  return pCapturedImage ? pCapturedImage->width : -1;
218 }
219 
221 {
222  return pCapturedImage ? pCapturedImage->height : -1;
223 }
224 
226 {
227  return pCapturedImage ? pCapturedImage->type : (CByteImage::ImageType) -1;
228 }
int width
The width of the image in pixels.
Definition: ByteImage.h:257
CByteImage::ImageType GetType()
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
Implementation of mutexes for synchronization.
Definition: Mutex.h:69
Threading::EMutexStatus UnLock()
Definition: Mutex.cpp:126
unsigned char * pixels
The pointer to the the pixels.
Definition: ByteImage.h:283
bool FlipY(const CByteImage *pInputImage, CByteImage *pOutputImage)
Flips the rows in a CByteImage vertically and writes the result to a CByteImage.
CByteImage * m_pImage
Definition: CVCamCapture.h:83
bool CopyImage(const CByteImage *pInputImage, CByteImage *pOutputImage, const MyRegion *pROI=0, bool bUseSameSize=false)
Copies one CByteImage to another.
Threading::EMutexStatus Lock()
Definition: Mutex.cpp:82
int height
The height of the image in pixels.
Definition: ByteImage.h:264
static CByteImage * pCapturedImage
int bytesPerPixel
The number of bytes used for encoding one pixel.
Definition: ByteImage.h:273
ImageType
Enum specifying the supported image types.
Definition: ByteImage.h:86
static bool bFirstFrame
GLenum GLenum GLenum input
Definition: glext.h:5307
ImageType type
The type of the image.
Definition: ByteImage.h:292
IplImage * Adapt(const CByteImage *pImage, bool bAllocateMemory=false)
Converts a CByteImage to an IplImage.
CMutex cvcam_critical_section
bool CaptureImage(CByteImage **ppImages)
static void callback(IplImage *pIplImage)


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:27