Skip to content

Instantly share code, notes, and snippets.

@ahmetkakici
Created January 28, 2017 19:49
Show Gist options
  • Save ahmetkakici/ff22dfaf7967bb84b0fbd91e912182b8 to your computer and use it in GitHub Desktop.
Save ahmetkakici/ff22dfaf7967bb84b0fbd91e912182b8 to your computer and use it in GitHub Desktop.
Blog
var nameProperty = user.GetType().GetProperty("Name");
var name = nameProperty.GetValue(user, null);
nameProperty.SetValue(user, "ahmeTT", null);
name = nameProperty.GetValue(user, null);
public static void SetValue(object container, string propertyName, object value)
{
container.GetType().GetProperty(propertyName).SetValue(container, value, null);
}
SetValue(user, "Name", "ahmeTT");
SetValue(user, "Name", 5);
public static void SetValue(object container, string propertyName, object value)
{
var propertyInfo = container.GetType().GetProperty(propertyName);
propertyInfo.SetValue(container, Convert.ChangeType(value, propertyInfo.PropertyType), null);
}
public static object GetValue(object container, string propertyName)
{
return container.GetType().GetProperty(propertyName).GetValue(container, null);
}
int[,] pixelArray = new int[pictureBox1.Image.Height, pictureBox1.Image.Width];
int[,] greyPixelArray = new int[pictureBox1.Image.Height, pictureBox1.Image.Width];
private void BuildPixelArray(Image myImage)
{
Rectangle rect = new Rectangle(0, 0, myImage.Width, myImage.Height);
Bitmap temp = new Bitmap(myImage);
BitmapData bmpData = temp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int remain = bmpData.Stride - bmpData.Width * 3;
unsafe
{
byte* ptr = (byte*)bmpData.Scan0;
for (int i = 0; i < bmpData.Height; i++)
{
for (int j = 0; j < bmpData.Width; j++)
{
pixelArray[i, j] = ptr[0] + ptr[1] * 255 + ptr[2] * 255 * 255;
greyPixelArray[i, j] = (int)((double)ptr[0] * 0.11 + (double)ptr[1] * 0.59 + (double)ptr[2] * 0.3);
ptr += 3;
}
ptr += remain;
}
}
temp.UnlockBits(bmpData);
}
private void SetImage(int[,] sourceArray, ref PictureBox myPictureBox)
{
Rectangle rect = new Rectangle(0, 0, myPictureBox.Image.Width, myPictureBox.Image.Height);
Bitmap temp = new Bitmap(myPictureBox.Image);
BitmapData bmpData = temp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int remain = bmpData.Stride - bmpData.Width * 3;
unsafe
{
byte* ptr = (byte*)bmpData.Scan0;
for (int i = 0; i < bmpData.Height; i++)
{
for (int j = 0; j < bmpData.Width; j++)
{
ptr[0] = ptr[1] = ptr[2] = (byte)sourceArray[i, j];
ptr += 3;
}
ptr += remain;
}
}
temp.UnlockBits(bmpData);
myPictureBox.Image = temp;
}
public void SetColorImage(ref int[,] sourceArray)
{
Rectangle rect = new Rectangle(0, 0, sourceArray.GetLength(0), sourceArray.GetLength(1));
Bitmap temp = new Bitmap(sourceArray.GetLength(0), sourceArray.GetLength(1));
BitmapData bmpData = temp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int remain = bmpData.Stride - bmpData.Width * 3;
unsafe
{
byte* ptr = (byte*)bmpData.Scan0;
for (int i = 0; i < bmpData.Width; i++)
{
for (int j = 0; j < bmpData.Height; j++)
{
ptr[0] = (byte)(sourceArray[i, j] % 256);
ptr[1] = (byte)((sourceArray[i, j] / 256) % 256);
ptr[2] = (byte)((sourceArray[i, j] / 65536) % 256);
ptr += 3;
}
ptr += remain;
}
}
temp.UnlockBits(bmpData);
destPictBox.Image = temp;
}
int[,] pixelArray = new int[pictureBox1.Image.Height, pictureBox1.Image.Width];
int[,] greyPixelArray = new int[pictureBox1.Image.Height, pictureBox1.Image.Width];
int[] histogram = new int[256];
void BuildPixelArray(ref Image myImage)
{
pixelArray = new int[imageWidth, imageHeight];
greyPixelArray = new int[imageWidth, imageHeight];
Rectangle rect = new Rectangle(0, 0, myImage.Width, myImage.Height);
Bitmap temp = new Bitmap(myImage);
BitmapData bmpData = temp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int remain = bmpData.Stride - bmpData.Width * 3;
unsafe
{
byte* ptr = (byte*)bmpData.Scan0;
for (int j = 0; j < bmpData.Height; j++)
{
for (int i = 0; i < bmpData.Width; i++)
{
pixelArray[i, j] = ptr[0] + ptr[1] * 256 + ptr[2] * 256 * 256;
greyPixelArray[i, j] = (int)((double)ptr[0] * 0.11 + (double)ptr[1] * 0.59 + (double)ptr[2] * 0.3);
histogram[greyPixelArray[i, j]]++;
ptr += 3;
}
ptr += remain;
}
}
temp.UnlockBits(bmpData);
}
int[,] pixelArray = new int[pictureBox1.Image.Height, pictureBox1.Image.Width];
int[,] greyPixelArray = new int[pictureBox1.Image.Height, pictureBox1.Image.Width];
int[] histogram = new int[256];
void BuildPixelArray(ref Image myImage)
{
pixelArray = new int[imageWidth, imageHeight];
greyPixelArray = new int[imageWidth, imageHeight];
Rectangle rect = new Rectangle(0, 0, myImage.Width, myImage.Height);
Bitmap temp = new Bitmap(myImage);
BitmapData bmpData = temp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int remain = bmpData.Stride - bmpData.Width * 3;
unsafe
{
byte* ptr = (byte*)bmpData.Scan0;
for (int j = 0; j < bmpData.Height; j++)
{
for (int i = 0; i < bmpData.Width; i++)
{
pixelArray[i, j] = ptr[0] + ptr[1] * 256 + ptr[2] * 256 * 256;
greyPixelArray[i, j] = (int)((double)ptr[0] * 0.11 + (double)ptr[1] * 0.59 + (double)ptr[2] * 0.3);
histogram[greyPixelArray[i, j]]++;
ptr += 3;
}
ptr += remain;
}
}
temp.UnlockBits(bmpData);
}
int[,] gaussianPixelArray;
void Gaussian()
{
gaussianPixelArray = new int[imageWidth, imageHeight];
int[,] gaussianArray =
new int[5, 5]
{
{1, 4, 7, 4, 1},
{4, 16, 26, 16, 4},
{7, 26, 41, 26, 7},
{4, 16, 26, 16, 4},
{1, 4, 7, 4, 1}
};
int tempSum;
for (int i = 2; i < imageHeight - 2; i++)
{
for (int j = 2; j < imageWidth - 2; j++)
{
tempSum = 0;
for (int k = -2; k < 3; k++)
{
for (int l = -2; l < 3; l++)
{
tempSum += greyPixelArray[j + l, i + k] * gaussianArray[k + 2, l + 2];
}
}
gaussianPixelArray[j, i] = tempSum / 273;
}
}
}
int otsuValue;
void Otsu()
{
double fmax = -1.0;
double m1, m2, S, toplam1 = 0.0, toplam2 = 0.0;
int nTop = 0, n1 = 0, n2;
for (int i = 0; i < 256; i++)
{
toplam1 += (double)i * (double)histogram[i];
nTop += histogram[i];
}
for (int i = 0; i < 256; i++)
{
n1 += histogram[i];
if (n1 == 0)
continue;
n2 = nTop - n1;
if (n2 == 0)
break;
toplam2 += (double)i * (double)histogram[i];
m1 = toplam2 / n1;
m2 = (toplam1 - toplam2) / n2;
S = (double)n1 * (double)n2 * (m1 - m2) * (m1 - m2);
if (S > fmax)
{
fmax = S;
otsuValue = i;
}
}
}
void Binary(int argThreshold)
{
binaryPixelArray = new int[imageWidth, imageHeight];
for (int i = 0; i < imageWidth; i++)
{
for (int j = 0; j < imageHeight; j++)
{
if (greyPixelArray[i, j] < threshold)
binaryPixelArray[i, j] = 0;
else
binaryPixelArray[i, j] = 255;
}
}
}
void Sobel(int type)
{
int normalizeMax = 0;
int normalizeMin = maxIntVal;
sobelPixelArray = new int[imageWidth, imageHeight];
int[,] sobelArray1 =
new int[3, 3]
{
{1, 0, -1},
{2, 0, -2},
{1, 0, -1}
};
int[,] sobelArray2 =
new int[3, 3]
{
{1, 2, 1},
{0, 0, 0},
{-1, -2, -1}
};
int[,] sobelArray3 =
new int[3, 3]
{
{2, 1, 0},
{1, 0, -1},
{0, -1, -2}
};
int G1, G2, G3;
for (int i = 1; i < imageWidth - 1; i++)
{
for (int j = 1; j < imageHeight - 1; j++)
{
G1 = 0;
G2 = 0;
G3 = 0;
for (int k = -1; k < 2; k++)
{
for (int l = -1; l < 2; l++)
{
G1 += greyPixelArray[i + k, j + l] * sobelArray1[k + 1, l + 1];
G2 += greyPixelArray[i + k, j + l] * sobelArray2[k + 1, l + 1];
G3 += greyPixelArray[i + k, j + l] * sobelArray3[k + 1, l + 1];
}
}
if (type == 0)
{
sobelPixelArray[i, j] = Math.Abs(G1) + Math.Abs(G2) + Math.Abs(G3);
}
else if (type == 1)
{
sobelPixelArray[i, j] = Math.Abs(G2);
}
else if (type == 2)
{
sobelPixelArray[i, j] = Math.Abs(G1);
}
else if (type == 3)
{
sobelPixelArray[i, j] = Math.Abs(G3);
}
if (normalizeMax < sobelPixelArray[i, j])
normalizeMax = sobelPixelArray[i, j];
if (normalizeMin > sobelPixelArray[i, j])
normalizeMin = sobelPixelArray[i, j];
}
}
NormalizeArray(ref sobelPixelArray, normalizeMax, normalizeMin);
}
void NormalizeArray(ref int[,] sourceArray, int normalizeMax, int normalizeMin)
{
int factor = normalizeMax - normalizeMin;
for (int i = 0; i < sourceArray.GetLength(0); i++)
{
for (int j = 0; j < sourceArray.GetLength(1); j++)
{
sourceArray[i, j] = (sourceArray[i, j] - normalizeMin) * 255 / factor;
}
}
}
int[,] prewittPixelArray;
void Prewitt()
{
int normalizeMax = 0;
int normalizeMin = maxIntVal;
prewittPixelArray = new int[imageWidth, imageHeight];
int[,] PrewittArray1 = new int[3, 3]
{
{-1, -1, -1},
{0, 0, 0},
{1, 1, 1}
};
int[,] PrewittArray2 = new int[3, 3]
{
{-1, 0, 1},
{-1, 0, 1},
{-1, 0, 1}
};
int[,] PrewittArray3 = new int[3, 3]
{
{-1, -1, 0},
{-1, 0, 1},
{0, 1, 1}
};
int[,] PrewittArray4 = new int[3, 3]
{
{1, 1, 0},
{1, 0, -1},
{0, -1, -1}
};
int G1, G2, G3, G4;
for (int i = 1; i < imageWidth - 1; i++)
{
for (int j = 1; j < imageHeight - 1; j++)
{
G1 = 0;
G2 = 0;
G3 = 0;
G4 = 0;
for (int k = -1; k < 2; k++)
{
for (int l = -1; l < 2; l++)
{
G1 += greyPixelArray[i + k, j + l] * PrewittArray1[k + 1, l + 1];
G2 += greyPixelArray[i + k, j + l] * PrewittArray2[k + 1, l + 1];
G3 += greyPixelArray[i + k, j + l] * PrewittArray3[k + 1, l + 1];
G4 += greyPixelArray[i + k, j + l] * PrewittArray4[k + 1, l + 1];
}
}
prewittPixelArray[i, j] = Math.Abs(G1) + Math.Abs(G2) + Math.Abs(G3) + Math.Abs(G4);
if (normalizeMax < prewittPixelArray[i, j])
normalizeMax = prewittPixelArray[i, j];
if (normalizeMin > prewittPixelArray[i, j])
normalizeMin = prewittPixelArray[i, j];
}
}
int factor = normalizeMax - normalizeMin;
for (int j = 1; j < imageHeight - 1; j++)
{
for (int i = 1; i < imageWidth - 1; i++)
{
prewittPixelArray[i, j] = (prewittPixelArray[i, j] - normalizeMin) * 255 / factor;
}
}
}
int[,] robertPixelArray;
void Robert()
{
robertPixelArray = new int[imageWidth, imageHeight];
for (int i = 1; i < imageHeight - 1; i++)
{
for (int j = 1; j < imageWidth - 1; j++)
{
robertPixelArray[j, i] = Math.Abs(greyPixelArray[j, i] - greyPixelArray[j - 1, i - 1]) +
Math.Abs(greyPixelArray[j, i - 1] - greyPixelArray[j - 1, i]);
}
}
}
void Mean()
{
meanPixelArray = new int[imageWidth, imageHeight];
for (int i = 1; i < imageHeight - 1; i++)
{
for (int j = 1; j < imageWidth - 1; j++)
{
meanPixelArray[j, i] =
(
greyPixelArray[j, i - 1] + greyPixelArray[j, i + 1] +
greyPixelArray[j, i] + greyPixelArray[j - 1, i - 1] +
greyPixelArray[j - 1, i + 1] + greyPixelArray[j - 1, i] + greyPixelArray[j + 1, i - 1] +
greyPixelArray[j + 1, i + 1] + greyPixelArray[j + 1, i]
)/9;
}
}
}
int[,] medianPixelArray;
void Median()
{
medianPixelArray = new int[imageWidth, imageHeight];
int[] tempArray = new int[9];
for (int i = 1; i < imageHeight - 1; i++)
{
for (int j = 1; j < imageWidth - 1; j++)
{
int counter = 0;
for (int k = -1; k < 2; k++)
{
for (int l = -1; l < 2; l++)
{
tempArray[counter++] = greyPixelArray[j + l, i + k];
}
}
System.Array.Sort(tempArray);
medianPixelArray[j, i] = tempArray[4];
}
}
}
void Dilation()
{
if (!dilationDone)
{
dilationPixelArray = new int[imageWidth, imageHeight];
for (int i = 1; i < imageWidth - 1; i++)
{
for (int j = 1; j < imageHeight - 1; j++)
{
if (binaryPixelArray[i, j] == 255)
{
dilationPixelArray[i - 1, j] = 255;
dilationPixelArray[i, j - 1] = 255;
dilationPixelArray[i, j + 1] = 255;
dilationPixelArray[i + 1, j] = 255;
}
}
}
}
else
{
int[,] tempArray = new int[imageWidth, imageHeight];
Array.Copy(dilationPixelArray, tempArray, imageWidth * imageHeight);
for (int i = 1; i < imageWidth - 1; i++)
{
for (int j = 1; j < imageHeight - 1; j++)
{
if (tempArray[i, j] == 255)
{
dilationPixelArray[i - 1, j] = 255;
dilationPixelArray[i, j - 1] = 255;
dilationPixelArray[i, j + 1] = 255;
dilationPixelArray[i + 1, j] = 255;
}
}
}
}
}
void Erosion()
{
if (!erosionDone)
{
erosionPixelArray = new int[imageWidth, imageHeight];
for (int i = 1; i < imageWidth - 1; i++)
{
for (int j = 1; j < imageHeight - 1; j++)
{
if (binaryPixelArray[i, j] == 255)
{
if (binaryPixelArray[i, j - 1] == 0 ||
binaryPixelArray[i - 1, j] == 0 ||
binaryPixelArray[i + 1, j] == 0 ||
binaryPixelArray[i, j + 1] == 0
)
{
erosionPixelArray[i - 1, j] = 0;
erosionPixelArray[i, j - 1] = 0;
erosionPixelArray[i, j + 1] = 0;
erosionPixelArray[i + 1, j] = 0;
erosionPixelArray[i, j] = 0;
}
else
{
erosionPixelArray[i, j] = binaryPixelArray[i, j];
}
}
else
{
erosionPixelArray[i, j] = binaryPixelArray[i, j];
}
}
}
}
else
{
int[,] tempArray = new int[imageWidth, imageHeight];
Array.Copy(erosionPixelArray, tempArray, imageWidth * imageHeight);
for (int i = 1; i < imageWidth - 1; i++)
{
for (int j = 1; j < imageHeight - 1; j++)
{
if (tempArray[i, j] == 255)
{
if (tempArray[i, j - 1] == 0 ||
tempArray[i - 1, j] == 0 ||
tempArray[i + 1, j] == 0 ||
tempArray[i, j + 1] == 0
)
{
erosionPixelArray[i - 1, j] = 0;
erosionPixelArray[i, j - 1] = 0;
erosionPixelArray[i, j + 1] = 0;
erosionPixelArray[i + 1, j] = 0;
erosionPixelArray[i, j] = 0;
}
else
{
erosionPixelArray[i, j] = tempArray[i, j];
}
}
else
{
erosionPixelArray[i, j] = tempArray[i, j];
}
}
}
}
}
public void ShowDilation()
{
if (!dilationDone)
{
if (!binaryDone)
{
if (otsuValue == 0)
Otsu();
Binary(256);
}
Dilation();
dilationDone = true;
}
else
{
Dilation();
}
SetImage(ref dilationPixelArray);
}
public void ShowErosion()
{
if (!erosionDone)
{
if (!binaryDone)
{
if (otsuValue == 0)
Otsu();
Binary(256);
}
Erosion();
erosionDone = true;
}
else
{
Erosion();
}
SetImage(ref erosionPixelArray);
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
}
var user = new User
{
Id = 1,
Name = "Ahmet",
Surname = "Kakıcı"
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment