利用matplotlib為圖片上添加觸發(fā)事件進(jìn)行交互
這篇文章的目的出于實(shí)驗(yàn)的需要,我需要對(duì)圖片上的部分區(qū)域做出涂抹標(biāo)記,本來是選擇用opencv做交互的,但在需要進(jìn)行圖像的輸出以及鼠標(biāo)時(shí)間添加時(shí),opencv出現(xiàn)錯(cuò)誤。
解決方案網(wǎng)上有很多,嘗試以后依然bug,這里先做一個(gè)記錄,有時(shí)間再來處理。
錯(cuò)誤報(bào)告如下:
OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvShowImage, file -------src-dir-------/opencv-2.4.10/modules/highgui/src/window.cpp, line 501
Traceback (most recent call last):
File "test.py", line 20, in <module>
cv2.imshow('img',img)
cv2.error: -------src-dir-------/opencv-2.4.10/modules/highgui/src/window.cpp:501: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvShowImage
這里我們切換另一種解決方案,利用python的matplotlib庫(kù)完成圖像的輸出以及鼠標(biāo)事件的添加。
點(diǎn)擊圖片,在圖像中鼠標(biāo)對(duì)應(yīng)位置畫點(diǎn):
# coding=utf-8 from matplotlib import pyplot as plt import cv2 def on_press(event): if event.inaxes == None: print "none" return #在鼠標(biāo)的當(dāng)前位置繪制一個(gè)點(diǎn) ax.scatter(event.xdata, event.ydata) #更新畫板 fig.canvas.draw() if __name__ == "__main__": fileN = r'./0107_1.3.6.1.4.1.14519.5.2.1.6279.6001.263660956768649083933159084365.bmp' img = cv2.imread(fileN) cv2.imshow('img',img) fig = py.figure() fig.canvas.mpl_connect("button_press_event", on_press) ax = fig.add_subplot(121) ax1 = fig.add_subplot(122) ax.imshow(img) ax1.imshow(img) plt.axis("off") plt.show()
先來簡(jiǎn)單解釋一下代碼的含義:
fig.canvas.mpl_connect("button_press_event", on_press)#在這個(gè)figure上加點(diǎn)擊事件,點(diǎn)擊后的情況在自己寫的on_press()方法里 def on_press(event): event.inaxes.figure.canvas.draw()#用于圖片刷新 event.x#事件的坐標(biāo)用于其他按鈕點(diǎn)擊和figure點(diǎn)擊發(fā)生沖突時(shí)判斷返回 event.xdata,event.ydata#鼠標(biāo)點(diǎn)擊的位置,與上面那個(gè)坐標(biāo)表示形式不同
最后的輸出結(jié)果入下圖。我們得到了非常奇怪的結(jié)果,如果你自己親自動(dòng)手試的話體會(huì)應(yīng)該會(huì)更有體會(huì),兩邊的圖像本來應(yīng)該一樣大,但在第一次繪制點(diǎn)的時(shí)候,左側(cè)圖像出現(xiàn)了閃動(dòng),然后尺寸的比例突然發(fā)生了變化。
是的,圖像尺寸沒有發(fā)生變化,但尺寸的比例的確變了,這里我們要做的就是關(guān)閉自動(dòng)變化的尺度比例。
if __name__ == "__main__": fileN = r'./0107_1.3.6.1.4.1.14519.5.2.1.6279.6001.263660956768649083933159084365.bmp' img = cv2.imread(fileN) cv2.imshow('img',img) fig = py.figure() fig.canvas.mpl_connect("button_press_event", on_press) ax = fig.add_subplot(121) ax1 = fig.add_subplot(122) ax.imshow(img) ax1.imshow(img) #關(guān)閉自動(dòng)尺度適配 ax.set_autoscale_on(False) plt.axis("off") plt.show()
當(dāng)然,我們可以改變繪制標(biāo)記的樣式:
ax.scatter(x,y,c='k',s=25,alpha=1.0,marker='o') #T:散點(diǎn)的顏色 #s:散點(diǎn)的大小 #alpha:是透明程度
現(xiàn)在我們能夠在圖像上進(jìn)行標(biāo)記了,但這樣還不夠,程序需要獲取這些標(biāo)記點(diǎn)。
實(shí)際上fig.canvas.mpl_connect("button_press_event", on_press)能夠進(jìn)行自定義的多參數(shù)傳遞,如果在每次繪制的時(shí)候?qū)?shù)據(jù)保存在外部傳入的列表中,那么當(dāng)畫板被銷毀時(shí),我們就能獲取到原來所有的繪制點(diǎn)。
這里介紹兩種使用方法:
def on_key(event, arg1, arg2, arg3): pass canvas.mpl_connect('key_press_event', lambda event: on_key(event, plt1, plt2, plt3))
和
def on_key(event, args_list): pass fig.canvas.mpl_connect('key_press_event', lambda event: on_key(event, [plt1, plt2, plt3]))
這里需要注意的是scatter繪制的點(diǎn),實(shí)際上并沒有大小的概念,這個(gè)點(diǎn)實(shí)質(zhì)是一個(gè)坐標(biāo)。
如果需要繪制有實(shí)際面積的圓形的標(biāo)記,可以使用matplotlib.patches.Circle
具體的使用如下:
from matplotlib.patches import Circle fig = plt.figure() ax = fig.add_subplot(111) cir = Circle(xy = (event.xdata, event.ydata),facecolor = 'black', edgecolor='black',radius=10, alpha=1.0) ax.add_patch(cir)
以上這篇利用matplotlib為圖片上添加觸發(fā)事件進(jìn)行交互就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python安裝PIL模塊時(shí)Unable to find vcvarsall.bat錯(cuò)誤的解決方法
這篇文章給大家分享了關(guān)于python安裝PIL模塊時(shí)遇到Unable to find vcvarsall.bat錯(cuò)誤的解決方法,相信會(huì)對(duì)不少人有一定的參考借鑒價(jià)值。有需要的朋友們下面來一起看看吧。2016-09-09基于python實(shí)現(xiàn)地址和經(jīng)緯度轉(zhuǎn)換
這篇文章主要介紹了基于python實(shí)現(xiàn)地址和經(jīng)緯度轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05使用Python+OpenCV進(jìn)行卡類型及16位卡號(hào)數(shù)字的OCR功能
本文將使用Python+OpenCV實(shí)現(xiàn)模板匹配算法,以自動(dòng)識(shí)別卡的類型和以及16位卡號(hào)數(shù)字,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-08-08解決pycharm不能自動(dòng)保存在遠(yuǎn)程linux中的問題
這篇文章主要介紹了解決pycharm不能自動(dòng)保存在遠(yuǎn)程linux中的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02