XuAilibTest.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // XuAilibTest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
  2. //
  3. //#pragma comment(lib,LIBPATH(__FILE__, "..\\srilm-lib\\common.lib"))
  4. #include <iostream>
  5. #include <SegDetector.h>
  6. #include <NanoDetector.h>
  7. #include <opencv2\imgproc\types_c.h>
  8. #include<opencv2/imgproc/imgproc_c.h>
  9. #include "LineHandle.h"
  10. #include<numeric>
  11. #include<windows.h>
  12. #include <DoublePointerAlgorithm.h>
  13. #include "SemicircularMeter.h"
  14. #include <BrowseForFolder.h>
  15. using namespace std;
  16. using namespace cv;
  17. int main()
  18. {
  19. const char* pstrModelFile = "Model_det.m";
  20. CNanoDetector cNanoDetector(pstrModelFile);
  21. cNanoDetector.Init();
  22. //LPCWSTR dir = TEXT("C:\\Users\\77411\\Desktop\\Images4\\");
  23. //ShellExecute(NULL, NULL, dir, NULL, NULL, SW_SHOWNORMAL);
  24. string path = "";
  25. BrowseForFolder browseForFolder;
  26. browseForFolder.SelectFile(path);
  27. Mat mat = imread(path);
  28. if ( mat.data!=NULL)
  29. {
  30. cNanoDetector.Execute(mat);
  31. vector<DetectorResult> vec;
  32. cNanoDetector.GetResults(vec);
  33. if (vec.size()>0)
  34. {
  35. for (size_t i = 0; i < vec.size(); i++)
  36. {
  37. int width = vec[i].width;
  38. int height = vec[i].height;
  39. Rect m_select = Rect(vec[i].left, vec[i].top, width, height);
  40. if ( string("biaopan").compare( vec[i].name ) ==0 )
  41. {
  42. Mat roi = mat(m_select);
  43. DoublePointerAlgorithm doublePointerAlgorithm;
  44. int result = doublePointerAlgorithm.GetResult(roi, true);
  45. cout << "biaopan结果 :" << result << endl;
  46. }
  47. if (string("ceshi").compare(vec[i].name) == 0)
  48. {
  49. Mat roi = mat(m_select);
  50. SemicircularMeter semicircularMeter;
  51. double result = semicircularMeter.GetResult(roi, false);
  52. cout << "ceshi结果 :" << result << endl;
  53. }
  54. }
  55. waitKey(0);
  56. //// destroyAllWindows();
  57. }
  58. }
  59. string str;
  60. std::cin >> str;
  61. }
  62. void CVmatToDatumEx(const std::vector<cv::Mat> images, Datum& datum, float input_scale = 1.f)
  63. {
  64. Shape shape(images.size(), images[0].channels(), images[0].rows, images[0].cols);
  65. datum.Reshape(shape);
  66. cv::Size input_size = images[0].size();
  67. const int channels = images[0].channels();
  68. datum.SetDataItemSize(1);
  69. uchar* datum_data = (uchar*)datum.Getdata();
  70. unsigned int offsetN = shape.C * shape.H * shape.W;
  71. for (int n = 0; n < images.size(); n++)
  72. {
  73. const cv::Mat& image = images[n];
  74. assert(input_size == image.size());
  75. assert(channels == image.channels());
  76. if (image.isContinuous())
  77. {
  78. memcpy(datum_data + n * offsetN, image.data, offsetN);
  79. }
  80. }
  81. }
  82. void CVmatVecToDatum(const std::vector<cv::Mat> images, Datum& datum, float input_scale = 1.f)
  83. {
  84. return CVmatToDatumEx(images, datum);
  85. //all input image shape must be same.
  86. Shape shape(images.size(), images[0].channels(), images[0].rows, images[0].cols);
  87. datum.Reshape(shape);
  88. cv::Size input_size = images[0].size();
  89. const int channels = images[0].channels();
  90. float* datum_data = (float*)datum.Getdata();
  91. for (int n = 0; n < images.size(); n++)
  92. {
  93. const cv::Mat& image = images[n];
  94. assert(input_size == image.size());
  95. assert(channels == image.channels());
  96. unsigned int offsetN = n * shape.C * shape.H * shape.W;
  97. for (int h = 0; h < image.rows; h++)
  98. {
  99. const uchar* ptr = image.ptr<uchar>(h);
  100. int img_index = 0;
  101. for (int w = 0; w < image.cols; ++w)
  102. {
  103. for (int c = 0; c < image.channels(); ++c)
  104. {
  105. int datum_index = (c * image.rows + h) * image.cols + w + offsetN;
  106. float value = (static_cast<float>(ptr[img_index++]));
  107. datum_data[datum_index] = value * (input_scale / 255.f);;
  108. }
  109. }
  110. }
  111. }
  112. }
  113. // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
  114. // 调试程序: F5 或调试 >“开始调试”菜单
  115. // 入门使用技巧:
  116. // 1. 使用解决方案资源管理器窗口添加/管理文件
  117. // 2. 使用团队资源管理器窗口连接到源代码管理
  118. // 3. 使用输出窗口查看生成输出和其他消息
  119. // 4. 使用错误列表窗口查看错误
  120. // 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
  121. // 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件