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