I'm completely new to Python, but what I would like to do is to draw a region in an image and then analyze it. This shall be done inside a GUI. My program can now draw a region with Lasso Selector (thanks to http://matplotlib.org/examples/event_handling/lasso_demo.html) and the vertices are saved in a numpy array. I would like the whole region to be saved as an array (or matrix). Is there an built-in function do to this? Do I need to make a foor-loop and if-statements and loop through all elements in the image and check which element is inside and which one is not. If that's the case, I don't know how to, since the elements from the vertice-array are not integers and unpaired (that is for row 45 there isn't exactly one vertice in column 3 and one in column 17). I use a mix of tkinter, matplotlib, numpy and what not.. (There are ofcourse tutorials on how to do this for a rectangle or so, I cannot apply that to this region.)
In short: What I would like is a numpy array with all the elements and their pixel values inside the region that I've drawn. The program shall be able to change that region and then replace it in the old image.
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()
In the example in the link given (http://matplotlib.org/examples/event_handling/lasso_demo.html) I suspect that the following lines does the trick, but I don't understand that with include = False. Also in my program I don't have such a good structure since I don't understand all with classes, objects, etc at the moment. This is more of a image-analysis project.
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
Thank you for any input in this problem!