1 / 12

Lecture 7:

Lecture 7:. Basics of Machine Vision. WebCam Capture Demo. Fast Image Processing Methods for C#. namespace ImageProFast { public struct PixelData { public byte blue; public byte green; public byte red; }.

colby
Download Presentation

Lecture 7:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 7: Basics of Machine Vision

  2. WebCam Capture Demo

  3. Fast Image Processing Methods for C# namespace ImageProFast { publicstructPixelData { publicbyte blue; publicbyte green; publicbyte red; } 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... publicunsafeclassImgPro { Bitmap bitmap; int width; BitmapData bitmapData = null; Byte* pBase = null; public ImgPro(Bitmap bitmap) { this.bitmap = bitmap; } publicvoid Save(string filename) { bitmap.Save(filename, ImageFormat.Jpeg); } publicvoid Dispose() { bitmap.Dispose(); } publicBitmap Bitmap { get { return (bitmap); } } publicPoint PixelSize { get { GraphicsUnit unit = GraphicsUnit.Pixel; RectangleF bounds = bitmap.GetBounds(ref unit); returnnewPoint((int)bounds.Width, (int)bounds.Height); } }

  4. LockBitmap( ) publicvoid LockBitmap() { GraphicsUnit unit = GraphicsUnit.Pixel; RectangleF boundsF = bitmap.GetBounds(ref unit); Rectangle bounds = newRectangle((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(); } publicPixelData* PixelAt(int x, int y) { return (PixelData*)(pBase + y * width + x * sizeof(PixelData)); } publicvoid UnlockBitmap() { bitmap.UnlockBits(bitmapData); bitmapData = null; pBase = null; }

  5. GrayScale( ) publicvoid 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(); }

  6. Invert( ) publicvoid 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(); }

  7. Posterize(int bits-per-pixel) publicvoid 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(); }

  8. Binary( ) publicvoid 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(); }

  9. Erode( ) publicvoid Erode() { byte[,] rmat = newbyte[bitmap.Width, bitmap.Height]; byte[,] gmat = newbyte[bitmap.Width, bitmap.Height]; byte[,] bmat = newbyte[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(); }

  10. Laplacian(int color) publicvoid Laplacian(int col) { int avg; byte[,] lap = newbyte[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(); }

  11. Majority( ) publicvoid Majority() { Color col = newColor(); Color[,] template = newColor[3, 3]; byte[,] rmat = newbyte[bitmap.Width, bitmap.Height]; byte[,] gmat = newbyte[bitmap.Width, bitmap.Height]; byte[,] bmat = newbyte[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(); }

  12. Saturate( ) publicvoid 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(); }

More Related