CircularArresterCurrentALGO.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "CircularArresterCurrentALGO.h"
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <math.h>
  7. #include <cmath>
  8. #include <opencv2/imgproc.hpp>
  9. #include <opencv2/highgui.hpp>
  10. #include <opencv2/highgui/highgui.hpp>
  11. #include "opencv2/imgproc/types_c.h"
  12. #include <onnxruntime_cxx_api.h>
  13. #include <vector>
  14. #define SEG_IMAGE_SIZE 512
  15. #define LINE_HEIGH 120
  16. #define LINE_WIDTH 1600
  17. #define CIRCLE_RADIUS 250
  18. #define METER_RANGE 3.0
  19. using namespace cv;
  20. using namespace std;
  21. using namespace Ort;
  22. void CircularArresterCurrentALGO::Init()
  23. {
  24. string model_path = "models/ArresterMonitor.onnx";
  25. std::wstring widestr = std::wstring(model_path.begin(), model_path.end());
  26. sessionOptions.SetGraphOptimizationLevel(ORT_ENABLE_BASIC);
  27. ort_session = new Session(env, widestr.c_str(), sessionOptions);
  28. size_t numInputNodes = ort_session->GetInputCount();
  29. size_t numOutputNodes = ort_session->GetOutputCount();
  30. AllocatorWithDefaultOptions allocator;
  31. for (int i = 0; i < numInputNodes; i++)
  32. {
  33. input_names.push_back(ort_session->GetInputName(i, allocator));
  34. Ort::TypeInfo input_type_info = ort_session->GetInputTypeInfo(i);
  35. auto input_tensor_info = input_type_info.GetTensorTypeAndShapeInfo();
  36. auto input_dims = input_tensor_info.GetShape();
  37. input_node_dims.push_back(input_dims);
  38. }
  39. for (int i = 0; i < numOutputNodes; i++)
  40. {
  41. output_names.push_back(ort_session->GetOutputName(i, allocator));
  42. Ort::TypeInfo output_type_info = ort_session->GetOutputTypeInfo(i);
  43. auto output_tensor_info = output_type_info.GetTensorTypeAndShapeInfo();
  44. auto output_dims = output_tensor_info.GetShape();
  45. output_node_dims.push_back(output_dims);
  46. }
  47. this->inpHeight = input_node_dims[0][2];
  48. this->inpWidth = input_node_dims[0][3];
  49. this->outHeight = output_node_dims[0][2];
  50. this->outWidth = output_node_dims[0][3];
  51. }
  52. float CircularArresterCurrentALGO::detect(Mat& srcimg)
  53. {
  54. vector<float> input_image_ = { 1, 3, 512, 512 }; //1, 3, 512, 512
  55. Mat dstimg;
  56. Size resize_size(input_image_[2], input_image_[3]);
  57. resize(srcimg, dstimg, resize_size, 0, 0, cv::INTER_LINEAR);
  58. int channels = dstimg.channels();
  59. input_image_.resize((this->inpWidth * this->inpHeight * dstimg.channels()));
  60. for (int c = 0; c < channels; c++)
  61. {
  62. for (int i = 0; i < this->inpHeight; i++)
  63. {
  64. for (int j = 0; j < this->inpWidth; j++)
  65. {
  66. float pix = dstimg.ptr<uchar>(i)[j * 3 + 2 - c];
  67. input_image_[(c * this->inpHeight * this->inpWidth + i * this->inpWidth + j)] = (pix / 255.0 - mean[c]) / stds[c];
  68. }
  69. }
  70. }
  71. array<int64_t, 4> input_shape_{ 1, 3, this->inpHeight, this->inpWidth };
  72. auto allocator_info = MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU);
  73. Value input_tensor_ = Value::CreateTensor<float>(allocator_info, input_image_.data(), input_image_.size(), input_shape_.data(), input_shape_.size());
  74. vector<Value> ort_outputs = ort_session->Run(RunOptions{ nullptr }, &input_names[0], &input_tensor_, 1, output_names.data(), output_names.size());
  75. float *pred = ort_outputs[0].GetTensorMutableData<float>();
  76. Mat result(outHeight, outWidth, CV_32FC1, pred);
  77. result = 6 - result;
  78. result *= 255;
  79. result.convertTo(result, CV_8UC1);
  80. //namedWindow("分割", WINDOW_NORMAL);
  81. //imshow("分割", result);
  82. //waitKey(1);
  83. Mat binary;
  84. threshold(result, binary, 150, 255, THRESH_BINARY);//二值化阈值处理
  85. //形态学变换
  86. Mat Sobel_Y_thres;
  87. Mat element = cv::getStructuringElement(MORPH_RECT, Size(5, 5));
  88. morphologyEx(binary, Sobel_Y_thres, cv::MORPH_OPEN, element, Point(-1, -1), 2);
  89. //cv::imshow("test", Sobel_Y_thres);
  90. //cv::waitKey(3000);
  91. //cv::destroyWindow("test");
  92. //查找边界轮廓
  93. vector<vector<Point>> contours;
  94. vector<Vec4i> hierarchy;
  95. float pointerArea = 0;
  96. float scaleArea = 0;
  97. vector<int>numArea;
  98. findContours(Sobel_Y_thres, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point());
  99. for (size_t t = 0; t < contours.size(); t++)
  100. {
  101. drawContours(dstimg, contours, -1, Scalar(0, 0, 255), 2, 8);
  102. double area = contourArea(contours[t]);
  103. //计算指针和刻度关系
  104. numArea.push_back(area);
  105. float maxValue = 0;
  106. float minValue = 0;
  107. //scale
  108. maxValue = *max_element(numArea.begin(), numArea.end());
  109. scaleArea = maxValue;
  110. //pointer
  111. minValue = *min_element(numArea.begin(), numArea.end());
  112. if (numArea.size() == 1)
  113. {
  114. pointerArea = 0;
  115. }
  116. else
  117. {
  118. pointerArea = minValue;
  119. }
  120. }
  121. //计算表盘读数
  122. cout << "指针:" << pointerArea << " 刻度:" << scaleArea << endl;
  123. float result_ratio = (1.0 * pointerArea / (pointerArea + scaleArea));
  124. float resutValue = (result_ratio * METER_RANGE);
  125. if (resutValue>0.0f)
  126. {
  127. resutValue += 0.05;
  128. }
  129. cout << "result_ratio:" << result_ratio << " result_value:" << resutValue << endl;
  130. //resutValue = result_value;
  131. //return result_value;
  132. return resutValue;
  133. }