Explorar o código

OutputImage extends Emgu.Image

Alexander Hendrich %!s(int64=11) %!d(string=hai) anos
pai
achega
9f7d09921e

+ 1 - 1
bbiwarg/Output/DebugOutput/DebugWindow.cs

@@ -135,7 +135,7 @@ namespace bbiwarg.Output.DebugOutput
                 int column = imageIndex % Parameters.DebugWindowNumImagesPerRow;
                 int row = imageIndex / Parameters.DebugWindowNumImagesPerRow;
                 GL.BindTexture(TextureTarget.Texture2D, textureID);
-                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, Parameters.ImageWidth, Parameters.ImageHeight, 0, PixelFormat.Rgb, PixelType.UnsignedByte, image.Image.MIplImage.imageData);
+                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, Parameters.ImageWidth, Parameters.ImageHeight, 0, PixelFormat.Rgb, PixelType.UnsignedByte, image.MIplImage.imageData);
                 GL.Begin(PrimitiveType.Quads);
                 GL.Color3(1.0, 1.0, 1.0);
                 GL.TexCoord2(0, 0); GL.Vertex3(0 + column, 0 + row, -1);

+ 50 - 14
bbiwarg/Output/GlassesOutput/GlassesWindow.cs

@@ -21,12 +21,25 @@ namespace bbiwarg.Output.GlassesOutput
         private uint textureID;
         private int currentFrameID;
         private OutputImage image;
+        private Projection2DTo2D projection;
+        private bool calibrationImageUpToDate;
+        private List<Vector2D> calibrationPoints;
+        private int calibrationPointIndex;
 
         public GlassesWindow(InputProvider inputProvider, InputHandler inputHandler)
             : base(Parameters.GlassesWindowWidth, Parameters.GlassesWindowHeight)
         {
             this.inputProvider = inputProvider;
             this.inputHandler = inputHandler;
+
+            projection = new Projection2DTo2D(Parameters.ImageWidth, Parameters.ImageHeight, Parameters.GlassesWindowWidth, Parameters.GlassesWindowHeight);
+            calibrationImageUpToDate = false;
+            calibrationPoints = new List<Vector2D>();
+            calibrationPoints.Add(new Vector2D(0.25f * Parameters.GlassesWindowWidth, 0.25f * Parameters.GlassesWindowHeight));
+            calibrationPoints.Add(new Vector2D(0.75f * Parameters.GlassesWindowWidth, 0.25f * Parameters.GlassesWindowHeight));
+            calibrationPoints.Add(new Vector2D(0.75f * Parameters.GlassesWindowWidth, 0.75f * Parameters.GlassesWindowHeight));
+            calibrationPoints.Add(new Vector2D(0.25f * Parameters.GlassesWindowWidth, 0.75f * Parameters.GlassesWindowHeight));
+            calibrationPointIndex = 0;
         }
 
         protected override void OnLoad(EventArgs e)
@@ -75,25 +88,29 @@ namespace bbiwarg.Output.GlassesOutput
             if (!inputProvider.IsActive)
                 Exit();
 
-            FrameData frameData = inputHandler.FrameData;
-            if (frameData != null)
+            if (projection.IsCalibrated)
             {
-                lock (frameData)
+                FrameData frameData = inputHandler.FrameData;
+                if (frameData != null)
                 {
-                    if (currentFrameID != frameData.FrameID)
+                    lock (frameData)
                     {
-                        currentFrameID = frameData.FrameID;
-
-                        Timer.start("GlassesWindow.OnUpdateFrame::updateImage");
-                        //TODO update image
-                        if (image != null)
-                            image.Dispose();
-                        image = new OutputImage(Parameters.GlassesWindowWidth, Parameters.GlassesWindowHeight);
-                        Timer.stop("GlassesWindow.OnUpdateFrame::updateImage");
+                        if (currentFrameID != frameData.FrameID)
+                        {
+                            currentFrameID = frameData.FrameID;
+
+                            Timer.start("GlassesWindow.OnUpdateFrame::updateImage");
+                            updateImage(frameData);
+                            Timer.stop("GlassesWindow.OnUpdateFrame::updateImage");
+                        }
                     }
                 }
             }
-
+            else {
+                if (!calibrationImageUpToDate) {
+                    updateCalibrationImage();
+                }
+            }
             Timer.stop("GlassesWindow.OnUpdateFrame");
         }
 
@@ -114,7 +131,7 @@ namespace bbiwarg.Output.GlassesOutput
             Timer.start("GlassesWindow.OnRenderFrame::drawImage");
             
             GL.BindTexture(TextureTarget.Texture2D, textureID);
-            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, Parameters.GlassesWindowWidth, Parameters.GlassesWindowHeight, 0, PixelFormat.Rgb, PixelType.UnsignedByte, image.Image.MIplImage.imageData);
+            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, Parameters.GlassesWindowWidth, Parameters.GlassesWindowHeight, 0, PixelFormat.Rgb, PixelType.UnsignedByte, image.MIplImage.imageData);
             GL.Begin(PrimitiveType.Quads);
             GL.Color3(1.0, 1.0, 1.0);
             GL.TexCoord2(0, 0); GL.Vertex3(0, 0, -1);
@@ -137,5 +154,24 @@ namespace bbiwarg.Output.GlassesOutput
 
         }
 
+        private void updateImage(FrameData frameData) {
+            if (image != null)
+                image.Dispose();
+            image = new OutputImage(Parameters.GlassesWindowWidth, Parameters.GlassesWindowHeight);
+                            
+        }
+
+        private void updateCalibrationImage() {
+            //if (image != null)
+            //    image.Dispose();
+
+            image = new OutputImage(Parameters.GlassesWindowWidth, Parameters.GlassesWindowHeight);
+
+            Vector2D calibrationPoint = calibrationPoints[calibrationPointIndex];
+            image.fillCircle(calibrationPoint.IntX, calibrationPoint.IntY, 10, Parameters.CalibrationPointColor);
+
+            calibrationImageUpToDate = true;
+        }
+
     }
 }

+ 39 - 45
bbiwarg/Output/OutputImage.cs

@@ -11,64 +11,63 @@ using Emgu.CV.Structure;
 
 namespace bbiwarg.Output
 {
-    class OutputImage : IDisposable
+    class OutputImage : Image<Rgb,byte>
     {
-        public Image<Rgb, byte> Image { get; private set; }
-
         public OutputImage(int width, int height)
+            :base(width,height)
         {
-            Image = new Image<Rgb, byte>(width, height);
         }
 
+
         public Color getColotAt(int x, int y)
         {
 
-            byte red = Image.Data[y, x, 0];
-            byte green = Image.Data[y, x, 1];
-            byte blue = Image.Data[y, x, 2];
+            byte red = Data[y, x, 0];
+            byte green = Data[y, x, 1];
+            byte blue = Data[y, x, 2];
             return Color.FromArgb(red, green, blue);
         }
 
         public void drawLineSegment(bbiwarg.Utility.LineSegment2D lineSegment, Color color, int thickness = 1)
         {
-            Image.Draw(new Emgu.CV.Structure.LineSegment2D(lineSegment.P1, lineSegment.P2), new Rgb(color), thickness);
+            Draw(new Emgu.CV.Structure.LineSegment2D(lineSegment.P1, lineSegment.P2), new Rgb(color), thickness);
         }
 
         public void drawContour(Contour<Point> contour, Color color, int thickness = 1)
         {
-            Image.Draw(contour, new Rgb(color), thickness);
+            Draw(contour, new Rgb(color), thickness);
         }
 
         public void drawPoints(Seq<Point> points, Color color, int thickness = 1)
         {
-            Image.Draw(points, new Rgb(color), thickness);
+            Draw(points, new Rgb(color), thickness);
         }
 
         public void drawPixel(int x, int y, Color color)
         {
-            Image.Data[y, x, 0] = color.R;
-            Image.Data[y, x, 1] = color.G;
-            Image.Data[y, x, 2] = color.B;
+            Data[y, x, 0] = color.R;
+            Data[y, x, 1] = color.G;
+            Data[y, x, 2] = color.B;
         }
 
         public void fillCircle(int x, int y, float radius, Color color)
         {
-            Image.Draw(new CircleF(new PointF(x, y), radius), new Rgb(color), 0);
+            Draw(new CircleF(new PointF(x, y), radius), new Rgb(color), 0);
         }
 
         public void fillRectangle(int x, int y, int width, int height, Color color) {
-            Image.Draw(new Rectangle(x, y, width, height), new Rgb(color),-1);
+            Draw(new Rectangle(x, y, width, height), new Rgb(color),-1);
         }
 
         public void drawRectangle(int x, int y, int width, int height, Color color, int thichness = 0)
         {
-            Image.Draw(new Rectangle(x, y, width, height), new Rgb(color), thichness);
+            Draw(new Rectangle(x, y, width, height), new Rgb(color), thichness);
         }
 
         public void drawText(int x, int y, String text, Color color)
         {
             MCvFont font = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_PLAIN, 1, 1);
-            Image.Draw(text, ref font, new Point(x, y), new Rgb(color));
+            Draw(text, ref font, new Point(x, y), new Rgb(color));
         }
 
         public void drawDefect(ConvexityDefect defect, Color pointColor, Color lineColor)
@@ -82,31 +81,6 @@ namespace bbiwarg.Output
 
         }
 
-        public void drawImage(Image<Gray, byte> image, Color color)
-        {
-            if (color.R != 0)
-            {
-                if (color.R != byte.MaxValue)
-                    Image[0] = Image[0].Or(image.Mul((float)color.R / byte.MaxValue));
-                else
-                    Image[0] = Image[0].Or(image);
-            }
-            if (color.G != 0)
-            {
-                if (color.G != byte.MaxValue)
-                    Image[1] = Image[1].Or(image.Mul((float)color.G / byte.MaxValue));
-                else
-                    Image[1] = Image[1].Or(image);
-            }
-            if (color.B != 0)
-            {
-                if (color.B != byte.MaxValue)
-                    Image[2] = Image[2].Or(image.Mul((float)color.B / byte.MaxValue));
-                else
-                    Image[2] = Image[2].Or(image);
-            }
-        }
-
         public void drawQuadrangleGrid(Quadrangle quad, Color borderColor, Color gridColor, int numRows, int numCols)
         {
 
@@ -137,7 +111,7 @@ namespace bbiwarg.Output
         }
 
         public void drawBorder(Color color) {
-            drawRectangle(0, 0, Image.Width-1, Image.Height-1, color);
+            drawRectangle(0, 0, Width-1, Height-1, color);
         }
 
         public void drawTouchGesture(List<Vector2D> positions, float opacity=1)
@@ -156,9 +130,29 @@ namespace bbiwarg.Output
             fillCircle(lastPos.IntX, lastPos.IntY, 3, pointColorFaded);
         }
 
-        public void Dispose()
+        public void drawImage(Image<Gray, byte> image, Color color)
         {
-            Image.Dispose();
+            if (color.R != 0)
+            {
+                if (color.R != byte.MaxValue)
+                    this[0] = this[0].Or(image.Mul((float)color.R / byte.MaxValue));
+                else
+                    this[0] = this[0].Or(image);
+            }
+            if (color.G != 0)
+            {
+                if (color.G != byte.MaxValue)
+                    this[1] = this[1].Or(image.Mul((float)color.G / byte.MaxValue));
+                else
+                    this[1] = this[1].Or(image);
+            }
+            if (color.B != 0)
+            {
+                if (color.B != byte.MaxValue)
+                    this[2] = this[2].Or(image.Mul((float)color.B / byte.MaxValue));
+                else
+                    this[2] = this[2].Or(image);
+            }
         }
     }
 }