LockBitmap( )

builderanthologyAI and Robotics

Oct 19, 2013 (3 years and 7 months ago)

63 views

Lecture 7:

Basics of Machine Vision

WebCam Capture Demo

Fast Image Processing Methods for C#


public

unsafe

class

ImgPro


{


Bitmap

bitmap;


int

width;


BitmapData

bitmapData =
null
;


Byte
* pBase =
null
;


public

ImgPro(
Bitmap

bitmap)


{


this
.bitmap = bitmap;


}


public

void

Save(
string

filename)


{


bitmap.Save(filename,
ImageFormat
.Jpeg);


}


public

void

Dispose()


{


bitmap.Dispose();


}


public

Bitmap

Bitmap


{


get


{


return

(bitmap);


}


}

namespace

ImageProFast

{


public

struct

PixelData


{


public

byte

blue;


public

byte

green;


public

byte

red;


}


public

Point

PixelSize


{


get


{


GraphicsUnit

unit =
GraphicsUnit
.Pixel;


RectangleF

bounds = bitmap.GetBounds(
ref

unit);


return

new

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


}


}

The “trick” to writing fast image processing apps in
a managed code environment is to convert bitmaps
to byte
-
arrays.


In C# you have to declare that you are aware that
you are taking control of memory management by
calling the reserved word “unsafe”.


Clearly a programming language design influenced
by lawyers...


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
* PixelAt(
int

x,
int

y)


{


return

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


}


public

void

UnlockBitmap()


{


bitmap.UnlockBits(bitmapData);


bitmapData =
null
;


pBase =
null
;


}

LockBitmap( )


public

void

GrayScale()


{


Point

size = PixelSize;


LockBitmap();


for

(
int

x = 0; x < size.X; x++)


{


for

(
int

y = 0; y < size.Y; y++)


{


PixelData
* pPixel = PixelAt(x, y);


int

value = (pPixel
-
>red + pPixel
-
>green + pPixel
-
>blue) / 3;


pPixel
-
>red = (
byte
)value;


pPixel
-
>green = (
byte
)value;


pPixel
-
>blue = (
byte
)value;


}


}


UnlockBitmap();


}

GrayScale( )


public

void

Invert()


{


Point

size = PixelSize;


LockBitmap();


for

(
int

x = 0; x < size.X; x++)


{


for

(
int

y = 0; y < size.Y; y++)


{


PixelData
* pPixel = PixelAt(x, y);


pPixel
-
>red = (
byte
)(255
-

pPixel
-
>red);


pPixel
-
>green = (
byte
)(255
-

pPixel
-
>green);


pPixel
-
>blue = (
byte
)(255
-

pPixel
-
>blue);


}


}


UnlockBitmap();


}

Invert( )


public

void

Posterize(
int

bpp)


{


int

r, g, b, s, r0, g0, b0;


Point

size = PixelSize;


LockBitmap();


if

(bpp > 0)


s = 255 / bpp;


else


s = 255;


for

(
int

x = 0; x < size.X; x++)


{


for

(
int

y = 0; y < size.Y; y++)


{


PixelData
* pPixel = PixelAt(x, y);


r = pPixel
-
>red;


r0 = (r / s) * s;
// integer division and multiplication


g = pPixel
-
>green;
// is used to quantize the RGB color components


g0 = (g / s) * s;


b = pPixel
-
>blue;


b0 = (b / s) * s;


r = r0 + (255
-

r0) / (s / bpp);


g = g0 + (255
-

g0) / (s / bpp);


b = b0 + (255
-

b0) / (s / bpp);


pPixel
-
> red = (
byte
)r;


pPixel
-
> green = (
byte
)g;


pPixel
-
> blue = (
byte
)b;


}


}


UnlockBitmap();


}

Posterize(
int

bits
-
per
-
pixel)


public

void

Binary()


{


int

r, g, b;


Point

size = PixelSize;


LockBitmap();


for

(
int

x = 0; x < size.X; x++)


{


for

(
int

y = 0; y < size.Y; y++)


{


PixelData
* pPixel = PixelAt(x, y);


r = pPixel
-
>red;


g = pPixel
-
>green;


b = pPixel
-
>blue;


if

(r > 128)
// R G B


r = 255;
// 0 0 0 black


else

// 0 0 255 blue


r = 0;
// 0 255 0 green





// 0 255 255 cyan


if

(g > 128)
// 255 0 0 red


g = 255;
// 255 0 255 magenta


else

// 255 255 0 yellow


g = 0;
// 255 255 255 white


if

(b > 128)
// each pixel will be set to one of these 8 colors


b = 255;


else


b = 0;


pPixel
-
> red = (
byte
)r;


pPixel
-
> green = (
byte
)g;


pPixel
-
> blue = (
byte
)b;


}


}


UnlockBitmap();


}

Binary( )


public

void

Erode()


{


byte
[,] rmat =
new

byte
[bitmap.Width, bitmap.Height];


byte
[,] gmat =
new

byte
[bitmap.Width, bitmap.Height];


byte
[,] bmat =
new

byte
[bitmap.Width, bitmap.Height];


Point

size = PixelSize;


LockBitmap();


for

(
int

x = 1; x < size.X
-

1; x++)


{


for

(
int

y = 1; y < size.Y
-

1; y++)


{


PixelData
* pPixel = PixelAt(x, y);


rmat[x, y] = pPixel
-
>red;


gmat[x, y] = pPixel
-
>green;


bmat[x, y] = pPixel
-
>blue;


}


}


for

(
int

x = 1; x < size.X
-

1; x++)


{


for

(
int

y = 1; y < size.Y
-

1; y++)


{


PixelData
* pPixel = PixelAt(x, y);


if

(pPixel
-
>red == 0 && pPixel
-
>green == 0 && pPixel
-
>blue == 0)


{


for

(
int

k =
-
1; k < 2; k++)


{


for

(
int

m =
-
1; m < 2; m++)


{


rmat[x + k, y + m] = 0;


gmat[x + k, y + m] = 0;


bmat[x + k, y + m] = 0;


}


}


}


}


}


for

(
int

x = 1; x < size.X
-

1; x++)


{


for

(
int

y = 1; y < size.Y
-

1; y++)


{


PixelData
* pPixel = PixelAt(x, y);


pPixel
-
>red = rmat[x, y];


pPixel
-
>green = gmat[x, y];


pPixel
-
>blue = bmat[x, y];


}


}


UnlockBitmap();

}

Erode( )


public

void

Laplacian(
int

col)


{


int

avg;


byte
[,] lap =
new

byte
[bitmap.Width, bitmap.Height];


Point

size = PixelSize;


LockBitmap();


for

(
int

x = 1; x < size.X
-

1; x++)


{


for

(
int

y = 1; y < size.Y
-

1; y++)


{


PixelData
* c = PixelAt(x, y);


PixelData
* u = PixelAt(x
-

1, y);


PixelData
* d = PixelAt(x + 1, y);


PixelData
* l = PixelAt(x, y
-

1);


PixelData
* r = PixelAt(x, y + 1);


switch

(col)


{


case

0:


avg = (4 * (c
-
>red + c
-
>green + c
-
>blue)


-

(u
-
>red + u
-
>green + u
-
>blue)


-

(d
-
>red + d
-
>green + d
-
>blue)


-

(l
-
>red + l
-
>green + l
-
>blue)


-

(r
-
>red + r
-
>green + r
-
>blue)) / 3;


break
;


case

1:


avg = (4 * c
-
>red

-

u
-
>red

-

d
-
>red

-

l
-
>red

-

r
-
>red);


break
;


case

2:


avg = (4 * c
-
>green

-

u
-
>green

-

d
-
>green

-

l
-
>green

-

r
-
>green);


break
;


case

3:


avg = (4 * c
-
>blue

-

u
-
>blue

-

d
-
>blue

-

l
-
>blue

-

r
-
>blue);


break
;


default
:


avg = 0;


break
;


}


lap[x, y] = (
byte
)avg;


}


}


for

(
int

x = 1; x < size.X
-

1; x++)


{


for

(
int

y = 1; y < size.Y
-

1; y++)


{


PixelData
* pPixel = PixelAt(x, y);


pPixel
-
>red = lap[x, y];


pPixel
-
>green = lap[x, y];


pPixel
-
>blue = lap[x, y];


}


}


UnlockBitmap();

}

Laplacian(
int

color)


public

void

Majority()


{


Color

col =
new

Color
();


Color
[,] template =
new

Color
[3, 3];


byte
[,] rmat =
new

byte
[bitmap.Width, bitmap.Height];


byte
[,] gmat =
new

byte
[bitmap.Width, bitmap.Height];


byte
[,] bmat =
new

byte
[bitmap.Width, bitmap.Height];


Point

size = PixelSize;


LockBitmap();


for

(
int

x = 1; x < size.X; x++)


{


for

(
int

y = 1; y < size.Y; y++)


{


PixelData
* pPixel = PixelAt(x, y);


rmat[x, y] = pPixel
-
>red;


gmat[x, y] = pPixel
-
>green;


bmat[x, y] = pPixel
-
>blue;


}


}


for

(
int

x = 1; x < size.X
-

1; x++)


{


for

(
int

y = 1; y < size.Y
-

1; y++)


{


PixelData
* pPixel = PixelAt(x, y);


for

(
int

w = 0; w < 3; w++)


for

(
int

h = 0; h < 3; h++)


template[w, h] =
Color
.FromArgb(rmat[x + (w
-

1), y


+ (h
-

1)], gmat[x + (w
-

1), y + (h
-

1)], bmat[x + (w
-

1), y + (h
-

1)]);


MajorColor(template,
ref

col);


pPixel
-
>red = col.R;


pPixel
-
>green = col.G;


pPixel
-
>blue = col.B;


}


}


UnlockBitmap();


}

Majority( )


public

void

Saturate()


{


int

r, g, b;


Point

size = PixelSize;


LockBitmap();


for

(
int

x = 0; x < size.X; x++)


{


for

(
int

y = 0; y < size.Y; y++)


{


PixelData
* pPixel = PixelAt(x, y);


r = pPixel
-
>red;


g = pPixel
-
>green;


b = pPixel
-
>blue;


if

(r > b & r > g)


{


r = (255
-

r) / 4 + r;


g = 4 * g / 4;


b = 4 * b / 4;


}


if

(g > r & g > b)


{


g = (255
-

g) / 4 + g;


r = 4 * r / 4;


b = 4 * b / 4;


}


if

(b > r & b > g)


{


b = (255
-

b) / 4 + b;


r = 4 * r / 4;


g = 4 * g / 4;


}


pPixel
-
>red = (
byte
)r;


pPixel
-
>green = (
byte
)g;


pPixel
-
>blue = (
byte
)b;


}


}


UnlockBitmap();


}

Saturate( )