-
Notifications
You must be signed in to change notification settings - Fork 0
/
Webcam.cs
74 lines (66 loc) · 1.75 KB
/
Webcam.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Emgu.CV;
using Emgu.CV.Structure;
using System.Windows.Forms;
namespace SSD2
{
class WebCam
{
public Boolean connected = false;
private Capture webcam;
public void Init()
{
if (!scanCams())
{
MessageBox.Show("No camera found (or too dark)");
Application.Exit();
}
}
private bool namedCam(int camIndex)
{
webcam = new Capture(camIndex);
return (AvrLum(webcam) >= 10);
}
private bool scanCams()
{
int i = 0;
bool found = false;
while (i < 10)
{
webcam = new Capture(i);
if (AvrLum(webcam) >= 10)
{
Console.WriteLine(i.ToString());
found = true;
break;
}
i++;
}
return found;
}
private static double AvrLum(Capture webcam)
{
Image<Bgr, Byte> testFrame = webcam.QueryFrame();
Image<Bgr, Byte> cloneFrame = testFrame.Clone();
int sum = new int();
foreach (int lum in cloneFrame.Data)
{
sum += lum;
}
Double avr = sum / (cloneFrame.Height * cloneFrame.Width * 3);
return avr;
}
public Image<Bgr, Byte> grab()
{
Image<Bgr, Byte> frame = webcam.QueryFrame();
return frame.Clone(); // return clone to populate .Data[,,]
}
~WebCam()
{
if (webcam != null) webcam.Dispose();
}
}
}