#include "VolumeDataPreProcessor.h"
///
/// 构造函数
///
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;
}
///
/// 构造函数
///
///
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;
}
///
/// 析构函数
///
VolumeDataPreProcessor::~VolumeDataPreProcessor()
{
while(!_imageIndex.empty())
{
RemoveTopMostImage();
}
}
///
/// 添加一幅图
///
///
///
void VolumeDataPreProcessor::AddImage(int index, cv::Mat image)
{
auto found = _images.find(index);
if(found == _images.end())
{
_imageIndex.push(index);
_images.insert(std::map::value_type(index, image));
if(_queueSize < _imageIndex.size())
{
RemoveTopMostImage();
}
}
else
{
found->second = image;
}
}
///
/// 容器内是否有指定index的图像
///
///
///
bool VolumeDataPreProcessor::Contains(int index)
{
auto found = _images.find(index);
return found != _images.end();
}
///
/// 取得一幅图
///
///
///
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];
}
///
/// 移除最顶部的图像
///
void VolumeDataPreProcessor::RemoveTopMostImage()
{
int index = _imageIndex.front();
_imageIndex.pop();
_images[index].release();
_images.erase(index);
}