123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- #include "VolumeDataPreProcessor.h"
- /// <summary>
- /// 构造函数
- /// </summary>
- VolumeDataPreProcessor::VolumeDataPreProcessor()
- {
- _queueSize = 1;
- _dataBuffer = nullptr;
- _origWidth = 0;
- _origHeight = 0;
- _origImgCount = 0;
- _dstWidth = 0;
- _dstHeight = 0;
- _depthFlag = CV_8UC1;
- _bytePerPixel = 1;
- _cropRect = cv::Rect(0,0,0,0);
- _origImgByteCount = 0;
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="size"></param>
- VolumeDataPreProcessor::VolumeDataPreProcessor(int size, VolumeDataPreProcessorInfo dataInfo)
- {
- // 读出输入
- _queueSize = size;
- _dataBuffer = dataInfo.dataBuffer;
- _origWidth = dataInfo.origImgWidth;
- _origHeight = dataInfo.origImgHeight;
- _origImgCount = dataInfo.origImgCount;
- ColorType colorType = dataInfo.colorType;
- Rect cropRect = dataInfo.cropRect;
- _dstWidth = dataInfo.desiredImgWidth;
- _dstHeight = dataInfo.desiredImgHeight;
- // 求出所需的中间变量
- _depthFlag = ImageHelper::GetDepthFlag(colorType);
- _bytePerPixel = ImageHelper::GetBytesPerPixel(colorType);
- _cropRect = cv::Rect(cropRect.left, cropRect.top, cropRect.width, cropRect.height);
- // 计算图像尺寸和一幅图的指针偏移量
- int imgSize = _origWidth * _origHeight;
- int bytesPerPixel = ImageHelper::GetBytesPerPixel(colorType);
- _origImgByteCount = imgSize * bytesPerPixel;
- }
- /// <summary>
- /// 析构函数
- /// </summary>
- VolumeDataPreProcessor::~VolumeDataPreProcessor()
- {
- while(!_imageIndex.empty())
- {
- RemoveTopMostImage();
- }
- }
- /// <summary>
- /// 添加一幅图
- /// </summary>
- /// <param name="index"></param>
- /// <param name="image"></param>
- void VolumeDataPreProcessor::AddImage(int index, cv::Mat image)
- {
- auto found = _images.find(index);
- if(found == _images.end())
- {
- _imageIndex.push(index);
- _images.insert(std::map<int,cv::Mat>::value_type(index, image));
- if(_queueSize < _imageIndex.size())
- {
- RemoveTopMostImage();
- }
- }
- else
- {
- found->second = image;
- }
- }
- /// <summary>
- /// 容器内是否有指定index的图像
- /// </summary>
- /// <param name="index"></param>
- /// <returns></returns>
- bool VolumeDataPreProcessor::Contains(int index)
- {
- auto found = _images.find(index);
- return found != _images.end();
- }
- /// <summary>
- /// 取得一幅图
- /// </summary>
- /// <param name="index"></param>
- /// <returns></returns>
- cv::Mat VolumeDataPreProcessor::GetImage(int index)
- {
- if(index <0 || index>= _origImgCount)
- {
- char strMsgBuff[32];
- std::snprintf(strMsgBuff, 32, "unexpected image index ( %i ) for a volume data with ( %i ) images .", index, _origImgCount);
- throw std::invalid_argument(strMsgBuff);
- }
- if(!Contains(index))
- {
- // 取出感兴趣的帧
- void* pImgData = (void*)(_dataBuffer + _origImgByteCount * index);
- cv::Mat imgOrig = cv::Mat(cv::Size(_origWidth, _origHeight), _depthFlag, pImgData);
- imgOrig = cv::Mat(imgOrig, _cropRect);
- cv::Mat imgCrop = cv::Mat(cv::Size(_cropRect.width, _cropRect.height), _depthFlag);
- imgOrig.copyTo(imgCrop);
- imgOrig.release();
- cv::resize(imgCrop, imgCrop, cv::Size(_dstWidth, _dstHeight));
- AddImage(index, imgCrop);
- }
- return _images[index];
- }
- /// <summary>
- /// 移除最顶部的图像
- /// </summary>
- void VolumeDataPreProcessor::RemoveTopMostImage()
- {
- int index = _imageIndex.front();
- _imageIndex.pop();
- _images[index].release();
- _images.erase(index);
- }
|