日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

OpenCV学习笔记——判断两张图的相似度

發(fā)布時(shí)間:2023/12/18 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OpenCV学习笔记——判断两张图的相似度 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

判斷兩張圖的相似度

方法

  • 直方圖對(duì)比法
  • ORB算法
  • 實(shí)驗(yàn)

    1.直方圖對(duì)比法

    參考如何使用OpenCV3直方圖方法進(jìn)行人臉相似度對(duì)比
    因?yàn)槲业沫h(huán)境是VS2010+OpenCV2.4.8,所以在原版的基礎(chǔ)上做了一點(diǎn)小修改。

    #include <opencv2/opencv.hpp> #include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <iostream> #include <string> #include <stdlib.h> using namespace std; using namespace cv; //直方圖比對(duì) bool compareFacesByHist(Mat img,Mat orgImg) {Mat tmpImg;resize(img, tmpImg, Size(orgImg.cols, orgImg.rows));imshow("Img1", img);imshow("tmpImg", tmpImg);imshow("orgImg", orgImg);//HSV顏色特征模型(色調(diào)H,飽和度S,亮度V)cvtColor(tmpImg, tmpImg, COLOR_BGR2HSV);cvtColor(orgImg, orgImg, COLOR_BGR2HSV);//直方圖尺寸設(shè)置//一個(gè)灰度值可以設(shè)定一個(gè)bins,256個(gè)灰度值就可以設(shè)定256個(gè)bins//對(duì)應(yīng)HSV格式,構(gòu)建二維直方圖//每個(gè)維度的直方圖灰度值劃分為256塊進(jìn)行統(tǒng)計(jì),也可以使用其他值int hBins = 256, sBins = 256;int histSize[] = { hBins,sBins };//H:0~180, S:0~255,V:0~255//H色調(diào)取值范圍float hRanges[] = { 0,180 };//S飽和度取值范圍float sRanges[] = { 0,255 };const float* ranges[] = { hRanges,sRanges };int channels[] = { 0,1 };//二維直方圖MatND hist1, hist2;calcHist(&tmpImg, 1, channels, Mat(), hist1,2,histSize, ranges, true, false);normalize(hist1, hist1, 0, 1, NORM_MINMAX, -1, Mat());calcHist(&orgImg, 1, channels, Mat(), hist2, 2, histSize, ranges, true, false);normalize(hist2, hist2, 0, 1, NORM_MINMAX, -1, Mat());double similarityValue = compareHist(hist1, hist2, CV_COMP_CORREL);cout << "相似度:" << similarityValue << endl;if (similarityValue >= 0.85){return true;}return false; }int main() {Mat orgImg = imread("p34.png");Mat img = imread("p45.png");compareFacesByHist(img, orgImg);waitKey(0);return 0; }

    主要實(shí)現(xiàn)思路:
    1)從本地讀取兩張圖像
    2)將需要對(duì)比的圖像進(jìn)行HSV格式轉(zhuǎn)換
    3)構(gòu)建圖像的直方圖模型,并進(jìn)行直方圖歸一化
    4)比較兩張圖片的直方圖模型,計(jì)算圖片的直方圖相似度
    5)判斷相似度值,如果大于0.85左右我們可以認(rèn)為兩張圖片比較相似的

    2. ORB算法

    參考OpenCV圖像相似度ORB算法(圖像特征比對(duì))

    #include "stdafx.h" #include <iostream> #include<opencv2/opencv.hpp> #include "opencv2/core/core.hpp" #include "opencv2/features2d/features2d.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/nonfree/nonfree.hpp" #include "opencv2/nonfree/features2d.hpp" #include<opencv2/legacy/legacy.hpp>using namespace std; using namespace cv;int getORB(char * imagePatha,char * imagePathb){double t;t=getTickCount();Mat img_1 = imread(imagePatha);Mat img_2 = imread(imagePathb);if (!img_1.data || !img_2.data) {cout << "error reading images " << endl; return -1;}ORB orb;vector<KeyPoint> keyPoints_1, keyPoints_2;Mat descriptors_1, descriptors_2;orb(img_1, Mat(), keyPoints_1, descriptors_1);orb(img_2, Mat(), keyPoints_2, descriptors_2);BruteForceMatcher<HammingLUT> matcher;vector<DMatch> matches;matcher.match(descriptors_1, descriptors_2, matches);double max_dist = 0; double min_dist = 100;for( int i = 0; i < descriptors_1.rows; i++ ) {double dist = matches[i].distance;if( dist < min_dist ) min_dist = dist;if( dist > max_dist ) max_dist = dist;}printf("-- Max dist : %f \n", max_dist );printf("-- Min dist : %f \n", min_dist );std::vector< DMatch > good_matches;for( int i = 0; i < descriptors_1.rows; i++ ) {if( matches[i].distance < 0.6*max_dist ){good_matches.push_back(matches[i]);}}t=getTickCount()-t;t=t*1000/getTickFrequency();Mat img_matches;drawMatches(img_1, keyPoints_1, img_2, keyPoints_2,good_matches, img_matches,Scalar::all(-1), Scalar::all(-1),vector<char>(),DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);imshow( "Match", img_matches); printf( "%f ms\n", t );cvWaitKey(0);return 0; }int main() {getORB("r.png","s.png");return 0; }

    總結(jié)

    以上是生活随笔為你收集整理的OpenCV学习笔记——判断两张图的相似度的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。