PlaqueHelper.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "PlaqueHelper.h"
  2. float CalcRatio(std::vector<cv::Point> artery, std::vector<cv::Point> plaque)
  3. {
  4. double areaArtery = fmax(cv::contourArea(artery), 0);
  5. double areaPlaque = fmax(cv::contourArea(plaque), 0);
  6. if (areaPlaque <= areaArtery)
  7. {
  8. return (areaPlaque / areaArtery) * 100;
  9. }
  10. else
  11. {
  12. return 100;
  13. }
  14. }
  15. bool comparePoints(const cv::Point& p1, const cv::Point& p2)
  16. {
  17. if (p1.x == p2.x)
  18. {
  19. return p1.y < p2.y;
  20. }
  21. return p1.x < p2.x;
  22. }
  23. extern "C" __declspec(dllexport) bool FitLine(const cv::Point * points, const int pointCount, float* line, cv::DistanceTypes type,
  24. double param, double reps, double aeps)
  25. {
  26. try
  27. {
  28. std::vector<cv::Point> pointsIn;
  29. for (int ni = 0; ni < pointCount; ni++)
  30. {
  31. pointsIn.push_back(points[ni]);
  32. }
  33. cv::Vec4f lineParam;
  34. cv::fitLine(pointsIn, lineParam, type, param, reps, aeps);
  35. for (int ni = 0; ni < 4; ni++)
  36. {
  37. line[ni] = lineParam[ni];
  38. }
  39. return true;
  40. }
  41. catch (const std::exception& ex)
  42. {
  43. return false;
  44. }
  45. }
  46. extern "C" __declspec(dllexport) bool PointInPolygon(const cv::Point * contours, const int pointCount, const cv::Point * point)
  47. {
  48. try
  49. {
  50. std::vector<cv::Point> pointsIn;
  51. for (int ni = 0; ni < pointCount; ni++)
  52. {
  53. pointsIn.push_back(contours[ni]);
  54. }
  55. if (pointPolygonTest(pointsIn, point[0], false) >0)
  56. {
  57. return true;
  58. }
  59. else
  60. {
  61. return false;
  62. }
  63. }
  64. catch (const std::exception& ex)
  65. {
  66. return false;
  67. }
  68. }
  69. extern "C" __declspec(dllexport) bool CalcMaxPlaque(const cv::Point * arteryContour, const cv::Point * plaqueContour,
  70. const int arteryCount, const int plaqueCount, const int left, const int top , float& ratio, RotatedRectangle& RealRect)
  71. {
  72. try
  73. {
  74. std::vector<cv::Point> arteryPoint;
  75. for (int ni = 0; ni < arteryCount; ni++)
  76. {
  77. arteryPoint.push_back(arteryContour[ni]);
  78. }
  79. std::vector<cv::Point> plaquePoint;
  80. for (int ni = 0; ni < plaqueCount; ni++)
  81. {
  82. plaquePoint.push_back(plaqueContour[ni]);
  83. }
  84. //计算狭窄率
  85. ratio = CalcRatio(arteryPoint, plaquePoint);
  86. //斑块外接矩阵
  87. cv::RotatedRect plaqueRect = cv::minAreaRect(plaquePoint);
  88. //斑块实际位置
  89. RealRect.Angle = plaqueRect.angle;
  90. RealRect.Cx = plaqueRect.center.x + left;
  91. RealRect.Cy = plaqueRect.center.y + top;
  92. RealRect.Height = plaqueRect.size.height;
  93. RealRect.Width = plaqueRect.size.width;
  94. return true;
  95. }
  96. catch (const std::exception& ex)
  97. {
  98. return false;
  99. }
  100. }
  101. extern "C" __declspec(dllexport) bool IsBrightPlaque(ImageInfo srcImgInfo, const cv::Point * contours, const int pointCount)
  102. {
  103. try
  104. {
  105. cv::Mat imgEncode = cv::Mat(cv::Size(srcImgInfo.width, srcImgInfo.height), CV_8UC1, (void*)srcImgInfo.dataBuffer);
  106. std::vector<cv::Point> pointsIn;
  107. for (int ni = 0; ni < pointCount; ni++)
  108. {
  109. pointsIn.push_back(contours[ni]);
  110. }
  111. if (pointCount < 5)
  112. {
  113. return false;
  114. }
  115. //std::sort(pointsIn.begin(), pointsIn.end(), comparePoints); //排序 优先x升序排列,x相同时,y升序排列
  116. int brightPointNum = 0;
  117. int brightPointThreshold = 200;
  118. for (int i = 0; i < pointCount; i++)
  119. {
  120. if (imgEncode.at<uchar>(pointsIn[i].x, pointsIn[i].y) > brightPointThreshold)
  121. {
  122. brightPointNum++;
  123. }
  124. }
  125. if (brightPointNum > 30)
  126. {
  127. return true;
  128. }
  129. else
  130. {
  131. return false;
  132. }
  133. }
  134. catch (const std::exception& ex)
  135. {
  136. return false;
  137. }
  138. }
  139. /// <summary>
  140. /// 获取用于内膜检测的切面图像
  141. /// </summary>
  142. /// <param name="srcImgInfo"></param>
  143. /// <param name="extension"></param>
  144. /// <param name="imwriteParams"></param>
  145. /// <param name="paramCount"></param>
  146. /// <param name="savePath"></param>
  147. /// <returns></returns>
  148. extern "C" __declspec(dllexport) bool Get2DMeasureImage(ImageInfo srcImgInfo, const int dataSize, ImageInfo & dstImgInfo)
  149. {
  150. try
  151. {
  152. auto depthFlag = ImageDepth::GetDepthFlag(srcImgInfo.colorType);
  153. cv::Mat imgEncode = cv::Mat(cv::Size(srcImgInfo.width, srcImgInfo.height), depthFlag, (void*)srcImgInfo.dataBuffer);
  154. //图像先水平翻转,再顺时针旋转90°
  155. cv::Mat imgDst;
  156. cv::flip(imgEncode, imgDst, 1); // 1表示水平翻转
  157. cv::rotate(imgDst, imgDst, cv::ROTATE_90_CLOCKWISE); //旋转后宽高互换
  158. //转为BGR
  159. //cvtColor(imgDst, imgDst, COLOR_GRAY2BGR);
  160. //cv::imwrite("D:\\三维重建模型\\22.jpg", imgDst);
  161. //图像数据copy至内存指针中
  162. uint8_t* imageData = new uint8_t[imgDst.total()];
  163. std::memcpy(imageData, imgDst.data, imgDst.total());
  164. dstImgInfo.dataBuffer = imageData;
  165. dstImgInfo.colorType = srcImgInfo.colorType;
  166. dstImgInfo.height = imgDst.rows;
  167. dstImgInfo.width = imgDst.cols;
  168. //delete[] imageData; //需等调用完以后才能释放
  169. imgEncode.release();
  170. imgDst.release();
  171. return true;
  172. }
  173. catch (const std::exception& ex)
  174. {
  175. ErrorMsg::SetErrorMsg({ ex.what() });
  176. return false;
  177. }
  178. }
  179. /// <summary>
  180. /// 存储每张切片图像至本地,用于测试
  181. /// </summary>
  182. /// <param name="srcImgInfo"></param>
  183. /// <param name="extension"></param>
  184. /// <param name="imwriteParams"></param>
  185. /// <param name="paramCount"></param>
  186. /// <param name="savePath"></param>
  187. /// <returns></returns>
  188. extern "C" __declspec(dllexport) bool SaveSliceImage(ImageInfo srcImgInfo, const int name)
  189. {
  190. try
  191. {
  192. auto depthFlag = ImageDepth::GetDepthFlag(srcImgInfo.colorType);
  193. cv::Mat imgEncode = cv::Mat(cv::Size(srcImgInfo.width, srcImgInfo.height), depthFlag, (void*)srcImgInfo.dataBuffer);
  194. string savepath = "E:\\workspace\\SVN\\Trunk\\SourceCode\\AIReconstruction\\Bin\\CarotidIntimaDetect\\" + to_string(name) + ".jpg";
  195. imwrite(savepath, imgEncode);
  196. return true;
  197. }
  198. catch (const std::exception& ex)
  199. {
  200. ErrorMsg::SetErrorMsg({ ex.what() });
  201. return false;
  202. }
  203. }