OpenCVCapture.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
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
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
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 }