#include "SliceHelper.h" /// /// 构造函数 /// SliceHelper::SliceHelper() { _dataLoaded = false; _volumeData = nullptr; _x = 0; _y = 0; _z = 0; _spacing = 0; _colorType = Gray8; _dataByteCounts = 0; _bytesPerPixel = 1; } /// /// 析构函数 /// SliceHelper::~SliceHelper() { if(_dataLoaded && _volumeData) { delete[] _volumeData; _volumeData = nullptr; } } /// /// 加载一个体数据 /// /// 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; } /// /// 体数据是否已加载 /// /// bool SliceHelper::VolumeDataLoaded() { return _dataLoaded; } /// /// 获取切面最大可能的图像尺寸 /// /// int SliceHelper::GetMaximumImgByteCounts() { return static_cast(std::pow(_x, 2)) + std::pow(_y, 2) + std::pow(_z, 2); } /// /// 获取任意切面的图片 /// /// /// /// void SliceHelper::GetSlicePlaneImage(Plane plane, ImageInfo& imageInfo) { } /// /// 获取垂直于某个轴的切面图片 /// /// /// /// 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 /// 获取用于内膜检测的2D图像 /// /// /// /// 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 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(); }