-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
66 lines (45 loc) · 1.61 KB
/
main.go
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
package main
import (
"gocv.io/x/gocv"
"time"
)
func main() {
webcam, _ := gocv.VideoCaptureDevice(0)
window := gocv.NewWindow("Invisibility Cloak")
curImg := gocv.NewMatWithSize(5,5, gocv.MatTypeCV8U)
background := gocv.NewMatWithSize(5,5, gocv.MatTypeCV8U)
hsvImg := gocv.NewMatWithSize(5,5, gocv.MatTypeCV8U)
// this is the mask where we do all the morphing (the magic)
morphMask := gocv.NewMat()
// this is the mask where will keep all the inverted image data
clearMask := gocv.NewMat()
// this will be our final frame
processedFrame := gocv.NewMatWithSize(5,5, gocv.MatTypeCV8U)
// hsv values for green
lGreenMat := gocv.NewScalar(40,0,0, 0)
uGreenMat := gocv.NewScalar(95,255,255, 0)
defer func() {
curImg.Close()
background.Close()
}()
time.Sleep(5 * time.Second)
webcam.Read(&background)
gocv.Flip(background, &background, 1)
for {
webcam.Read(&curImg)
gocv.Flip(curImg, &curImg, 1)
gocv.CvtColor(curImg, &hsvImg, gocv.ColorBGRToHSV)
gocv.InRangeWithScalar(hsvImg, lGreenMat, uGreenMat, &morphMask)
kernal := gocv.NewMatWithSize(5,5, gocv.MatTypeCV8U)
gocv.MorphologyEx(morphMask, &morphMask, gocv.MorphDilate, kernal)
gocv.BitwiseNot(morphMask, &clearMask)
imgSize := curImg.Size()
imgRes := gocv.NewMatWithSize(imgSize[0], imgSize[1], gocv.MatTypeCV8U)
backgroundRes := gocv.NewMatWithSize(background.Size()[0], background.Size()[1], gocv.MatTypeCV8U)
curImg.CopyToWithMask(&imgRes, clearMask)
background.CopyToWithMask(&backgroundRes, morphMask)
gocv.AddWeighted(imgRes, 1, backgroundRes, 1, 0, &processedFrame)
window.IMShow(processedFrame)
window.WaitKey(1)
}
}