forked from sightmachine/SimpleCV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorSegmentation.py
More file actions
59 lines (58 loc) · 2.02 KB
/
Copy pathColorSegmentation.py
File metadata and controls
59 lines (58 loc) · 2.02 KB
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
import time
from SimpleCV import *
from SimpleCV.Display import Display, pg
from SimpleCV.Segmentation import ColorSegmentation
segmentation = ColorSegmentation()
cam = Camera()
SegmentMode = False
x0 = 0
y0 = 0
x1 = 0
y1 = 0
img = cam.getImage()
display = Display((img.width,img.height))
mouse_down = False
while not display.isDone():
img = cam.getImage()
img = img.scale(160,120)
dl = DrawingLayer((img.width,img.height))
mystring = "( " +str(display.mouseX)+" , "+str(display.mouseY)+" )"
#print((display.mouseX,display.mouseY))
if SegmentMode:
segmentation.addImage(img)
if(segmentation.isReady()):
img = segmentation.getSegmentedImage()
img = img.erode(iterations = 2).dilate().invert()
img.dl().ezViewText(mystring, (30,30))
img = img.applyLayers()
display.writeFrame(img)
if(display.mouseLeft):
SegmentMode = False
segmentation.reset()
display.mouseLeft = False
else:
if(display.mouseLeft and not mouse_down):
#print( (display.mouseX,display.mouseY))
x0 = display.mouseX
y0 = display.mouseY
mouse_down = True
elif( display.mouseLeft and mouse_down ):
dl.circle((x0,y0),radius=10,color=Color.RED)
dl.rectangle2pts( (display.mouseX,display.mouseY),(x0,y0),color=Color.RED)
dl.circle((display.mouseX,display.mouseY),radius=10,color=Color.RED)
elif(mouse_down):
x = min(x0,display.mouseX)
y = min(y0,display.mouseY)
ww = max(x0,display.mouseX)-x
hh = max(y0,display.mouseY)-y
if( ww > 0 and hh > 0):
crop = img.crop(x,y,ww,hh)
segmentation.addToModel(crop)
SegmentMode = True
mouse_down = False
img.addDrawingLayer(dl)
img.dl().ezViewText(mystring, (30,30))
#img.save(display)
img = img.applyLayers();
display.writeFrame(img)
time.sleep(0.001)