Images
I wrote this for an INETA project that I have been working on, but thought it would be great to share with everyone. We had a need to take an image and change its size. Below is an implementation of a Resize Extension Method on the .NET Image object.
I borrowed some ideas from this post by Mark McDonnell (http://weblogs.asp.net/markmcdonnell/archive/2008/03/09/resize-image-before-uploading-to-server.aspx)
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Drawing;
6: using System.Drawing.Drawing2D;
7: ...
BTW to convert from the byte[] back to an image is simple enough
private Bitmap ConvertBitmap(byte[] frame, int width, int height){ Bitmap bmp = new Bitmap( width, height, PixelFormat.Format24bppRgb);
BitmapData data = bmp.LockBits( new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
System.Runtime.InteropServices.Marshal.Copy(frame, 0, data.Scan0, frame.Length);
bmp.UnlockBits(data); return bmp;}
I was tearing into v1.5 of Microsoft Robotics Studio (http://www.microsoft.com/robotics) and came across a novel way to process the pixels in an image. In previous posts I was using unmanaged C# code and pointers to move around the image. This worked well but is hard to manage. They take the approach of just dumping the entire image to a byte[], pure genius, now why did I not think of that. I will be rebuilding my library to utilize this technique but I thought this was so cool that I just had to share it!
/// <summary>/// Coverts a bitmap to a...
Here is the powerpoint for my presentation tomorrow.
http://www.onetug.org/DNN/Portals/0/member_files/2007_07_14_robot.ppt
In my earlier post I put together an image processing class (http://drowningintechnicaldebt.com/blogs/shawnweisfeld/archive/2006/11/06/Bitmap-Processing-in-C_2300_.aspx). I needed a method that built a bounding box around a given shape. Here is the code:
public static void BoundingBox(Bitmap b, ref Point upperLeft, ref Point lowerRight){ ImagerBitmap i = new ImagerBitmap(b.Clone() as Bitmap); Queue<Point> q = new Queue<Point>(); q.Enqueue(upperLeft);
int jump = 10;
while (q.Count > 0) { Point p = q.Dequeue(); AddToStack(q, b, i, new Point(p.X + jump, p.Y), ref upperLeft, ref lowerRight); AddToStack(q, b, i, new Point(p.X - jump, p.Y), ref upperLeft, ref lowerRight); AddToStack(q, b, i, new Point(p.X, p.Y + jump), ref upperLeft, ref...
In my earlier post I put together an image processing class (http://drowningintechnicaldebt.com/blogs/shawnweisfeld/archive/2006/11/06/Bitmap-Processing-in-C_2300_.aspx). I needed a method that found the center of a given mass of pixels. Here is the code:
public static Point CenterOfMass(Bitmap b){ ImagerBitmap i = new ImagerBitmap(b.Clone() as Bitmap); int cogX = 0; int cogY = 0; int cogTotal = 0;
for (int column = 0; column < i.Bitmap.Width; column++) { for (int row = 0; row < i.Bitmap.Height; row++) { int px = i.GetGreyPixel(column, row); cogX += (px * column); cogY += (px * row); cogTotal += px; } } i.UnlockBitmap(); return new Point(cogX / cogTotal, cogY...
In my earlier post I put together an image processing class (http://drowningintechnicaldebt.com/blogs/shawnweisfeld/archive/2006/11/06/Bitmap-Processing-in-C_2300_.aspx). I needed a method that did some segmentation based on a given color. It needed to take in the color and a threshold and it would black out any pixels that are not within the color threshold. Here is the code:
public static Bitmap RemoveBackground(Bitmap b, Color c, int threshold){ ImagerBitmap i = new ImagerBitmap(b.Clone() as Bitmap); int maxGreen = c.G + threshold; int minGreen = c.G - threshold; int maxRed = c.R + threshold; int minRed = c.R - threshold; int maxBlue = c.B + threshold; int minBlue...
For a recent project I needed a Flood Fill Algorithm (see Flood Fill in Wikipedia, http://en.wikipedia.org/wiki/Flood_fill). Here is my implementation in C++.
Some things to note
1) I am using the ImagerBitmapClass from a previous post (http://drowningintechnicaldebt.com/blogs/shawnweisfeld/archive/2006/11/06/Bitmap-Processing-in-C_2300_.aspx), which I converted to C++
2) I am doing this fill in grey scale, you can easily change it to use full color objects
3) I am looking for all white objects, converting the first blob of white pixels to a given color, then moving to the next blob with a new color
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
marker = 100;for (int row =...
Need to crack a bitmap and dittle around with its pixels? I know there is a managed get pixel property on the bitmap object but that is SLOW if you have to work with a lot of pixels. After some digging around I have compiled a few examples together to make my ImagerBitmap Class. The class uses unmanaged C# and pointers to iterate over the pixels in an image to do stuff. Since unmanaged code is inherently dangerous everything is made private and only some static functions that perform the image filters are exposed. Some of the functions included are:...