123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- #include "PlaqueHelper.h"
- float CalcRatio(std::vector<cv::Point> artery, std::vector<cv::Point> plaque)
- {
- double areaArtery = fmax(cv::contourArea(artery), 0);
- double areaPlaque = fmax(cv::contourArea(plaque), 0);
- if (areaPlaque <= areaArtery)
- {
- return (areaPlaque / areaArtery) * 100;
- }
- else
- {
- return 100;
- }
- }
- bool comparePoints(const cv::Point& p1, const cv::Point& p2)
- {
- if (p1.x == p2.x)
- {
- return p1.y < p2.y;
- }
- return p1.x < p2.x;
- }
- extern "C" __declspec(dllexport) bool FitLine(const cv::Point * points, const int pointCount, float* line, cv::DistanceTypes type,
- double param, double reps, double aeps)
- {
- try
- {
- std::vector<cv::Point> pointsIn;
- for (int ni = 0; ni < pointCount; ni++)
- {
- pointsIn.push_back(points[ni]);
- }
- cv::Vec4f lineParam;
- cv::fitLine(pointsIn, lineParam, type, param, reps, aeps);
- for (int ni = 0; ni < 4; ni++)
- {
- line[ni] = lineParam[ni];
- }
- return true;
- }
- catch (const std::exception& ex)
- {
- return false;
- }
- }
- extern "C" __declspec(dllexport) bool PointInPolygon(const cv::Point * contours, const int pointCount, const cv::Point * point)
- {
- try
- {
- std::vector<cv::Point> pointsIn;
- for (int ni = 0; ni < pointCount; ni++)
- {
- pointsIn.push_back(contours[ni]);
- }
- if (pointPolygonTest(pointsIn, point[0], false) >0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- catch (const std::exception& ex)
- {
- return false;
- }
- }
- extern "C" __declspec(dllexport) bool CalcMaxPlaque(const cv::Point * arteryContour, const cv::Point * plaqueContour,
- const int arteryCount, const int plaqueCount, const int left, const int top , float& ratio, RotatedRectangle& RealRect)
- {
- try
- {
- std::vector<cv::Point> arteryPoint;
- for (int ni = 0; ni < arteryCount; ni++)
- {
- arteryPoint.push_back(arteryContour[ni]);
- }
- std::vector<cv::Point> plaquePoint;
- for (int ni = 0; ni < plaqueCount; ni++)
- {
- plaquePoint.push_back(plaqueContour[ni]);
- }
- //计算狭窄率
- ratio = CalcRatio(arteryPoint, plaquePoint);
- //斑块外接矩阵
- cv::RotatedRect plaqueRect = cv::minAreaRect(plaquePoint);
- //斑块实际位置
- RealRect.Angle = plaqueRect.angle;
- RealRect.Cx = plaqueRect.center.x + left;
- RealRect.Cy = plaqueRect.center.y + top;
- RealRect.Height = plaqueRect.size.height;
- RealRect.Width = plaqueRect.size.width;
- return true;
- }
- catch (const std::exception& ex)
- {
- return false;
- }
-
- }
- extern "C" __declspec(dllexport) bool IsBrightPlaque(ImageInfo srcImgInfo, const cv::Point * contours, const int pointCount)
- {
- try
- {
- cv::Mat imgEncode = cv::Mat(cv::Size(srcImgInfo.width, srcImgInfo.height), CV_8UC1, (void*)srcImgInfo.dataBuffer);
- std::vector<cv::Point> pointsIn;
- for (int ni = 0; ni < pointCount; ni++)
- {
- pointsIn.push_back(contours[ni]);
- }
- if (pointCount < 5)
- {
- return false;
- }
- //std::sort(pointsIn.begin(), pointsIn.end(), comparePoints); //排序 优先x升序排列,x相同时,y升序排列
- int brightPointNum = 0;
- int brightPointThreshold = 200;
- for (int i = 0; i < pointCount; i++)
- {
- if (imgEncode.at<uchar>(pointsIn[i].x, pointsIn[i].y) > brightPointThreshold)
- {
- brightPointNum++;
- }
- }
-
- if (brightPointNum > 30)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- catch (const std::exception& ex)
- {
- return false;
- }
- }
- /// <summary>
- /// 获取用于内膜检测的切面图像
- /// </summary>
- /// <param name="srcImgInfo"></param>
- /// <param name="extension"></param>
- /// <param name="imwriteParams"></param>
- /// <param name="paramCount"></param>
- /// <param name="savePath"></param>
- /// <returns></returns>
- extern "C" __declspec(dllexport) bool Get2DMeasureImage(ImageInfo srcImgInfo, const int dataSize, ImageInfo & dstImgInfo)
- {
- try
- {
- auto depthFlag = ImageDepth::GetDepthFlag(srcImgInfo.colorType);
- cv::Mat imgEncode = cv::Mat(cv::Size(srcImgInfo.width, srcImgInfo.height), depthFlag, (void*)srcImgInfo.dataBuffer);
- //图像先水平翻转,再顺时针旋转90°
- cv::Mat imgDst;
- cv::flip(imgEncode, imgDst, 1); // 1表示水平翻转
- cv::rotate(imgDst, imgDst, cv::ROTATE_90_CLOCKWISE); //旋转后宽高互换
- //转为BGR
- //cvtColor(imgDst, imgDst, COLOR_GRAY2BGR);
- //cv::imwrite("D:\\三维重建模型\\22.jpg", imgDst);
- //图像数据copy至内存指针中
- uint8_t* imageData = new uint8_t[imgDst.total()];
- std::memcpy(imageData, imgDst.data, imgDst.total());
- dstImgInfo.dataBuffer = imageData;
- dstImgInfo.colorType = srcImgInfo.colorType;
- dstImgInfo.height = imgDst.rows;
- dstImgInfo.width = imgDst.cols;
- //delete[] imageData; //需等调用完以后才能释放
- imgEncode.release();
- imgDst.release();
- return true;
- }
- catch (const std::exception& ex)
- {
- ErrorMsg::SetErrorMsg({ ex.what() });
- return false;
- }
- }
- /// <summary>
- /// 存储每张切片图像至本地,用于测试
- /// </summary>
- /// <param name="srcImgInfo"></param>
- /// <param name="extension"></param>
- /// <param name="imwriteParams"></param>
- /// <param name="paramCount"></param>
- /// <param name="savePath"></param>
- /// <returns></returns>
- extern "C" __declspec(dllexport) bool SaveSliceImage(ImageInfo srcImgInfo, const int name)
- {
- try
- {
- auto depthFlag = ImageDepth::GetDepthFlag(srcImgInfo.colorType);
- cv::Mat imgEncode = cv::Mat(cv::Size(srcImgInfo.width, srcImgInfo.height), depthFlag, (void*)srcImgInfo.dataBuffer);
- string savepath = "E:\\workspace\\SVN\\Trunk\\SourceCode\\AIReconstruction\\Bin\\CarotidIntimaDetect\\" + to_string(name) + ".jpg";
- imwrite(savepath, imgEncode);
- return true;
- }
- catch (const std::exception& ex)
- {
- ErrorMsg::SetErrorMsg({ ex.what() });
- return false;
- }
- }
|