0
使用python框架,我們可以創建圖像段,如附件所示。現在,基於鼠標點擊圖像片段,我們需要使用特定的顏色來突出顯示片段。基於鼠標點擊的圖像分割
基於鼠標點擊我可以得到特定位置的x/y座標。請告訴我如何檢查座標屬於哪個圖像段?
以下是代碼片段:
from skimage.segmentation import felzenszwalb, slic,quickshift
from skimage.segmentation import mark_boundaries
from skimage.segmentation import find_boundaries
from skimage.util import img_as_float
from skimage import io
import matplotlib.pyplot as plt
from skimage import measure
from skimage import restoration
from skimage import img_as_float
import numpy as np
coords = []
def find_nearest(array,value):
idx = (np.abs(array-value)).argmin()
return array[idx]
def onclick(event):
global ix, iy
ix, iy = event.xdata, event.ydata
print ('ix ',ix)
print ("iy ",iy)
color = np.float64([1,0,1]) # red color
image[segments == 14] = color
mark_boundaries(image, segments)
ax.imshow(mark_boundaries(image, segments))
coords.append((ix, iy))
return
image=img_as_float(io.imread("amazon.jpg"))
segments = quickshift(image, ratio=1.0, kernel_size=20, max_dist=10,
return_tree=False, sigma=0, convert2lab=True, random_seed=42)
fig = plt.figure("Superpixels -- %d segments" % (500))
ax = fig.add_subplot(1, 1, 1)
fig,ax = plt.subplots()
color = np.float64([1,0,0])
image[segments == 14] = color # desired segment to be colored
fig.canvas.mpl_connect('button_press_event', onclick)
ax.imshow(mark_boundaries(image, segments))
plt.axis("off")
plt.show()
以下是在點擊的方法中添加的代碼:** clicked_segment =段[event.xdata,event.ydata]
圖像[clicked_segment] =顏色mark_boundaries (圖片,片段) fig.canvas.draw() fig.canvas.update() fig.canvas.flush_events()**但是顏色在所需段中仍未更新。請建議 –
我的matplotlib版本:1.5.3和Python版本3.5.2 –
我想我應該提到這個:'clicked_segment'是一個標量值,對應於'segments'矩陣中的標記區域。設置顏色時仍然需要使用邏輯數組索引。 '圖片[段== clicked_segment] =顏色' –