| 11 | from imagepy import IPy |
| 12 | |
| 13 | class RectangleRoi(ROI): |
| 14 | dtype = 'rect' |
| 15 | def __init__(self, l=0, t=0, r=0, b=0): |
| 16 | self.body = [] |
| 17 | self.dirty = False |
| 18 | self.lt, self.tp, self.rt, self.bm = l, t, r, b |
| 19 | self.commit() |
| 20 | |
| 21 | def snap(self, x, y, z,lim): |
| 22 | if abs(x-self.lt)<lim and abs(y-(self.tp+self.bm)/2)<lim:return 'l' |
| 23 | if abs(x-self.rt)<lim and abs(y-(self.tp+self.bm)/2)<lim:return 'r' |
| 24 | if abs(x-(self.lt+self.rt)/2)<lim and abs(y-self.tp)<lim:return 't' |
| 25 | if abs(x-(self.lt+self.rt)/2)<lim and abs(y-self.bm)<lim:return 'b' |
| 26 | if abs(x-self.lt)<lim and abs(y-self.tp)<lim:return 'lt' |
| 27 | if abs(x-self.rt)<lim and abs(y-self.bm)<lim:return 'rb' |
| 28 | if abs(x-self.rt)<lim and abs(y-self.tp)<lim:return 'rt' |
| 29 | if abs(x-self.lt)<lim and abs(y-self.bm)<lim:return 'lb' |
| 30 | return None |
| 31 | |
| 32 | def update(self): self.dirty = True |
| 33 | |
| 34 | def commit(self): |
| 35 | box = self.lt, self.rt, self.tp, self.bm |
| 36 | self.lt, self.rt, self.tp, self.bm = [int(round(i)) for i in box] |
| 37 | l,r,t,b = self.lt, self.rt, self.tp, self.bm |
| 38 | |
| 39 | self.update() |
| 40 | if l==r or t==b: |
| 41 | self.body = [];return False |
| 42 | else: |
| 43 | self.body = [(l,b),(r,b),(r,t),(l,t),(l,b)] |
| 44 | return True |
| 45 | |
| 46 | def info(self, ips, cur): |
| 47 | k, u = ips.unit |
| 48 | l,r,t,b = self.lt, self.rt, self.tp, self.bm |
| 49 | IPy.set_info('Rectangle : x:%.1f y:%.1f w:%.1f h:%.1f S:%.1f'%( |
| 50 | min(l,r)*k,min(t,b)*k,abs(r-l)*k,abs(b-t)*k,abs((r-l)*(b-t)*k**2))) |
| 51 | |
| 52 | def pick(self, x, y, z, lim): |
| 53 | rst = self.snap(x, y, z,lim) |
| 54 | if rst != None:return rst |
| 55 | if (x-self.lt)*(x-self.rt)<0 and (y-self.tp)*(y-self.bm)<0: |
| 56 | return True |
| 57 | return None |
| 58 | |
| 59 | def draged(self, ox, oy, nx, ny, nz, i): |
| 60 | if i == True: |
| 61 | self.lt, self.rt = self.lt+nx-ox, self.rt+nx-ox |
| 62 | self.tp, self.bm = self.tp+ny-oy, self.bm+ny-oy |
| 63 | else: |
| 64 | if 'l' in i:self.lt = nx |
| 65 | if 'r' in i:self.rt = nx |
| 66 | if 't' in i:self.tp = ny |
| 67 | if 'b' in i:self.bm = ny |
| 68 | self.commit() |
| 69 | |
| 70 | def get_box(self): |