OpenCVCapture.cpp

Go to the documentation of this file.
00001 // ****************************************************************************
00002 // This file is part of the Integrating Vision Toolkit (IVT).
00003 //
00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
00006 //
00007 // Copyright (C) 2010 Karlsruhe Institute of Technology (KIT).
00008 // All rights reserved.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are met:
00012 //
00013 // 1. Redistributions of source code must retain the above copyright
00014 //    notice, this list of conditions and the following disclaimer.
00015 //
00016 // 2. Redistributions in binary form must reproduce the above copyright
00017 //    notice, this list of conditions and the following disclaimer in the
00018 //    documentation and/or other materials provided with the distribution.
00019 //
00020 // 3. Neither the name of the KIT nor the names of its contributors may be
00021 //    used to endorse or promote products derived from this software
00022 //    without specific prior written permission.
00023 //
00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 // ****************************************************************************
00035 // ****************************************************************************
00036 // Filename:  OpenCVCapture.cpp
00037 // Author:    Pedram Azad
00038 // Date:      2004
00039 // ****************************************************************************
00040 
00041 
00042 // ****************************************************************************
00043 // Includes
00044 // ****************************************************************************
00045 
00046 #include "OpenCVCapture.h"
00047 #include "Image/ImageProcessor.h"
00048 #include "Image/ByteImage.h"
00049 
00050 #include <highgui.h>
00051 
00052 
00053 
00054 // ****************************************************************************
00055 // Constructor / Destructor
00056 // ****************************************************************************
00057 
00058 COpenCVCapture::COpenCVCapture(int nIndex, const char *pFilename) : m_nIndex(nIndex)
00059 {
00060         m_pCapture = 0;
00061         m_pIplImage = 0;
00062 
00063         m_sFilename = "";
00064 
00065         if (pFilename)
00066                 m_sFilename += pFilename;
00067 }
00068 
00069 COpenCVCapture::~COpenCVCapture()
00070 {
00071         CloseCamera();
00072 }
00073 
00074 
00075 // ****************************************************************************
00076 // Methods
00077 // ****************************************************************************
00078 
00079 bool COpenCVCapture::OpenCamera()
00080 {
00081         CloseCamera();
00082 
00083         if (m_sFilename.length() > 0)
00084                 m_pCapture = cvCaptureFromFile(m_sFilename.c_str());
00085         else
00086                 m_pCapture = cvCaptureFromCAM(m_nIndex);
00087         if (!m_pCapture)
00088                 return false;
00089 
00090         cvSetCaptureProperty(m_pCapture, CV_CAP_PROP_FPS, 30);
00091         cvSetCaptureProperty(m_pCapture, CV_CAP_PROP_FRAME_WIDTH, 640);
00092         cvSetCaptureProperty(m_pCapture, CV_CAP_PROP_FRAME_HEIGHT, 480);
00093 
00094         m_pIplImage = cvQueryFrame(m_pCapture);
00095         
00096         return true;
00097 }
00098 
00099 void COpenCVCapture::CloseCamera()
00100 {
00101         cvReleaseCapture(&m_pCapture);
00102         m_pIplImage = 0;
00103 }
00104 
00105 bool COpenCVCapture::CaptureImage(CByteImage **ppImages)
00106 {
00107         m_pIplImage = cvQueryFrame(m_pCapture);
00108         if (!m_pIplImage)
00109                 return false;
00110 
00111         CByteImage *pImage = ppImages[0];
00112         if (!pImage || pImage->width != m_pIplImage->width || pImage->height != m_pIplImage->height)
00113                 return false;
00114 
00115         if (m_pIplImage->depth != IPL_DEPTH_8U)
00116                 return false;
00117         else if (m_pIplImage->nChannels != 1 && m_pIplImage->nChannels != 3)
00118                 return false;
00119         else if (m_pIplImage->nChannels == 1 && pImage->type != CByteImage::eGrayScale)
00120                 return false;
00121         else if (m_pIplImage->nChannels == 3 && pImage->type != CByteImage::eRGB24)
00122                 return false;
00123         
00124         const int nBytes = pImage->width * pImage->height * pImage->bytesPerPixel;
00125         unsigned char *input = (unsigned char *) m_pIplImage->imageData;
00126         unsigned char *output = pImage->pixels;
00127 
00128         if (pImage->type == CByteImage::eGrayScale)
00129         {
00130                 memcpy(output, input, nBytes);
00131         }
00132         else if (pImage->type == CByteImage::eRGB24)
00133         {
00134                 for (int i = 0; i < nBytes; i += 3)
00135                 {
00136                         output[i] = input[i + 2];
00137                         output[i + 1] = input[i + 1];
00138                         output[i + 2] = input[i];
00139                 }
00140         }
00141 
00142         #ifdef WIN32
00143         ImageProcessor::FlipY(pImage, pImage);
00144         #endif
00145         
00146         return true;
00147 }
00148 
00149 
00150 int COpenCVCapture::GetWidth()
00151 {
00152         return m_pIplImage ? m_pIplImage->width : -1;
00153 }
00154 
00155 int COpenCVCapture::GetHeight()
00156 {
00157         return m_pIplImage ? m_pIplImage->height : -1;
00158 }
00159 
00160 CByteImage::ImageType COpenCVCapture::GetType()
00161 {
00162         return m_pIplImage ? (m_pIplImage->nChannels == 3 ? CByteImage::eRGB24 : CByteImage::eGrayScale) : (CByteImage::ImageType) -1;
00163 }

Generated on Tue Apr 5 10:53:28 2011 for IVT by  doxygen 1.5.8