123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #include "Export.h"
- #include "FeatureMatch.h"
- // 计算2图像的相似度
- // isLesion: true 表示输入图像只裁取了病灶区域,对比两个病灶的相似程度
- // false 表示输入图像是整个原始图像,对比两幅图像的相似程度
- bool __cdecl CalcuSimilarity(__in unsigned char* imgData1, __in unsigned char* imgData2, bool isLesion,
- ImgInfo imgInfo1, ImgInfo imgInfo2, float& outVal)
- {
- try
- {
- if (imgData1 == NULL || imgData2 == NULL)
- {
- #ifdef _DEBUG
- cout << "图像数据加载失败!" << endl;
- #endif
- return false;
- }
- int imgW1 = imgInfo1.imgW;
- int imgH1 = imgInfo1.imgH;
- int imgW2 = imgInfo2.imgW;
- int imgH2 = imgInfo2.imgH;
- int channel = imgInfo1.channel;
- if (imgW1 == 0 || imgW2==0)
- {
- return false;
- }
- if (!(channel == 1 || channel == 3 || channel == 4) )
- {
- return false;
- }
- Mat img1(cv::Size(imgW1, imgH1), CV_8UC(channel), imgData1);
- Mat img2(cv::Size(imgW2, imgH2), CV_8UC(channel), imgData2);
- Mat imgGray1, imgGray2;
- if (channel > 1)
- {
- cv::cvtColor(img1, imgGray1, COLOR_BGR2GRAY);
- cv::cvtColor(img2, imgGray2, COLOR_BGR2GRAY);
- }
- else
- {
- imgGray1 = img1.clone();
- imgGray2 = img2.clone();
- }
- int threWidth;
- if (!isLesion)
- {
- // 对比图像相似程度,需要先做滤波
- float sigma = 3.f; //高斯核标准差
- int kSize = static_cast<int>(round(2 * sigma) * 2) + 1;
- cv::GaussianBlur(imgGray1, imgGray1, Size(kSize, kSize), sigma, sigma, BORDER_CONSTANT);
- cv::GaussianBlur(imgGray2, imgGray2, Size(kSize, kSize), sigma, sigma, BORDER_CONSTANT);
- threWidth = 80;
- }
- else
- {
- cv::medianBlur(imgGray1, imgGray1, 5);
- cv::medianBlur(imgGray2, imgGray2, 5);
- threWidth = 140;
- }
- if (imgW1 > threWidth)
- {
- int hightNew1 = cvRound((threWidth /(float)imgW1)*imgH1 );
- Size sz(threWidth, hightNew1);
- resize(imgGray1, imgGray1, sz);
- }
- if (imgW2 > threWidth)
- {
- int hightNew2 = cvRound((threWidth /(float)imgW2)*imgH2 );
- Size sz(threWidth, hightNew2);
- resize(imgGray2, imgGray2, sz);
- }
- vector<KeyPoint> keypoints1, keypoints2;
- vector<DMatch> rightMatchs;
- SIFTMatch(imgGray1, imgGray2, keypoints1, keypoints2, rightMatchs);
- int num = (int)rightMatchs.size(); //正确匹配点的个数,以此衡量2张图像的相似度
- if (keypoints1.size() + keypoints2.size() <= 0)
- {
- outVal = 0.f;
- }
- else
- {
- //outVal = 1.0f * num / float(keypoints1.size() + keypoints2.size() - num); //旧版公式
- outVal = 2.0f * num / float(keypoints1.size() + keypoints2.size()); //相似度计算公式
- }
- if (outVal > 1.0f)
- {
- outVal = 1.0f;
- }
- #ifdef _DEBUG
- cout << "正确匹配特征点的数目是:" << num << endl;
- cout << "2幅图的相似度是:" << outVal << endl;
- cout << endl;
- #endif
- return true;
- }
- catch (...)
- {
- return false;
- }
- }
|