GrayImageSource.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "StdAfx.h"
  2. #include "GrayImageSource.h"
  3. CGrayImageSource::CGrayImageSource(IplImage* img) :LuminanceSource(img == NULL ? 0 : cvGetImageROI(img).width, img == NULL ? 0 : cvGetImageROI(img).height)
  4. {
  5. if (img != NULL){
  6. if (img->nChannels == 3){
  7. CvRect rect = cvGetImageROI(img);
  8. m_pImg = cvCreateImage(cvSize(rect.width, rect.height), IPL_DEPTH_8U, 1);
  9. cvCvtColor(img, m_pImg, CV_BGR2GRAY);
  10. }
  11. else if (img->nChannels == 1){
  12. CvRect rect = cvGetImageROI(img);
  13. m_pImg = cvCreateImage(cvSize(rect.width, rect.height), IPL_DEPTH_8U, 1);
  14. cvCopy(img, m_pImg);
  15. }
  16. }
  17. }
  18. CGrayImageSource::~CGrayImageSource(void)
  19. {
  20. if (m_pImg)cvReleaseImage(&m_pImg);
  21. }
  22. int CGrayImageSource::getWidth() const
  23. {
  24. return LuminanceSource::getWidth();
  25. }
  26. int CGrayImageSource::getHeight() const
  27. {
  28. return LuminanceSource::getHeight();
  29. }
  30. unsigned char* CGrayImageSource::getRow(int y, unsigned char* row)
  31. {
  32. if (row == NULL){
  33. row = new unsigned char[m_pImg->widthStep];
  34. }
  35. memcpy(row, m_pImg->imageData + y*m_pImg->widthStep, m_pImg->widthStep);
  36. return row;
  37. }
  38. ArrayRef<char> CGrayImageSource::getRow(int y, ArrayRef<char> row) const
  39. {
  40. char * rowdata = m_pImg->imageData + y*m_pImg->widthStep;
  41. for (int i = 0; i < getWidth(); i++)
  42. {
  43. row[i] = rowdata[i];
  44. }
  45. return row;
  46. }
  47. unsigned char* CGrayImageSource::getMatrix()
  48. {
  49. int width = getWidth();
  50. int height = getHeight();
  51. unsigned char* matrix = new unsigned char[width*height];
  52. unsigned char* m = matrix;
  53. for (int y = 0; y < height; y++) {
  54. memcpy(matrix + y*width, m_pImg->imageData + y*m_pImg->widthStep, width);
  55. }
  56. return matrix;
  57. }
  58. ArrayRef<char> CGrayImageSource::getMatrix() const
  59. {
  60. int width = getWidth();
  61. int height = getHeight();
  62. ArrayRef<char> fff(getWidth()*getHeight());
  63. char * matrix = &(*fff)[0];
  64. //unsigned char* matrix = new unsigned char[width*height];
  65. for (int y = 0; y < height; y++) {
  66. memcpy(matrix + y*width, m_pImg->imageData + y*m_pImg->widthStep, width);
  67. }
  68. //ArrayRef<char> fff((char*)matrix,getWidth()*getHeight());
  69. return fff;
  70. }