12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #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<char> CGrayImageSource::getRow(int y, ArrayRef<char> 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<char> CGrayImageSource::getMatrix() const
- {
- int width = getWidth();
- int height = getHeight();
- ArrayRef<char> 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<char> fff((char*)matrix,getWidth()*getHeight());
- return fff;
- }
|