Files
@ 928d9a0edc05
Branch filter:
Location: OneEye/src/gui/mainwindow.py
928d9a0edc05
3.7 KiB
text/x-python
fix of coordinates computation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | import logging as log
import tkinter as tk
import PIL
from PIL import ImageTk
import config
import imageanalyzer
from corners import Corners
from epoint import EPoint
from grid import Grid
from .boardview import BoardView
class MainWindow(tk.Frame):
def __init__(self,parent,master=None):
self.parent=parent
self.corners=Corners()
self.currentFrame=None
self._boardGrid=None
self.img=None
self._imgSizeCoef=1
self._imgShift=EPoint(0,0)
tk.Frame.__init__(self, master)
self.grid(column=0,row=0)
self._createWidgets()
def setCurrentFrame(self,frame):
self.currentFrame=frame
w=int(self.imgView['width'])
h=int(self.imgView['height'])
wo,ho=frame.size # o for original
widthRatio=wo/w
heightRatio=ho/h
self._imgSizeCoef=max(widthRatio,heightRatio)
# shift compensates possible horizontal or vertical empty margins from unmatching aspect ratios
self._imgShift=EPoint(wo-w*self._imgSizeCoef,ho-h*self._imgSizeCoef)/2
frame.thumbnail((w,h)) # resize
self.img=ImageTk.PhotoImage(frame)
def _createWidgets(self):
# a captured frame with overlay graphics
self.imgView=tk.Canvas(self)
self.imgView.configure(width=480,height=360)
self.imgView.bind('<1>',lambda e: self.addCorner(e.x,e.y))
self.imgView.grid(column=0,row=0)
# board with detected stones
self.boardView= BoardView(self)
self.boardView.grid(column=1,row=0)
# more controls below the board
self.scaleTresB=tk.Scale(self, orient=tk.HORIZONTAL, length=200, from_=0.0, to=100.0, command=self.refreshTresholds)
self.scaleTresW=tk.Scale(self, orient=tk.HORIZONTAL, length=200, from_=0.0, to=100.0, command=self.refreshTresholds)
self.scaleTresB.set(30.0) # !! proper defaults
self.scaleTresW.set(60.0)
self.scaleTresB.grid(column=0,row=1,columnspan=2)
self.scaleTresW.grid(column=0,row=2,columnspan=2)
# render everything
self.redrawImgView()
## Stores a grid corner located at x,y coordinates.
def addCorner(self,x,y):
self.corners.add(x,y)
log.debug("click on %d,%d",x,y)
log.debug("sizeCoef: %f, shift: %d,%d",self._imgSizeCoef,self._imgShift.x,self._imgShift.y)
if self.corners.canonizeOrder():
# transform corners from show coordinates to real coordinates
log.debug(self.corners.corners)
self._boardGrid=Grid(self.corners.corners)
corners=[(c*self._imgSizeCoef+self._imgShift) for c in self.corners.corners]
self.parent.sendMsg("setCorners",(corners,))
self.redrawImgView()
## Redraws the current image and its overlay.
def redrawImgView(self):
if self.currentFrame and self.img:
self.imgView.create_image(240,180,anchor="center",image=self.img)
for corner in self.corners.corners:
self.markPoint(corner.x,corner.y)
if self._boardGrid!=None and config.gui.showGrid:
for r in range(19):
a=self._boardGrid.intersections[r][0]
b=self._boardGrid.intersections[r][-1]
self.imgView.create_line(a.x,a.y,b.x,b.y,fill='#00ff00')
for c in range(19):
a=self._boardGrid.intersections[0][c]
b=self._boardGrid.intersections[-1][c]
self.imgView.create_line(a.x,a.y,b.x,b.y,fill='#00ff00')
if self._boardGrid!=None and config.gui.showBigPoints:
for r in range(19):
for c in range(19):
((r1,c1),(r2,c2))=imageanalyzer.relevantRect(self._boardGrid.intersections[r][c], *(self._boardGrid.stoneSizeAt(r, c)))
self.imgView.create_rectangle(r1,c1,r2,c2,outline="#00ffff")
self.imgView.grid()
## Marks a point at the image with a green cross. Used for corners.
def markPoint(self,x,y):
self.imgView.create_line(x-3,y-3,x+4,y+4,fill="#00ff00")
self.imgView.create_line(x-3,y+3,x+4,y-4,fill="#00ff00")
self.imgView.grid()
def refreshTresholds(self,_):
self.parent.sendMsg("setTresholds",tuple(),{"tresB":self.scaleTresB.get(), "tresW":self.scaleTresW.get()})
|