Export.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "Export.h"
  2. #include "FeatureMatch.h"
  3. // 计算2图像的相似度
  4. // isLesion: true 表示输入图像只裁取了病灶区域,对比两个病灶的相似程度
  5. // false 表示输入图像是整个原始图像,对比两幅图像的相似程度
  6. bool __cdecl CalcuSimilarity(__in unsigned char* imgData1, __in unsigned char* imgData2, bool isLesion,
  7. ImgInfo imgInfo1, ImgInfo imgInfo2, float& outVal)
  8. {
  9. try
  10. {
  11. if (imgData1 == NULL || imgData2 == NULL)
  12. {
  13. #ifdef _DEBUG
  14. cout << "图像数据加载失败!" << endl;
  15. #endif
  16. return false;
  17. }
  18. int imgW1 = imgInfo1.imgW;
  19. int imgH1 = imgInfo1.imgH;
  20. int imgW2 = imgInfo2.imgW;
  21. int imgH2 = imgInfo2.imgH;
  22. int channel = imgInfo1.channel;
  23. if (imgW1 == 0 || imgW2==0)
  24. {
  25. return false;
  26. }
  27. if (!(channel == 1 || channel == 3 || channel == 4) )
  28. {
  29. return false;
  30. }
  31. Mat img1(cv::Size(imgW1, imgH1), CV_8UC(channel), imgData1);
  32. Mat img2(cv::Size(imgW2, imgH2), CV_8UC(channel), imgData2);
  33. Mat imgGray1, imgGray2;
  34. if (channel > 1)
  35. {
  36. cv::cvtColor(img1, imgGray1, COLOR_BGR2GRAY);
  37. cv::cvtColor(img2, imgGray2, COLOR_BGR2GRAY);
  38. }
  39. else
  40. {
  41. imgGray1 = img1.clone();
  42. imgGray2 = img2.clone();
  43. }
  44. int threWidth;
  45. if (!isLesion)
  46. {
  47. // 对比图像相似程度,需要先做滤波
  48. float sigma = 3.f; //高斯核标准差
  49. int kSize = static_cast<int>(round(2 * sigma) * 2) + 1;
  50. cv::GaussianBlur(imgGray1, imgGray1, Size(kSize, kSize), sigma, sigma, BORDER_CONSTANT);
  51. cv::GaussianBlur(imgGray2, imgGray2, Size(kSize, kSize), sigma, sigma, BORDER_CONSTANT);
  52. threWidth = 80;
  53. }
  54. else
  55. {
  56. cv::medianBlur(imgGray1, imgGray1, 5);
  57. cv::medianBlur(imgGray2, imgGray2, 5);
  58. threWidth = 140;
  59. }
  60. if (imgW1 > threWidth)
  61. {
  62. int hightNew1 = cvRound((threWidth /(float)imgW1)*imgH1 );
  63. Size sz(threWidth, hightNew1);
  64. resize(imgGray1, imgGray1, sz);
  65. }
  66. if (imgW2 > threWidth)
  67. {
  68. int hightNew2 = cvRound((threWidth /(float)imgW2)*imgH2 );
  69. Size sz(threWidth, hightNew2);
  70. resize(imgGray2, imgGray2, sz);
  71. }
  72. vector<KeyPoint> keypoints1, keypoints2;
  73. vector<DMatch> rightMatchs;
  74. SIFTMatch(imgGray1, imgGray2, keypoints1, keypoints2, rightMatchs);
  75. int num = (int)rightMatchs.size(); //正确匹配点的个数,以此衡量2张图像的相似度
  76. if (keypoints1.size() + keypoints2.size() <= 0)
  77. {
  78. outVal = 0.f;
  79. }
  80. else
  81. {
  82. //outVal = 1.0f * num / float(keypoints1.size() + keypoints2.size() - num); //旧版公式
  83. outVal = 2.0f * num / float(keypoints1.size() + keypoints2.size()); //相似度计算公式
  84. }
  85. if (outVal > 1.0f)
  86. {
  87. outVal = 1.0f;
  88. }
  89. #ifdef _DEBUG
  90. cout << "正确匹配特征点的数目是:" << num << endl;
  91. cout << "2幅图的相似度是:" << outVal << endl;
  92. cout << endl;
  93. #endif
  94. return true;
  95. }
  96. catch (...)
  97. {
  98. return false;
  99. }
  100. }