123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- #include "SliceHelper.h"
- /// <summary>
- /// 构造函数
- /// </summary>
- SliceHelper::SliceHelper()
- {
- _dataLoaded = false;
- _volumeData = nullptr;
- _x = 0;
- _y = 0;
- _z = 0;
- _spacing = 0;
- _colorType = Gray8;
- _dataByteCounts = 0;
- _bytesPerPixel = 1;
- }
- /// <summary>
- /// 析构函数
- /// </summary>
- SliceHelper::~SliceHelper()
- {
- if(_dataLoaded && _volumeData)
- {
- delete[] _volumeData;
- _volumeData = nullptr;
- }
- }
- /// <summary>
- /// 加载一个体数据
- /// </summary>
- /// <param name="volumeDataInfo"></param>
- void SliceHelper::LoadVolumeData(UniformVolumeDataInfo volumeDataInfo)
- {
- uint8_t* dataBuffer = volumeDataInfo.dataBuffer;
- ColorType colorType = volumeDataInfo.colorType;
- int x = volumeDataInfo.x;
- int y = volumeDataInfo.y;
- int z = volumeDataInfo.z;
- float spacing = volumeDataInfo.spacing;
- int bytesPerPixel = ImageHelper::GetBytesPerPixel(colorType);
- int dataByteCounts = x * y * z * bytesPerPixel;
- if(_dataLoaded)
- {
- // 只有当空间不足的时候才重新分配
- if(_dataByteCounts < dataByteCounts)
- {
- delete[] _volumeData;
- _volumeData = new uint8_t[dataByteCounts];
- }
- }
- else
- {
- _volumeData = new uint8_t[dataByteCounts];
- }
- std::memcpy(_volumeData, dataBuffer, dataByteCounts);
- _x = x;
- _y = y;
- _z = z;
- _spacing = spacing;
- _colorType = colorType;
- _dataByteCounts = dataByteCounts;
- _bytesPerPixel = bytesPerPixel;
- _dataLoaded = true;
- }
- /// <summary>
- /// 体数据是否已加载
- /// </summary>
- /// <returns></returns>
- bool SliceHelper::VolumeDataLoaded()
- {
- return _dataLoaded;
- }
- /// <summary>
- /// 获取切面最大可能的图像尺寸
- /// </summary>
- /// <returns></returns>
- int SliceHelper::GetMaximumImgByteCounts()
- {
- return static_cast<int>(std::pow(_x, 2)) + std::pow(_y, 2) + std::pow(_z, 2);
- }
- /// <summary>
- /// 获取任意切面的图片
- /// </summary>
- /// <param name="plane"></param>
- /// <param name="imageInfo"></param>
- /// <returns></returns>
- void SliceHelper::GetSlicePlaneImage(Plane plane, ImageInfo& imageInfo)
- {
- }
- /// <summary>
- /// 获取垂直于某个轴的切面图片
- /// </summary>
- /// <param name="axisName"></param>
- /// <param name="intersectionVal"></param>
- /// <param name="imageInfo"></param>
- void SliceHelper::GetVerticalToAxisSlicePlaneImage(AxisName axisName, int intersectionVal, ImageInfo& imageInfo)
- {
- int width = 0;
- int height = 0;
- int pixelOffset;
- int volumeOffset;
- int xi = 0;
- int yi = 0;
- int zi = 0;
- uint8_t* dataBuffer = imageInfo.dataBuffer;
- // 根据垂直于哪个轴,得到切面的宽度和高度,再逐个像素为切面赋值
- // 注意:为了减少循环里的语句数量(为了提高执行的速度),把switch case 放在了循环外(代价是,看起来每个case下的代码很相似)
- switch(axisName)
- {
- case X:
- width = _z;
- height = _y;
- xi = intersectionVal;
- for (int nh = 0; nh < height; nh++)
- {
- yi = nh;
- for (int nw = 0; nw < width; nw++)
- {
- zi = nw;
- pixelOffset = (nh * width + nw) * _bytesPerPixel;
- volumeOffset = (zi * _y * _x + yi * _x + xi) * _bytesPerPixel;
- for(int nc =0; nc<_bytesPerPixel; nc++)
- {
- dataBuffer[pixelOffset + nc] = _volumeData[volumeOffset +nc];
- }
- }
- }
- break;
- case Y:
- width = _x;
- height = _z;
- yi = intersectionVal;
- for(int nh=0; nh<height; nh++)
- {
- zi = nh;
- for (int nw = 0; nw < width; nw++)
- {
- xi = nw;
- pixelOffset = (nh * width + nw) * _bytesPerPixel;
- volumeOffset = (zi * _y * _x + yi * _x + xi) * _bytesPerPixel;
- for (int nc = 0; nc < _bytesPerPixel; nc++)
- {
- dataBuffer[pixelOffset + nc] = _volumeData[volumeOffset + nc];
- }
- }
- }
- break;
- case Z:
- width = _x;
- height = _y;
- zi = intersectionVal;
- for (int nh = 0; nh < height; nh++)
- {
- yi = nh;
- for (int nw = 0; nw < width; nw++)
- {
- xi = nw;
- pixelOffset = (nh * width + nw) * _bytesPerPixel;
- volumeOffset = (zi * _y * _x + yi * _x + xi) * _bytesPerPixel;
- for (int nc = 0; nc < _bytesPerPixel; nc++)
- {
- dataBuffer[pixelOffset + nc] = _volumeData[volumeOffset + nc];
- }
- }
- }
- break;
- }
- // 给ImageInfo赋值
- imageInfo.width = width;
- imageInfo.height = height;
- imageInfo.colorType = _colorType;
- }
- /// <summary>
- /// 获取用于内膜检测的2D图像
- /// </summary>
- /// <param name="axisName"></param>
- /// <param name="intersectionVal"></param>
- /// <param name="imageInfo"></param>
- void SliceHelper::ImgFlipAndRotate(ImageInfo srcImgInfo, const int srcDataSize, cv::Mat& imgDst)
- {
- auto depthFlag = ImageHelper::GetDepthFlag(srcImgInfo.colorType);
- cv::Mat imgEncode = cv::Mat(cv::Size(srcImgInfo.width, srcImgInfo.height), depthFlag, (void*)srcImgInfo.dataBuffer);
- std::vector<uchar> dataDecode;
- dataDecode.resize(srcDataSize);
- for (int ni = 0; ni < srcDataSize; ni++)
- {
- //dataDecode[ni] = imgEncode[ni];
- }
- cv::Mat imgDecode = cv::imdecode(dataDecode, cv::IMREAD_UNCHANGED);
-
- //图像先水平翻转,再顺时针旋转90°
- cv::flip(imgDecode, imgDst, 1); // 1表示水平翻转
- cv::rotate(imgDst, imgDst, cv::ROTATE_90_CLOCKWISE); //旋转后宽高互换
- imgDecode.release();
- }
|