您好,登錄后才能下訂單哦!
小編這次要給大家分享的是matplotlib如何在圖片上添加觸發(fā)事件進(jìn)行交互,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
錯誤報(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庫完成圖像的輸出以及鼠標(biāo)事件的添加。
點(diǎn)擊圖片,在圖像中鼠標(biāo)對應(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)前位置繪制一個點(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()
先來簡單解釋一下代碼的含義:
fig.canvas.mpl_connect("button_press_event", on_press)#在這個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ā)生沖突時判斷返回 event.xdata,event.ydata#鼠標(biāo)點(diǎn)擊的位置,與上面那個坐標(biāo)表示形式不同
最后的輸出結(jié)果入下圖。我們得到了非常奇怪的結(jié)果,如果你自己親自動手試的話體會應(yīng)該會更有體會,兩邊的圖像本來應(yīng)該一樣大,但在第一次繪制點(diǎn)的時候,左側(cè)圖像出現(xiàn)了閃動,然后尺寸的比例突然發(fā)生了變化。
是的,圖像尺寸沒有發(fā)生變化,但尺寸的比例的確變了,這里我們要做的就是關(guān)閉自動變化的尺度比例。
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)閉自動尺度適配 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ù)傳遞,如果在每次繪制的時候?qū)?shù)據(jù)保存在外部傳入的列表中,那么當(dāng)畫板被銷毀時,我們就能獲取到原來所有的繪制點(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í)際上并沒有大小的概念,這個點(diǎn)實(shí)質(zhì)是一個坐標(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)
看完這篇關(guān)于matplotlib如何在圖片上添加觸發(fā)事件進(jìn)行交互的文章,如果覺得文章內(nèi)容寫得不錯的話,可以把它分享出去給更多人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。