VolumeDataPreProcessor.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "VolumeDataPreProcessor.h"
  2. /// <summary>
  3. /// 构造函数
  4. /// </summary>
  5. VolumeDataPreProcessor::VolumeDataPreProcessor()
  6. {
  7. _queueSize = 1;
  8. _dataBuffer = nullptr;
  9. _origWidth = 0;
  10. _origHeight = 0;
  11. _origImgCount = 0;
  12. _dstWidth = 0;
  13. _dstHeight = 0;
  14. _depthFlag = CV_8UC1;
  15. _bytePerPixel = 1;
  16. _cropRect = cv::Rect(0,0,0,0);
  17. _origImgByteCount = 0;
  18. }
  19. /// <summary>
  20. /// 构造函数
  21. /// </summary>
  22. /// <param name="size"></param>
  23. VolumeDataPreProcessor::VolumeDataPreProcessor(int size, VolumeDataPreProcessorInfo dataInfo)
  24. {
  25. // 读出输入
  26. _queueSize = size;
  27. _dataBuffer = dataInfo.dataBuffer;
  28. _origWidth = dataInfo.origImgWidth;
  29. _origHeight = dataInfo.origImgHeight;
  30. _origImgCount = dataInfo.origImgCount;
  31. ColorType colorType = dataInfo.colorType;
  32. Rect cropRect = dataInfo.cropRect;
  33. _dstWidth = dataInfo.desiredImgWidth;
  34. _dstHeight = dataInfo.desiredImgHeight;
  35. // 求出所需的中间变量
  36. _depthFlag = ImageHelper::GetDepthFlag(colorType);
  37. _bytePerPixel = ImageHelper::GetBytesPerPixel(colorType);
  38. _cropRect = cv::Rect(cropRect.left, cropRect.top, cropRect.width, cropRect.height);
  39. // 计算图像尺寸和一幅图的指针偏移量
  40. int imgSize = _origWidth * _origHeight;
  41. int bytesPerPixel = ImageHelper::GetBytesPerPixel(colorType);
  42. _origImgByteCount = imgSize * bytesPerPixel;
  43. }
  44. /// <summary>
  45. /// 析构函数
  46. /// </summary>
  47. VolumeDataPreProcessor::~VolumeDataPreProcessor()
  48. {
  49. while(!_imageIndex.empty())
  50. {
  51. RemoveTopMostImage();
  52. }
  53. }
  54. /// <summary>
  55. /// 添加一幅图
  56. /// </summary>
  57. /// <param name="index"></param>
  58. /// <param name="image"></param>
  59. void VolumeDataPreProcessor::AddImage(int index, cv::Mat image)
  60. {
  61. auto found = _images.find(index);
  62. if(found == _images.end())
  63. {
  64. _imageIndex.push(index);
  65. _images.insert(std::map<int,cv::Mat>::value_type(index, image));
  66. if(_queueSize < _imageIndex.size())
  67. {
  68. RemoveTopMostImage();
  69. }
  70. }
  71. else
  72. {
  73. found->second = image;
  74. }
  75. }
  76. /// <summary>
  77. /// 容器内是否有指定index的图像
  78. /// </summary>
  79. /// <param name="index"></param>
  80. /// <returns></returns>
  81. bool VolumeDataPreProcessor::Contains(int index)
  82. {
  83. auto found = _images.find(index);
  84. return found != _images.end();
  85. }
  86. /// <summary>
  87. /// 取得一幅图
  88. /// </summary>
  89. /// <param name="index"></param>
  90. /// <returns></returns>
  91. cv::Mat VolumeDataPreProcessor::GetImage(int index)
  92. {
  93. if(index <0 || index>= _origImgCount)
  94. {
  95. char strMsgBuff[32];
  96. std::snprintf(strMsgBuff, 32, "unexpected image index ( %i ) for a volume data with ( %i ) images .", index, _origImgCount);
  97. throw std::invalid_argument(strMsgBuff);
  98. }
  99. if(!Contains(index))
  100. {
  101. // 取出感兴趣的帧
  102. void* pImgData = (void*)(_dataBuffer + _origImgByteCount * index);
  103. cv::Mat imgOrig = cv::Mat(cv::Size(_origWidth, _origHeight), _depthFlag, pImgData);
  104. imgOrig = cv::Mat(imgOrig, _cropRect);
  105. cv::Mat imgCrop = cv::Mat(cv::Size(_cropRect.width, _cropRect.height), _depthFlag);
  106. imgOrig.copyTo(imgCrop);
  107. imgOrig.release();
  108. cv::resize(imgCrop, imgCrop, cv::Size(_dstWidth, _dstHeight));
  109. AddImage(index, imgCrop);
  110. }
  111. return _images[index];
  112. }
  113. /// <summary>
  114. /// 移除最顶部的图像
  115. /// </summary>
  116. void VolumeDataPreProcessor::RemoveTopMostImage()
  117. {
  118. int index = _imageIndex.front();
  119. _imageIndex.pop();
  120. _images[index].release();
  121. _images.erase(index);
  122. }