123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Emgu.CV;
- using Emgu.CV.Structure;
- using bbiwarg.Utility;
- using bbiwarg.Detectors.Fingers;
- namespace bbiwarg.Images
- {
- public enum FingerImageState {
- none = 0,
- possibleFinger = 1,
- fingerDetected = 2,
- fingerTracked = 3
- }
- class FingerImage
- {
- private Image<Gray, byte> image;
- public FingerImage(int width, int height) {
- image = new Image<Gray, byte>(width, height);
- }
- public void setFingerAt(int x, int y, FingerImageState fis) {
- image.Data[y, x, 0] = (byte) fis;
- }
- public FingerImageState getStateAt(int x, int y) {
- return (FingerImageState)image.Data[y, x, 0];
- }
- public void drawLine(Line2D line, FingerImageState state)
- {
- Vector2D start = line.P1;
- Vector2D end = line.P2;
- int width = image.Width;
- int height = image.Height;
- // bresenham from wikipedia
- int xstart = (int)start.X, xend = (int)end.X, ystart = (int)start.Y, yend = (int)end.Y;
- int x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, es, el, err;
- /* Entfernung in beiden Dimensionen berechnen */
- dx = xend - xstart;
- dy = yend - ystart;
- /* Vorzeichen des Inkrements bestimmen */
- incx = dx < 0 ? -1 : 1;
- incy = dy < 0 ? -1 : 1;
- if (dx < 0) dx = -dx;
- if (dy < 0) dy = -dy;
- /* feststellen, welche Entfernung größer ist */
- if (dx > dy)
- {
- /* x ist schnelle Richtung */
- pdx = incx; pdy = 0; /* pd. ist Parallelschritt */
- ddx = incx; ddy = incy; /* dd. ist Diagonalschritt */
- es = dy; el = dx; /* Fehlerschritte schnell, langsam */
- }
- else
- {
- /* y ist schnelle Richtung */
- pdx = 0; pdy = incy; /* pd. ist Parallelschritt */
- ddx = incx; ddy = incy; /* dd. ist Diagonalschritt */
- es = dx; el = dy; /* Fehlerschritte schnell, langsam */
- }
- /* Initialisierungen vor Schleifenbeginn */
- x = xstart;
- y = ystart;
- err = el / 2;
- setFingerAt(Math.Min(width - 1, Math.Max(0, x)), Math.Min(height - 1, Math.Max(0, y)), state);
- /* Pixel berechnen */
- for (t = 0; t < el; ++t) /* t zaehlt die Pixel, el ist auch Anzahl */
- {
- /* Aktualisierung Fehlerterm */
- err -= es;
- if (err < 0)
- {
- /* Fehlerterm wieder positiv (>=0) machen */
- err += el;
- /* Schritt in langsame Richtung, Diagonalschritt */
- x += ddx;
- y += ddy;
- }
- else
- {
- /* Schritt in schnelle Richtung, Parallelschritt */
- x += pdx;
- y += pdy;
- }
- setFingerAt(Math.Min(width - 1, Math.Max(0, x)), Math.Min(height - 1, Math.Max(0, y)), state);
- }
- }
- }
- }
|