-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program_Old_Nov04.cs
194 lines (149 loc) · 6.6 KB
/
Program_Old_Nov04.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using System.Drawing;
namespace ShaprCVTest
{
class MainClass
{
//Set to TRUE to display debugging messages on console
static bool DebugMode = true;
/*
public static void Main()
{
Mat frame = CvInvoke.Imread( "..\\..\\Images\\Cups.jpg", LoadImageType.AnyColor );
CvInvoke.Imshow( "DetectCups", DetectCups( frame ) );
CvInvoke.WaitKey( 0 );
CvInvoke.DestroyAllWindows();
//SimpleVideoFeed ();
}
*/
public static Image<Bgr, byte> DetectCups(Mat input_image)
{
Size size = new Size( 700, 700 );
//Load image
Image<Gray, byte> preprocessed_image = new Image<Gray, byte>( size );
//denoise, smoothe and threshold
Preprocess( input_image, preprocessed_image, size );
Image<Bgr, byte> output_image = new Image<Bgr, byte>( input_image.Size );
output_image = input_image.ToImage<Bgr, byte>();
CvInvoke.Resize( output_image, output_image, size );
DrawContours( output_image, GetContours( preprocessed_image ) );
return output_image;
}
private static void CheckMouseClicks()
{
System.Windows.Forms.MouseButtons Click = System.Windows.Forms.Control.MouseButtons;
if ( Click != System.Windows.Forms.MouseButtons.None )
{
if (DebugMode) Console.WriteLine( "CheckMouseClicks(): Button Pressed = " + Click );
}
}
private static void SimpleVideoFeed ()
{
// Initialize video capture from camera and check if it worked. If not, use a video file.
Capture vidCap = new Capture ( 0 );
if ( vidCap.Grab())
{
if (DebugMode) Console.WriteLine( "SimpleVideoFeed(): Successfully opened a camera." );
// Some webcams return a strange image the first time.
// So we just read one frame and ignore it.
vidCap.Retrieve( new Mat() );
}
else
{
if (DebugMode) Console.WriteLine( "SimpleVideoFeed(): Could not open camera!" );
return;
}
// Just for fun, output the video frame size.
if (DebugMode) Console.WriteLine( "SimpleVideoFeed(): Video frame size is [ " + vidCap.Width + " x " + vidCap.Height + "] pixels." );
// Create a window and give it a name.
string wiName = "This is a Video";
CvInvoke.NamedWindow( wiName );
// This matrix will contain our image.
Mat frame = new Mat();
//The Main Loop: Instead of while(true)
for ( int TimeOut = 0; TimeOut < 10000; TimeOut++ )
{
// Read a video frame into our image.
// If we get an empty frame, we abort because have reached the end of the video stream.
vidCap.Retrieve( frame );
if ( frame.IsEmpty ) break;
// Make sure the image is a 3-channel 24-bit image.
if ( !( frame.Depth == DepthType.Cv8U ) && frame.NumberOfChannels == 3 )
{
if (DebugMode) Console.WriteLine( "SimpleVideoFeed(): Unexpected image format!" );
if (DebugMode) Console.WriteLine( "SimpleVideoFeed(): Type [" + frame.GetType().ToString() + "] and Channels [" + frame.NumberOfChannels + "]" );
return;
}
// Apply a 5x5 median filter.
//CvInvoke.MedianBlur( frame, frame, 5 );
// Display a text.
//CvInvoke.PutText( frame, "Click somewhere!", new System.Drawing.Point( 50, 50 ), FontFace.HersheyPlain, 1.5, new MCvScalar( 255, 0, 255 ), 2 );
// Show the image in the window.
CvInvoke.Imshow( wiName, frame );
// Quit the loop when the Esc key is pressed.
// Calling waitKey is important, even if you're not interested in keyboard input!
int keyPressed = CvInvoke.WaitKey( 1 );
CheckMouseClicks ();
if ( keyPressed != -1 && keyPressed != 255 )
{
// Only the least-significant 16 bits contain the actual key code. The other bits contain modifier key states.
keyPressed &= 0xFFFF;
if (DebugMode) Console.WriteLine( "SimpleVideoFeed(): Key pressed: " + keyPressed );
if ( keyPressed == 27 ) break;
}
}
if (DebugMode) Console.WriteLine( "SimpleVideoFeed(): Ended Video Function." );
}
private static VectorOfVectorOfPoint GetContours( Image<Gray, byte> input ) {
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
Image<Gray, float> laplace_image = input.Laplace( 3 );
Image<Gray, float> erode_image = laplace_image.Erode( 2 );
Image<Gray, byte> byteErode_image = erode_image.Convert<Gray, byte>();
Image<Gray, byte> thresholded_image = byteErode_image.ThresholdToZero( new Gray( 240 ) );
Image<Gray, byte> erode2_image = thresholded_image.Erode( 3 );
int[,] tree = CvInvoke.FindContourTree( erode2_image, contours, ChainApproxMethod.ChainApproxSimple );
for ( int n = 0; n < tree.Length / 4; n++ ) {
for ( int m = 0; m < 4; m++ ) {
Console.Write( tree[n, m] + ", " );
}
Console.WriteLine();
}
return contours;
}
private static void DrawContours( Image<Bgr, byte> output, VectorOfVectorOfPoint contours ) {
Bgr bgrRed = new Bgr( Color.Red );
for ( int i = 0; i < contours.Size; i++ ) {
Rectangle box = CvInvoke.BoundingRectangle( contours[i] );
if ( ( box.Width < 400 && box.Height < 400 ) &&
( box.Width > 50 && box.Height > 50 ) &&
( box.Height > box.Width )
) {
output.Draw( box, bgrRed, 2 );
}
}
}
private static void Preprocess( Mat input, Image<Gray, byte> output, Size size ) {
//Resize image
Image<Bgr, byte> resized_image = new Image<Bgr, byte>( size );
CvInvoke.Resize( input, resized_image, size );
CvInvoke.FastNlMeansDenoisingColored( resized_image, resized_image, 3, 3, 7, 21 );
resized_image = resized_image.SmoothMedian( 15 );
resized_image._GammaCorrect( 2d );
resized_image._EqualizeHist();
Image<Hsv, byte> hsv_image = new Image<Hsv, byte>( size );
CvInvoke.CvtColor( resized_image, hsv_image, ColorConversion.Bgr2Hsv );
CvInvoke.Imshow( "hsv", hsv_image );
Image<Gray, byte> gray_image = new Image<Gray, byte>( size );
CvInvoke.CvtColor( resized_image, gray_image, ColorConversion.Bgr2Gray );
CvInvoke.Imshow( "gray", gray_image );
ScalarArray lower = new ScalarArray( new Hsv( 0, 0, 0 ).MCvScalar );
ScalarArray upper = new ScalarArray( new Hsv( 35, 255, 255 ).MCvScalar );
CvInvoke.InRange( hsv_image, lower, upper, output );
//CvInvoke.AdaptiveThreshold( gray_image, output, 255, AdaptiveThresholdType.GaussianC, ThresholdType.Binary, 15, 4 );
}
}
}