Python 如何使用套索选择器绘制区域数组?
我对Python完全陌生,但我想做的是在图像中绘制一个区域,然后对其进行分析。这应在GUI内完成。我的程序现在可以用套索选择器绘制一个区域(多亏了),顶点保存在numpy数组中。我希望整个区域保存为数组(或矩阵)。是否有一个内置的函数来执行此操作?我是否需要做一个foor循环和if语句,并通过图像中的所有元素循环,检查哪个元素在里面,哪个不在里面。如果是这样的话,我不知道怎么做,因为vertice数组中的元素不是整数,也不是不成对的(也就是说,对于第45行,第3列和第17列中没有一个vertice)。我混合使用了tkinter、matplotlib、numpy等等。。(当然,有一些教程介绍如何对矩形或其他矩形执行此操作,我无法将其应用于此区域。) 简而言之:我想要的是一个numpy数组,所有元素及其像素值都在我绘制的区域内。程序应能够改变该区域,然后在旧图像中替换该区域Python 如何使用套索选择器绘制区域数组?,python,user-interface,roi,Python,User Interface,Roi,我对Python完全陌生,但我想做的是在图像中绘制一个区域,然后对其进行分析。这应在GUI内完成。我的程序现在可以用套索选择器绘制一个区域(多亏了),顶点保存在numpy数组中。我希望整个区域保存为数组(或矩阵)。是否有一个内置的函数来执行此操作?我是否需要做一个foor循环和if语句,并通过图像中的所有元素循环,检查哪个元素在里面,哪个不在里面。如果是这样的话,我不知道怎么做,因为vertice数组中的元素不是整数,也不是不成对的(也就是说,对于第45行,第3列和第17列中没有一个vertic
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()
在给出的链接()中的示例中,我怀疑下面的几行会起作用,但我不理解include=False。另外,在我的程序中,我没有这么好的结构,因为我不理解所有的类、对象等。这更像是一个图像分析项目
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
感谢您对此问题的任何意见 我使用脚本的某些部分提出了以下解决方案:
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()
忘记添加,p.contains_points(v)只给我一个带真/假的向量如果图像元素在直线上,我想要一个在封闭区域内给我所有点的向量。如果运行该程序,则需要单击“打开图像”并选择一个,以便正确地绘制图像。