利用matplotlib為圖片上添加觸發(fā)事件進行交互
這篇文章的目的出于實驗的需要,我需要對圖片上的部分區(qū)域做出涂抹標記,本來是選擇用opencv做交互的,但在需要進行圖像的輸出以及鼠標時間添加時,opencv出現(xiàn)錯誤。
解決方案網(wǎng)上有很多,嘗試以后依然bug,這里先做一個記錄,有時間再來處理。
錯誤報告如下:
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庫完成圖像的輸出以及鼠標事件的添加。
點擊圖片,在圖像中鼠標對應位置畫點:
# coding=utf-8 from matplotlib import pyplot as plt import cv2 def on_press(event): if event.inaxes == None: print "none" return #在鼠標的當前位置繪制一個點 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()
先來簡單解釋一下代碼的含義:
fig.canvas.mpl_connect("button_press_event", on_press)#在這個figure上加點擊事件,點擊后的情況在自己寫的on_press()方法里 def on_press(event): event.inaxes.figure.canvas.draw()#用于圖片刷新 event.x#事件的坐標用于其他按鈕點擊和figure點擊發(fā)生沖突時判斷返回 event.xdata,event.ydata#鼠標點擊的位置,與上面那個坐標表示形式不同
最后的輸出結(jié)果入下圖。我們得到了非常奇怪的結(jié)果,如果你自己親自動手試的話體會應該會更有體會,兩邊的圖像本來應該一樣大,但在第一次繪制點的時候,左側(cè)圖像出現(xiàn)了閃動,然后尺寸的比例突然發(fā)生了變化。
是的,圖像尺寸沒有發(fā)生變化,但尺寸的比例的確變了,這里我們要做的就是關閉自動變化的尺度比例。
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) #關閉自動尺度適配 ax.set_autoscale_on(False) plt.axis("off") plt.show()
當然,我們可以改變繪制標記的樣式:
ax.scatter(x,y,c='k',s=25,alpha=1.0,marker='o') #T:散點的顏色 #s:散點的大小 #alpha:是透明程度
現(xiàn)在我們能夠在圖像上進行標記了,但這樣還不夠,程序需要獲取這些標記點。
實際上fig.canvas.mpl_connect("button_press_event", on_press)能夠進行自定義的多參數(shù)傳遞,如果在每次繪制的時候?qū)?shù)據(jù)保存在外部傳入的列表中,那么當畫板被銷毀時,我們就能獲取到原來所有的繪制點。
這里介紹兩種使用方法:
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繪制的點,實際上并沒有大小的概念,這個點實質(zhì)是一個坐標。
如果需要繪制有實際面積的圓形的標記,可以使用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ā)事件進行交互就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python安裝PIL模塊時Unable to find vcvarsall.bat錯誤的解決方法
這篇文章給大家分享了關于python安裝PIL模塊時遇到Unable to find vcvarsall.bat錯誤的解決方法,相信會對不少人有一定的參考借鑒價值。有需要的朋友們下面來一起看看吧。2016-09-09基于python實現(xiàn)地址和經(jīng)緯度轉(zhuǎn)換
這篇文章主要介紹了基于python實現(xiàn)地址和經(jīng)緯度轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-05-05使用Python+OpenCV進行卡類型及16位卡號數(shù)字的OCR功能
本文將使用Python+OpenCV實現(xiàn)模板匹配算法,以自動識別卡的類型和以及16位卡號數(shù)字,通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2021-08-08