2015-04-17 50 views
2

我對Python完全陌生,但我想要做的是在圖像中繪製一個區域,然後對其進行分析。這應該在GUI內完成。我的程序現在可以用Lasso Selector繪製一個區域(感謝http://matplotlib.org/examples/event_handling/lasso_demo.html)並且頂點保存在一個numpy數組中。我希望將整個區域保存爲一個數組(或矩陣)。有沒有一個內置的功能呢?我是否需要製作foor-loop和if-statements並遍歷圖像中的所有元素,並檢查哪個元素在裏面,哪個不在。如果是這種情況,我不知道該怎麼做,因爲vertice-array中的元素不是整數和不成對的(對於第45行,第3列和第17列中並不只有一個頂點)。我用tkinter,matplotlib,numpy和什麼不是..(有關於如何做到這一點的矩形左右,我不能適用於這個地區)的組合教程。像是一個numpy數組,其中包含我繪製區域內的所有元素和它們的像素值。該程序應能夠改變該區域,然後將其替換爲舊圖像。如何獲得用套索選擇器繪製的區域數組?

from tkinter import * 

from matplotlib.widgets import LassoSelector 
import matplotlib.image as mpimg 
from pylab import * 
from matplotlib import path 
from tkinter.filedialog import askopenfilename 
import tkinter as tk 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 

global fig, v 
fname = "bild3.png" #Starting with file 

def onselect(verts): 
    global v, length_y 
    print(verts) 
    p = path.Path(verts) #path array of verts 
    print(v) 
    ind = p.contains_points(v) 

root = tk.Tk() 

# Creating two frames 
topFrame = tk.Frame() 
bottomFrame = tk.Frame() 
topFrame.pack(side="top") 
bottomFrame.pack(side="bottom") 

# Create buttons 
button_open = tk.Button(topFrame, text="Open image", command=lambda: selectIm()) 
button_draw = tk.Button(topFrame, text="Draw ROI") 
button_quit = tk.Button(topFrame, text="Quit", command=root.quit) 
button_clear= tk.Button(topFrame, text="Clear") 
button_save = tk.Button(topFrame, text="Save") 

# Place buttons 
button_open.pack(side="left") 
button_draw.pack(side="left") 
button_quit.pack(side="right") #fill makes it fill the frame 
button_save.pack(side="left") 
button_clear.pack(side = "left") 


# Set figure on canvas 
fig = plt.figure() #displays image and creates figure object?? 
img = mpimg.imread(fname) #ndarray [rows [cols [rgb 3 val]]] 

plt.imshow(img) #also needed to display image 
ax = plt.gca() #Axlarna hänger med .. 

canvas = FigureCanvasTkAgg(fig, bottomFrame) 
canvas.show() 
canvas.get_tk_widget().pack(side='top', fill='both', expand=1) 
lasso = LassoSelector(ax, onselect) #Should be in draw-function 


def selectIm(): 
    # global fig, 
    global v, length_y 
    print("Hej") 

    fname=askopenfilename() 
    img = mpimg.imread(fname) 
    plt.imshow(img) #does this overwrite or plot another one? 
    canvas.show() #updates canvas, or overwrites? 

    length_y=img.shape[0] #Number of rows 
    length_x=img.shape[1] #Number of cols 
    v = np.zeros((length_x*length_y, 2), dtype=np.int) #Creates zero array 

    #For y 
    a = np.array(range(0,length_x)) 
    y = np.tile(a, length_y) 

    #For x 
    b = np.array(range(0,length_y)) 
    x = np.repeat(b, length_x) 

    v[:,0] = x 
    v[:,1] = y 


root.title("Ett nytt test") 
root.mainloop() 

在給定的(http://matplotlib.org/examples/event_handling/lasso_demo.html)的鏈接我懷疑是以下行的伎倆,但我不明白,與包括=假的例子。同樣在我的程序中,我沒有這麼好的結構,因爲我目前不瞭解所有的類,對象等。這更像是一個圖像分析項目。

class Datum(object): 
    colorin = colorConverter.to_rgba('red') 
    colorout = colorConverter.to_rgba('blue') 
    def __init__(self, x, y, include=False): 
     self.x = x 
     self.y = y 
     if include: self.color = self.colorin 
     else: self.color = self.colorout 

謝謝你對這個問題的任何輸入!

+0

忘了補充,p.contains_points(V)只是給了我一個載體真/假,如果圖像元素是在線,我想要一個,如果在封閉的區域內給我所有的點。如果您運行該程序,則需要單擊打開圖像並選擇一個,才能正確繪製該圖像。 – Mianen

回答

3

我想出了使用腳本的某些部分以下解決方案:

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.widgets import LassoSelector 
from matplotlib import path 

fig = plt.figure() 
ax1 = fig.add_subplot(121) 
ax1.set_title('lasso selection:') 
ax1.plot() 
ax1.set_xlim([0, 10]) 
ax1.set_ylim([0, 10]) 
ax1.set_aspect('equal') 

# Empty array to be filled with lasso selector 
array = np.zeros((10,10)) 
ax2 = fig.add_subplot(122) 
ax2.set_title('numpy array:') 
msk = ax2.imshow(array, origin='lower',vmax=1, interpolation='nearest') 
ax2.set_xlim([-1, 10]) 
ax2.set_ylim([-1, 10]) 

# Pixel coordinates 
pix = np.arange(10) 
xv, yv = np.meshgrid(pix,pix) 
pix = np.vstack((xv.flatten(), yv.flatten())).T 

def updateArray(array, indices): 
    lin = np.arange(array.size) 
    newArray = array.flatten() 
    newArray[lin[indices]] = 1 
    return newArray.reshape(array.shape) 

def onselect(verts): 
    global array, pix 
    p = path.Path(verts) 
    ind = p.contains_points(pix, radius=1) 
    array = updateArray(array, ind) 
    msk.set_data(array) 
    fig.canvas.draw_idle() 

lasso = LassoSelector(ax1, onselect) 

plt.show() 

Output figure

相關問題