#include "PlaqueHelper.h" float CalcRatio(std::vector artery, std::vector 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 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 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 arteryPoint; for (int ni = 0; ni < arteryCount; ni++) { arteryPoint.push_back(arteryContour[ni]); } std::vector 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 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(pointsIn[i].x, pointsIn[i].y) > brightPointThreshold) { brightPointNum++; } } if (brightPointNum > 30) { return true; } else { return false; } } catch (const std::exception& ex) { return false; } } /// /// 获取用于内膜检测的切面图像 /// /// /// /// /// /// /// 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; } } /// /// 存储每张切片图像至本地,用于测试 /// /// /// /// /// /// /// 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; } }