#include "StdAfx.h" #include "GrayImageSource.h" CGrayImageSource::CGrayImageSource(IplImage* img) :LuminanceSource(img == NULL ? 0 : cvGetImageROI(img).width, img == NULL ? 0 : cvGetImageROI(img).height) { if (img != NULL){ if (img->nChannels == 3){ CvRect rect = cvGetImageROI(img); m_pImg = cvCreateImage(cvSize(rect.width, rect.height), IPL_DEPTH_8U, 1); cvCvtColor(img, m_pImg, CV_BGR2GRAY); } else if (img->nChannels == 1){ CvRect rect = cvGetImageROI(img); m_pImg = cvCreateImage(cvSize(rect.width, rect.height), IPL_DEPTH_8U, 1); cvCopy(img, m_pImg); } } } CGrayImageSource::~CGrayImageSource(void) { if (m_pImg)cvReleaseImage(&m_pImg); } int CGrayImageSource::getWidth() const { return LuminanceSource::getWidth(); } int CGrayImageSource::getHeight() const { return LuminanceSource::getHeight(); } unsigned char* CGrayImageSource::getRow(int y, unsigned char* row) { if (row == NULL){ row = new unsigned char[m_pImg->widthStep]; } memcpy(row, m_pImg->imageData + y*m_pImg->widthStep, m_pImg->widthStep); return row; } ArrayRef CGrayImageSource::getRow(int y, ArrayRef row) const { char * rowdata = m_pImg->imageData + y*m_pImg->widthStep; for (int i = 0; i < getWidth(); i++) { row[i] = rowdata[i]; } return row; } unsigned char* CGrayImageSource::getMatrix() { int width = getWidth(); int height = getHeight(); unsigned char* matrix = new unsigned char[width*height]; unsigned char* m = matrix; for (int y = 0; y < height; y++) { memcpy(matrix + y*width, m_pImg->imageData + y*m_pImg->widthStep, width); } return matrix; } ArrayRef CGrayImageSource::getMatrix() const { int width = getWidth(); int height = getHeight(); ArrayRef fff(getWidth()*getHeight()); char * matrix = &(*fff)[0]; //unsigned char* matrix = new unsigned char[width*height]; for (int y = 0; y < height; y++) { memcpy(matrix + y*width, m_pImg->imageData + y*m_pImg->widthStep, width); } //ArrayRef fff((char*)matrix,getWidth()*getHeight()); return fff; }