1. 程式人生 > >關於opencv2和3在影象特徵識別的區別。

關於opencv2和3在影象特徵識別的區別。

轉https://www.hongweipeng.com/index.php/archives/709/

1.opencv3並不自帶sift,fast等,需要額外安裝。

2.cv2.drawMatches這個函式在OpenCV 2.4.12中不存在。3.0以後才提供。所以執行時得到這樣的報錯。

函式原型如下:

cv2.drawMatches(img1, keypoints1, img2, keypoints2, matches1to2[, outImg[, matchColor[, singlePointColor[, matchesMask[, flags]]]]]) → outImg
  • img1 – 源影象1

  • keypoints1 –源影象1的特徵點.

  • img2 – 源影象2.

  • keypoints2 – 源影象2的特徵點

  • matches1to2 – 源影象1的特徵點匹配源影象2的特徵點[matches[i]] DMatch.

  • outImg – 輸出影象具體由flags決定.

  • matchColor – 匹配的顏色(特徵點和連線),若matchColor==Scalar::all(-1),顏色隨機.

  • singlePointColor – 單個點的顏色,即未配對的特徵點,若matchColor==Scalar::all(-1),顏色隨機.

  • matchesMask – Mask決定哪些點將被畫出,若為空,則畫出所有匹配點.

  • flags – Fdefined by DrawMatchesFlags.

實現

自給自足,豐衣足食。

def drawMatches(img1, kp1, img2, kp2, matches):
    """
    My own implementation of cv2.drawMatches as OpenCV 2.4.9
    does not have this function available but it's supported in
    OpenCV 3.0.0

    This function takes in two images with their associated
    keypoints, as well as a list of DMatch data structure (matches)
    that contains which keypoints matched in which images.

    An image will be produced where a montage is shown with
    the first image followed by the second image beside it.

    Keypoints are delineated with circles, while lines are connected
    between matching keypoints.

    img1,img2 - Grayscale images
    kp1,kp2 - Detected list of keypoints through any of the OpenCV keypoint
              detection algorithms
    matches - A list of matches of corresponding keypoints through any
              OpenCV keypoint matching algorithm
    """
# Create a new output image that concatenates the two images together # (a.k.a) a montage rows1 = img1.shape[0] cols1 = img1.shape[1] rows2 = img2.shape[0] cols2 = img2.shape[1] out = np.zeros((max([rows1,rows2]),cols1+cols2,3), dtype='uint8') # Place the first image to the left out[:rows1, :cols1] = np.dstack([img1]) # Place the next image to the right of it out[:rows2, cols1:] = np.dstack([img2]) # For each pair of points we have between both images # draw circles, then connect a line between them for mat in matches: # Get the matching keypoints for each of the images img1_idx = mat.queryIdx img2_idx = mat.trainIdx # x - columns # y - rows (x1,y1) = kp1[img1_idx].pt (x2,y2) = kp2[img2_idx].pt # Draw a small circle at both co-ordinates # radius 4 # colour blue # thickness = 1 cv2.circle(out, (int(x1),int(y1)), 4, (255, 0, 0), 1) cv2.circle(out, (int(x2)+cols1,int(y2)), 4, (255, 0, 0), 1) # Draw a line in between the two points # thickness = 1 # colour blue cv2.line(out, (int(x1),int(y1)), (int(x2)+cols1,int(y2)), (255, 0, 0), 1) # Show the image # cv2.imshow('Matched Features', out) # cv2.waitKey(0) # cv2.destroyWindow('Matched Features') # Also return the image if you'd like a copy return out

測試

# -*- coding: utf-8 -*-
import cv2
import numpy as np

img1 = cv2.imread('static/images/1a.jpg',cv2.IMREAD_COLOR)
img2 = cv2.imread('static/images/1b.jpg',cv2.IMREAD_COLOR)

# img1 = cv2.resize(img1, (256, 256))
# img2 = cv2.resize(img2, (256, 256))

gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

#SIFT
detector = cv2.SIFT()

# 特徵點集
keypoints1 = detector.detect(gray1, None)
keypoints2 = detector.detect(gray2, None)

outimg1 = cv2.drawKeypoints(gray1, keypoints1)
outimg2 = cv2.drawKeypoints(gray2, keypoints2)


cv2.imshow('img1', outimg1)
cv2.imshow('img2', outimg2)

# kp,des = sift.compute(gray,kp)
kp1, des1 = detector.compute(gray1, keypoints1)
kp2, des2 = detector.compute(gray2, keypoints2)


# 定義一個burte force matcher物件
matcher = cv2.BFMatcher()

matches = matcher.match(des1, des2)
matches = sorted(matches, key = lambda x:x.distance)

end_img = drawMatches(img1, kp1, img2, kp2, matches[:30])
cv2.imshow('end_img', end_img)

cv2.waitKey(0)
cv2.destroyAllWindows()

相關推薦

關於opencv23影象特徵識別區別

轉https://www.hongweipeng.com/index.php/archives/709/ 1.opencv3並不自帶sift,fast等,需要額外安裝。 2.cv2.drawMatches這個函式在OpenCV 2.4.12中不存在。3.0以後才提供。所以

opencv2.4.9影象特徵點的提取匹配

opencv影象特徵點的提取和匹配(二) 在上面一節大概分析了一下在opencv中如何實現特徵的提取,這一節分析一下opencv中如何生成特徵點的描述子並對描述子進行匹配。opencv提取的特徵點都儲存在一個向量(vector)中,元素的型別是Point類。所有實現特徵點描

程序通過定義學生結構體變量,存儲學生的學號、姓名3門課的成績函數fun的功能是:對形參b所指結構體變量中的數據進行修改,並在主函數中輸出修改後的數據

一次 數據 mod long 成績 nbsp data 例如 main 程序通過定義學生結構體變量,存儲學生的學號、姓名和3門課的成績。函數fun的功能是:對形參b所指結構體變量中的數據進行修改,並在主函數中輸出修改後的數據。例如,若b所指變量t中的學號、姓名和三門課的成績

python單例模式控制成只初始化一次,常規型的python單例模式在新式類經典類中的區別

spa alt let __main__ python2 urn 時間 div 分享 單例模式的寫法非常多,但常規型的單例模式就是這樣寫的,各種代碼可能略有差異,但核心就是要搞清楚類屬性 實例屬性,就很容易寫出來,原理完全一模一樣。 如下: 源碼: class

策略模式橋接模式的區別

策略模式: 設計一個介面或者抽象類。 public interface Employment{ public void work(); } 多種具體實現: public class Enterprise implements Employment{  

mongodb 互動式操作script檔案指令碼的區別

Differences Between Interactive and Scripted mongo https://docs.mongodb.org/manual/tutorial/write-scripts-for-the-mongo-shell/#differ

vue-cli 2.x3.x安裝的區別

1.全域性安裝vue的腳手架:vue-cli(指定版本後面加@2.x.x) npm install -g vue-cli npm install -g @vue/cli 2.使用初始化 vue 專案: vue init webpack <project-name> vue create

javascript for迴圈條件 2個條件 3個條件的區別

<!doctype html> <html lang="en">  <head>   <meta charset="UTF-8">   <meta name="Generator" content="EditPlus®"

hibernate4.0+版本3.0+版本的區別總結

1.資料庫方言設定 <property name=”dialect”>org.hibernate.dialect.MySQL5Dialect</property> 在3.3版本中連線MySQL資料庫只需要指明MySQLDialect即可。在4.1版本

10. OverrideOverload的含義與區別

Override(重寫,執行時多型): 是子類對容許訪問父類的方法進行新的編寫的稱呼。其中方法名和形參不能發生改變;即外殼不變,內容可以發生改變。 優點:子類可以根據實際需要,定義特定於自己的行為。也就是說子類能夠實際根據需要實現父類的方法。

簡要描述行內元素塊級元素的區別

塊級元素的前後都會自動換行,如同存在換行符一樣。預設情況下,塊級元素會獨佔一行。例如,<p>、<hn>、<div>都是塊級元素。在顯示這些元素中間的文字時,都將從新行

普通集合泛型集合的區別,哈希表字典表的區別,隊列堆棧的區別以及堆棧的區別

ear 釋放內存 main 廣泛 棧內存 節點 except {0} 常數 普通集合和泛型集合的區別: 泛型集合與傳統集合相比 類型更安全. 泛型集合無需裝箱拆箱操作. 泛型的重要性. 泛型是未來五年的主流技術 ...通常情況下,建議您使用泛型集合,因為這樣可以獲得類型安全

html、val、attr、prop區別this.value$(this).val()區別以及return用法

生態 所有 select 批量 控制 添加屬性 ext his 區別 html(): html() 方法返回或設置被選元素的內容 (inner HTML)。 當使用該方法讀取多個值時,它會返回第一個匹配元素的內容。 當使用該方法設置一個值時,它會覆蓋所有匹配元素的內容。 取

Python 2.7.x 3.x 版本區別小結

pythonpython現在很火,最近花了些時間去了解了一下,最初了解的是2.7.x版本,感覺,從書寫上是很不習慣,少了一雙大概號,取而代之的是縮進;然後跟kotlin和swift一樣省去了每行的分號,象我們這種分號強迫癥的人真心的不習慣;還有!True的條件改成not True、while後面可以跟else

cookies,sessionStorage localStorage 的區別

瀏覽器和服務器 請求 cli 存儲 之前 自動刪除 bsp 不能 窗口 cookie是網站為了標示用戶身份而儲存在用戶本地終端(Client Side)上的數據(通常經過加密)。 cookie數據始終在同源的http請求中攜帶(即使不需要),記會在瀏覽器和服務器間來回傳遞。

Python 2.7.x 3.x 版本的語法區別

ast ssa pan att 可叠代對象 abcde unicode 用戶 列表 <__future__模塊> Python 3.x引入了一些與Python 2不兼容的關鍵字和特性,在Python 2中,可以通過內置的__future__模塊導入這些新

【JS點滴】substringsubstr以及slicesplice的用法區別

[0 相等 交換 top subst char ima cas 負數 那麽就由一道筆試題引入吧,已知有字符串a=”get-element-by-id”,寫一個function將其轉化成駝峰表示法”getElementById”; var a = "get-element-

C#:refout的聯系及區別

args c# 區分 div temp 查看 按引用傳遞 影響 bsp 之前學習C#時候就遇到了這個問題,不過當時沒有深究。昨晚想到這個問題時候自己嘗試敲了敲代碼,結果從運行的結果來看,越看越亂。在查看了一些資料的基礎上,自己總結了一下。 可能會有點亂,但是自己總結出來的東

python23區別,怎麽樣做到輕松切換23

除了 之間 空格 Go 遍歷 error 努力 spa 捕獲異常 以下是菜鳥教程列舉的。這些零散的改變需要註意。 下面這些東西可能平時的程序根本沒用到,或者稍加註意就可以了。但2和3最主要的區別是,掌握編碼。 編碼在所有程序中無處不在,處理不好,要麽亂碼,要麽編碼