Click here to Skip to main content
15,886,788 members
Articles / Desktop Programming / Windows Forms

Computer Vision - Decoding a Morse Code Flashing LED

Rate me:
Please Sign up or sign in to vote.
4.94/5 (66 votes)
4 Dec 2009CPOL5 min read 216.1K   19.7K   126  
Using webcam and image processing to decode a Morse code flashing LED.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
public unsafe class UnsafeBitmap
{

    Bitmap bitmap;



    int width;

    BitmapData bitmapData = null;

    Byte* pBase = null;



    public UnsafeBitmap(Bitmap bitmap)
    {

        this.bitmap = new Bitmap(bitmap);
    }

    public UnsafeBitmap(int width, int height)
    {

        this.bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);

    }



    public void Dispose()
    {

        bitmap.Dispose();

    }



    public Bitmap Bitmap
    {
        get
        {

            return (bitmap);

        }

    }



    public struct PixelData
    {
        public byte blue;
        public byte green;
        public byte red;

    }



    private Point PixelSize
    {

        get
        {
            GraphicsUnit unit = GraphicsUnit.Pixel;
            RectangleF bounds = bitmap.GetBounds(ref unit);


            return new Point((int)bounds.Width, (int)bounds.Height);

        }

    }


    public void LockBitmap()
    {

        GraphicsUnit unit = GraphicsUnit.Pixel;

        RectangleF boundsF = bitmap.GetBounds(ref unit);
        Rectangle bounds = new Rectangle((int)boundsF.X,
     (int)boundsF.Y,

     (int)boundsF.Width,

      (int)boundsF.Height);



        width = (int)boundsF.Width * sizeof(PixelData);

        if (width % 4 != 0)
        {

            width = 4 * (width / 4 + 1);

        }

        bitmapData =

      bitmap.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);



        pBase = (Byte*)bitmapData.Scan0.ToPointer();

    }



    public PixelData GetPixel(int x, int y)
    {

        PixelData returnValue = *PixelAt(x, y);

        return returnValue;

    }



    public void SetPixel(int x, int y, PixelData colour)
    {

        PixelData* pixel = PixelAt(x, y);

        *pixel = colour;

    }



    public void UnlockBitmap()
    {

        bitmap.UnlockBits(bitmapData);

        bitmapData = null;

        pBase = null;
    }

    public PixelData* PixelAt(int x, int y)
    {

        return (PixelData*)(pBase + y * width + x * sizeof(PixelData));

    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
cat me.txt

Comments and Discussions