OpenCVCapture.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 
37 
38 // ****************************************************************************
39 // Includes
40 // ****************************************************************************
41 
42 #include "OpenCVCapture.h"
43 #include "Image/ImageProcessor.h"
44 #include "Image/ByteImage.h"
45 
46 
47 
48 // ****************************************************************************
49 // Constructor / Destructor
50 // ****************************************************************************
51 
52 COpenCVCapture::COpenCVCapture(int nIndex, const char *pFilename) : m_nIndex(nIndex)
53 {
54  m_pCapture = 0;
55  m_pIplImage = 0;
56 
57  m_sFilename = "";
58 
59  if (pFilename)
60  m_sFilename += pFilename;
61 }
62 
64 {
65  CloseCamera();
66 }
67 
68 
69 // ****************************************************************************
70 // Methods
71 // ****************************************************************************
72 
74 {
75  CloseCamera();
76 
77  if (m_sFilename.length() > 0)
78  m_pCapture = cvCaptureFromFile(m_sFilename.c_str());
79  else
80  m_pCapture = cvCaptureFromCAM(m_nIndex);
81  if (!m_pCapture)
82  return false;
83 
84  cvSetCaptureProperty(m_pCapture, CV_CAP_PROP_FPS, 30);
85  cvSetCaptureProperty(m_pCapture, CV_CAP_PROP_FRAME_WIDTH, 640);
86  cvSetCaptureProperty(m_pCapture, CV_CAP_PROP_FRAME_HEIGHT, 480);
87 
88  m_pIplImage = cvQueryFrame(m_pCapture);
89 
90  return true;
91 }
92 
94 {
95  cvReleaseCapture(&m_pCapture);
96  m_pIplImage = 0;
97 }
98 
100 {
101  m_pIplImage = cvQueryFrame(m_pCapture);
102  if (!m_pIplImage)
103  return false;
104 
105  CByteImage *pImage = ppImages[0];
106  if (!pImage || pImage->width != m_pIplImage->width || pImage->height != m_pIplImage->height)
107  return false;
108 
109  if (m_pIplImage->depth != IPL_DEPTH_8U)
110  return false;
111  else if (m_pIplImage->nChannels != 1 && m_pIplImage->nChannels != 3)
112  return false;
113  else if (m_pIplImage->nChannels == 1 && pImage->type != CByteImage::eGrayScale)
114  return false;
115  else if (m_pIplImage->nChannels == 3 && pImage->type != CByteImage::eRGB24)
116  return false;
117 
118  const int nBytes = pImage->width * pImage->height * pImage->bytesPerPixel;
119  unsigned char *input = (unsigned char *) m_pIplImage->imageData;
120  unsigned char *output = pImage->pixels;
121 
122  if (pImage->type == CByteImage::eGrayScale)
123  {
124  memcpy(output, input, nBytes);
125  }
126  else if (pImage->type == CByteImage::eRGB24)
127  {
128  for (int i = 0; i < nBytes; i += 3)
129  {
130  output[i] = input[i + 2];
131  output[i + 1] = input[i + 1];
132  output[i + 2] = input[i];
133  }
134  }
135 
136  #ifdef WIN32
137  ImageProcessor::FlipY(pImage, pImage);
138  #endif
139 
140  return true;
141 }
142 
143 
145 {
146  return m_pIplImage ? m_pIplImage->width : -1;
147 }
148 
150 {
151  return m_pIplImage ? m_pIplImage->height : -1;
152 }
153 
155 {
157 }
const int m_nIndex
Definition: OpenCVCapture.h:89
COpenCVCapture(int nIndex, const char *pFilename=0)
_IplImage * m_pIplImage
Definition: OpenCVCapture.h:88
int width
The width of the image in pixels.
Definition: ByteImage.h:257
std::string m_sFilename
Definition: OpenCVCapture.h:91
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
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.
bool CaptureImage(CByteImage **ppImages)
CvCapture * m_pCapture
Definition: OpenCVCapture.h:87
int height
The height of the image in pixels.
Definition: ByteImage.h:264
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
GLenum GLenum GLenum input
Definition: glext.h:5307
ImageType type
The type of the image.
Definition: ByteImage.h:292
CByteImage::ImageType GetType()


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:28