|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace UPImage
{
public class Skeletonization
{
public static Bitmap MedialAxisTransform(Bitmap orginal)
{
Bitmap bmp;
if (ImageProcessing.IsGrayscale(orginal) == false)
{
bmp = ImageProcessing.ColorToIndexedGrayscale(orginal);
}
else
{
bmp = (Bitmap)orginal.Clone();
}
// Create a new bitmap.
int width, height;
width = orginal.Width;
height = orginal.Height;
Bitmap newBitmap = (Bitmap)bmp.Clone();
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, newBitmap.Width, newBitmap.Height);
System.Drawing.Imaging.BitmapData bmpData =
newBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
newBitmap.PixelFormat);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * orginal.Height;
byte[] rgbValues = new byte[bytes];
byte[] rgbNewValures = new byte[bytes];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
rgbNewValures = (byte[])rgbValues.Clone();
while (true)
{
int count = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
Byte c = rgbValues[i * bmpData.Stride + j];
if (c<128)
{
Cell cell = new Cell(i, j, width, height, rgbValues, bmpData.Stride);
if (cell.Apz == 1 && 4 <= cell.Bpz && cell.Bpz <= 7 && cell.C1 == false && cell.D1 == false)
{
rgbNewValures[i * bmpData.Stride + j] = 255;
count++;
}
}
}
}
if (count == 0)
{
break;
}
count = 0;
rgbValues =(byte[]) rgbNewValures.Clone();
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
Byte c = rgbNewValures[i * bmpData.Stride + j];
if (c<128)
{
Cell cell = new Cell(i, j, width, height, rgbValues, bmpData.Stride);
if (cell.Apz == 1 && 5 <= cell.Bpz && cell.Bpz <= 7 && cell.C2 == false && cell.D2 == false)
{
rgbNewValures[i * bmpData.Stride + j] = 255;
count++;
}
}
}
}
if (count == 0)
{
break;
}
}
// Copy the RGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(rgbNewValures, 0, ptr, bytes);
// Unlock the bits.
newBitmap.UnlockBits(bmpData);
rgbValues = null;
return newBitmap;
}
}
internal class Cell
{
#region members
public int Apz;
public int Bpz;
public bool C1;
public bool C2;
public bool D1;
public bool D2;
private bool[] bMatrix;
#endregion
public Cell(int i, int j, int width, int height, byte[] rgbValues,int stride)
{
bMatrix = new bool[8];
int y, x;
y = i - 1;
x = j - 1;
if (y < 0 || x < 0)
{
bMatrix[7] = false;
}
else
{
Byte c = rgbValues[y*stride+ x];
if (c<128)
{
bMatrix[7] = true;
}
else
{
bMatrix[7] = false;
}
}
y = i - 1;
x = j;
if (y < 0)
{
bMatrix[0] = false;
}
else
{
Byte c = rgbValues[y * stride + x];
if (c<128)
{
bMatrix[0] = true;
}
else
{
bMatrix[0] = false;
}
}
y = i - 1;
x = j + 1;
if (y < 0 || x >= width)
{
bMatrix[1] = false;
}
else
{
Byte c = rgbValues[y * stride + x];
if (c<128)
{
bMatrix[1] = true;
}
else
{
bMatrix[1] = false;
}
}
y = i;
x = j + 1;
if (x >= width)
{
bMatrix[2] = false;
}
else
{
Byte c = rgbValues[y * stride + x];
if (c<128)
{
bMatrix[2] = true;
}
else
{
bMatrix[2] = false;
}
}
y = i + 1;
x = j + 1;
if (y >= height || x >= width)
{
bMatrix[3] = false;
}
else
{
Byte c = rgbValues[y * stride + x];
if (c<128)
{
bMatrix[3] = true;
}
else
{
bMatrix[3] = false;
}
}
y = i + 1;
x = j;
if (y >= height)
{
bMatrix[4] = false;
}
else
{
Byte c = rgbValues[y * stride + x];
if (c<128)
{
bMatrix[4] = true;
}
else
{
bMatrix[4] = false;
}
}
y = i + 1;
x = j - 1;
if (y >= height || x < 0)
{
bMatrix[5] = false;
}
else
{
Byte c = rgbValues[y * stride + x];
if (c<128)
{
bMatrix[5] = true;
}
else
{
bMatrix[5] = false;
}
}
y = i;
x = j - 1;
if (x < 0)
{
bMatrix[6] = false;
}
else
{
Byte c = rgbValues[y * stride + x];
if (c<128)
{
bMatrix[6] = true;
}
else
{
bMatrix[6] = false;
}
}
for (int ii = 0; ii < 8; ii++)
{
if (bMatrix[ii] == true)
{
Bpz++;
}
}
bool prev = false;
bool next = false;
for (int ii = 1; ii < 8; ii++)
{
if (ii == 1)
{
prev =bMatrix[7];
}
next = bMatrix[ii];
if (prev == false && next == true)
{
Apz++;
}
prev = next;
}
C1 = bMatrix[0] & bMatrix[2] & bMatrix[4];
C2 = bMatrix[0] & bMatrix[2] & bMatrix[6];
D1 = bMatrix[2] & bMatrix[4] & bMatrix[6];
D2 = bMatrix[0] & bMatrix[4] & bMatrix[6];
}
}
}
|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.