im using the second block code to find Even and Odd columns of a bitmap in order to build a Checkered page in bitmap i used my IsEven() and IsOdd() function
i want to do this job with lockbits:
public unsafe void DrawCheckeredPage()
{
Bitmap bmpShati = null;
bmpShati = new Bitmap(#"\ColorReductedBMP.bmp");
Color GrayOne = Color.FromArgb(153, 153, 153);
Color WhiteOne = Color.FromArgb(255, 255, 255);
for (int x = 0; x < bmpShati.Width; x++)
{
for (int y = 0; y < bmpShati.Height; y++)
{
Color PreCol = bmpShati.GetPixel(x, y);
if (OmitedPictureBoxes.Exists(item => (Color)item.Tag == PreCol))
{
if (IsEven(x) && IsEven(y))
{
bmpShati.SetPixel(x, y, GrayOne);
}
else if (IsOdd(x) && IsOdd(y))
{
bmpShati.SetPixel(x, y, GrayOne);
}
else if (IsEven(x) && IsOdd(y))
{
bmpShati.SetPixel(x, y, WhiteOne);
}
else if (IsOdd(x) && IsEven(y))
{
bmpShati.SetPixel(x, y, WhiteOne);
}
}
}
}
bmpShati = resizeBitmap2(bmpShati, newW, newH);
pbMain.Invoke((Action)(() => pbMain.Image = bmpShati));
}
but there is a problem that Checkered page is not drawing correct i did it with GetPixel() and SetPixel but i i must use LockBits, can you please help me?
here is my code :
public unsafe void DrawCheckeredPage()
{
Bitmap bmpShati = null;
bmpShati = new Bitmap(#"\ColorReductedBMP.bmp");
Rectangle rect = new Rectangle(0, 0, bmpShati.Width, bmpShati.Height);
var _bdata = bmpShati.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
var _ptr = (byte*)_bdata.Scan0;
int _numBytes = _bdata.Stride * _bdata.Height, row, col;
for (int i = 0; i < _numBytes; i += 4)
{
Color PreCol = Color.FromArgb(
(*(_ptr + i + 2)),
(*(_ptr + i + 1)),
(*(_ptr + i))
);
if (OmitedPictureBoxes.Exists(item => (Color)item.Tag == PreCol))
{
row = i / _bdata.Stride;
col = i % _bdata.Stride;
if (IsEven(row) && IsEven(col))
{
(*(_ptr + i + 2)) = Convert.ToByte(153);
(*(_ptr + i + 1)) = Convert.ToByte(153);
(*(_ptr + i)) = Convert.ToByte(153);
}
else if (IsOdd(row) && IsOdd(col))
{
(*(_ptr + i + 2)) = Convert.ToByte(153);
(*(_ptr + i + 1)) = Convert.ToByte(153);
(*(_ptr + i)) = Convert.ToByte(153);
}
else if (IsEven(row) && IsOdd(col))
{
(*(_ptr + i + 2)) = Convert.ToByte(255);
(*(_ptr + i + 1)) = Convert.ToByte(255);
(*(_ptr + i)) = Convert.ToByte(255);
}
else if (IsOdd(row) && IsEven(col))
{
(*(_ptr + i + 2)) = Convert.ToByte(255);
(*(_ptr + i + 1)) = Convert.ToByte(255);
(*(_ptr + i)) = Convert.ToByte(255);
}
}
}
bmpShati.UnlockBits(_bdata);
bmpShati = resizeBitmap2(bmpShati, newW, newH);
pbMain.Invoke((Action)(() => pbMain.Image = bmpShati));
}
public bool IsOdd(int value)
{
return value % 2 != 0;
}
public static bool IsEven(int value)
{
return value % 2 == 0;
}
Related
I'm trying to implement KMP algorithm. Part "if (W[i] == S[m + i])" returns index out of range exception and I can't get it to work.
I was following example on Wikipedia: https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
static int[] KMPTable(string W)
{
int[] T = new int[W.Length];
int pos = 2;
int cnd = 0;
T[0] = -1;
T[1] = 0;
while (pos < W.Length)
{
if (W[pos - 1] == W[cnd])
{
T[pos] = cnd + 1;
cnd = cnd + 1;
pos = pos + 1;
}
else
if (cnd > 0)
{
cnd = T[cnd];
}
else
{
T[pos] = 0;
pos = pos + 1;
}
}
return T;
}
static int[] KMPSearch(string S, string W)
{
int m = 0;
int i = 0;
int[] kmpNext = KMPTable(S);
List<int> result = new List<int>();
while (m + i < S.Length)
{
if (W[i] == S[m + i])
{
if (i == W.Length - 1)
{
result.Add(m);
}
i = i + 1;
}
else
{
m = m + i - kmpNext[i];
if (kmpNext[i] > -1)
i = kmpNext[i];
else
i = 0;
}
}
return result.ToArray();
}
When m + i < S.Length, then it might be W[i] that is out of its index. Try checking with a step-by-step debug.
I am trying to implement image noise reduction using MRF. I used this article. It also provides this code in matlab. I tried to rewrite it in c# to work with noised images with only blue channel like this
But after using my code I got only darker image. What am I doing wrong? Is it something in matlab that I didn't include in my code?
My code is:
public static Bitmap Process(Bitmap src, double covar, double max_diff, double weight_diff, int iterations)
{
var buffer = new Bitmap[2];
var b = new Bitmap(src.Width, src.Height, src.PixelFormat);
for (var i = 0; i < src.Width; i++)
for (var j = 0; j < src.Height; j++)
{
b.SetPixel(i, j, Color.FromArgb(src.GetPixel(i, j).A, 0, 0, 0));
}
buffer[0] = src;
buffer[1] = b;
var s = 1;
var d = 0;
var vMax = (src.Width * src.Height) * (Math.Pow(256, 2) / (2 * covar) + 4 * weight_diff * max_diff);
for (var i = 0; i < iterations; i++)
{
Console.WriteLine("Iteration: " + i.ToString());
if (s == 0) { s = 1; d = 0; }
else { s = 0; d = 1; }
for (var r = 0; r < src.Height - 1; r++)
for (var c = 0; c < src.Width - 1; c++)
{
//val = red
var vLocal = vMax;
var minVal = -1;
for (var val = 0; val < 255; val+=1)
{
var V_Data = Math.Pow((val - src.GetPixel(r, c).B), 2) / (2 * covar); //r
var V_diff = 0;
if (r > 1)
{
V_diff = V_diff + (int)Math.Min((val - Math.Pow(buffer[s].GetPixel(r - 1, c).B, 2)), max_diff);
}
if (r < src.Height)
{
V_diff = V_diff + (int)Math.Min((val - Math.Pow(buffer[s].GetPixel(r + 1, c).B, 2)), max_diff);
}
if (c > 1)
{
V_diff = V_diff + (int)Math.Min((val - Math.Pow(buffer[s].GetPixel(r, c - 1).B, 2)), max_diff);
}
if (c < src.Height)
{
V_diff = V_diff + (int)Math.Min((val - Math.Pow(buffer[s].GetPixel(r, c + 1).B, 2)), max_diff);
}
var V_current = V_Data + weight_diff * V_diff;
if (V_current < vLocal)
{
minVal = val;
vLocal = V_current;
}
}
Color color = buffer[d].GetPixel(r, c);
var e = Color.FromArgb(color.A, color.R, color.G, minVal);
buffer[d].SetPixel(r, c, e);
}
}
return buffer[d];
}
I'm trying to set opacity of surroundings of particular pixels in bitmap.
For now I can set opacity of pixels that meets some conditions. (e.g. pixel on left or rigt or top or down is not 100% white)
I'm using Bitmap.LockBits and BitmapData, becouse application will process very big images.
Here is my code:
private void SetOpacity(Bitmap processedBitmap, int radius)
{
int opacityStep = 255 / radius;
int opacity = 0;
unsafe
{
BitmapData bitmapData = processedBitmap.LockBits(new Rectangle(0, 0, processedBitmap.Width, processedBitmap.Height), ImageLockMode.ReadWrite, processedBitmap.PixelFormat);
int bytesPerPixel = System.Drawing.Bitmap.GetPixelFormatSize(processedBitmap.PixelFormat) / 8;
int heightInPixels = bitmapData.Height;
int widthInBytes = bitmapData.Width * bytesPerPixel;
byte* ptrFirstPixel = (byte*)bitmapData.Scan0;
for (int y = 0; y < heightInPixels; y++)
{
byte* currentLine = ptrFirstPixel + (y * bitmapData.Stride);
byte* previousLine = ptrFirstPixel + ((y - 1) * bitmapData.Stride);
byte* nextLine = ptrFirstPixel + ((y + 1) * bitmapData.Stride);
for (int x = 0; x < widthInBytes; x = x + bytesPerPixel)
{
if (currentLine[x] + currentLine[x + 1] + currentLine[x + 2] == 765)
{
if (currentLine[x + bytesPerPixel] + currentLine[x + bytesPerPixel + 1] + currentLine[x + bytesPerPixel + 2] != 765)
{
currentLine[x + 3] = (byte)opacity;
}
else if (currentLine[x - bytesPerPixel] + currentLine[x - bytesPerPixel - 1] + currentLine[x - bytesPerPixel - 2] != 765)
{
currentLine[x + 3] = (byte)opacity;
}
else if (previousLine[x] + previousLine[x + 1] + previousLine[x + 2] != 765)
{
currentLine[x + 3] = (byte)opacity;
}
else if (nextLine[x] + nextLine[x + 1] + nextLine[x + 2] != 765)
{
currentLine[x + 3] = (byte)opacity;
}
}
}
}
processedBitmap.UnlockBits(bitmapData);
}
}
Slow variant of this code is:
for (int x = 0; x < processedBitmap.Width; x++)
{
for (int y = 0; y < processedBitmap.Height; y++)
{
if (processedBitmap.GetPixel(x, y) == Color.White)
{
if (processedBitmap.GetPixel(x - 1, y) != Color.White || processedBitmap.GetPixel(x + 1, y) != Color.White
|| processedBitmap.GetPixel(x, y - 1) != Color.White || processedBitmap.GetPixel(x, y + 1) != Color.White)
{
processedBitmap.SetPixel(x, y, Color.FromArgb(0, processedBitmap.GetPixel(x, y)));
}
}
}
}
What I need is to set opacity not only of pixels that meet conditions, but also pixels in some radius.
Also I need some opacity steps.
What I'm trying to do is:
for (int r = Radius; Radius > 0; r--)
{
for (int x = 0; x < processedBitmap.Width; x++)
{
for (int y = 0; y < processedBitmap.Height; y++)
{
if (processedBitmap.GetPixel(x, y) == Color.White)
{
if (processedBitmap.GetPixel(x - 1, y) != Color.White || processedBitmap.GetPixel(x + 1, y) != Color.White
|| processedBitmap.GetPixel(x, y - 1) != Color.White || processedBitmap.GetPixel(x, y + 1) != Color.White)
{
processedBitmap.SetPixel(x, y, Color.FromArgb(0, processedBitmap.GetPixel(x, y)));
for (int i = -r; i < r; i++)
{
for (int j = -r; j < r; j++)
{
if ((i * i + j * j) < (r * r))
{
processedBitmap.SetPixel(x + i, y + j, Color.FromArgb(OpacityStep * r, processedBitmap.GetPixel(x + i, y + i)));
}
}
}
}
}
}
}
}
But way faster.
Pixel format of bitmaps i use is 32bbpArgb
Thanks for any suggestions.
I am working on an asp.net project, it takes values from the front end, stores them as string in a matrix. When the matrix size is greater than 5 * 5, it keep returns an InvalidCastException (It works fine on 5 * 5 matrix and under).
The code is attached below, with a screen shot of Exception:
public partial class WebForm1 : System.Web.UI.Page
{
public DataSet relmatrix = new DataSet();
public DataTable rt = new DataTable();
public Double[] inconsistency;
public int nodersnum;
public string strrel;
protected void Button1_Click(object sender, EventArgs e)
{
nodersnum = Convert.ToInt16(count.Text);
switch (nodersnum)
{
case 1:
break;
case 2:
{ strrel = RelationAB2.Text; }
break;
case 3:
{ strrel = RelationAB3.Text + " " + RelationAC3.Text + " " + RelationBC3.Text; }
break;
case 4:
{ strrel = RelationAB4.Text + " " + RelationAC4.Text + " " + RelationAD4.Text + " " + RelationBC4.Text + " " + RelationBD4.Text + " " + RelationCD4.Text; }
break;
case 5:
{ strrel = RelationAB5.Text + " " + RelationAC5.Text + " " + RelationAD5.Text + " " + RelationAE5.Text + " " + RelationBC5.Text + " " + RelationBD5.Text + " " + RelationBE5.Text + " " + RelationCD5.Text + " " + RelationCE5.Text + " " + RelationDE5.Text; }
break;
case 6:
{ strrel = RelationAB6.Text + " " + RelationAC6.Text + " " + RelationAD6.Text + " " + RelationAE6.Text + " " + RelationAF6.Text + " " + RelationBC6.Text + " " + RelationBD6.Text + " " + RelationBE6.Text + " " + RelationBF6.Text + " " + RelationCD6.Text + " " + RelationCE6.Text + " " + RelationCF6.Text + " " + RelationDE6.Text + " " + RelationDF6.Text + " " + RelationEF6.Text; }
break;
case 7:
{ strrel = RelationAB7.Text + " " + RelationAC7.Text + " " + RelationAD7.Text + " " + RelationAE7.Text + " " + RelationAF7.Text + " " + RelationAG7.Text + " " + RelationBC7.Text + " " + RelationBD7.Text + " " + RelationBE7.Text + " " + RelationBF7.Text + " " + RelationBG7.Text + " " + RelationCD7.Text + " " + RelationCE7.Text + " " + RelationCF7.Text + " " + RelationCG7.Text + " " + RelationDE7.Text + " " + RelationDF7.Text + " " + RelationDG7.Text + " " + RelationEF7.Text + " " + RelationEG7.Text + " " + RelationFG7.Text; }
break;
default:
{ strrel = ""; }
break;
}
GenerateTable.generatetable(relmatrix, strrel, rt, nodersnum);
Class1.generatePC(nodersnum, relmatrix);
int num = 0;
double maxincon = 0.0;
int mi = 0, mj = 0, mk = 0;
inconsistency = new Double[43] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
for (int i = 0; i < nodersnum - 2; i++)
{
for (int k = i + 1; k < nodersnum - 1; k++)
{
for (int j = k + 1; j < nodersnum; j++)
{
if ((Convert.ToString(relmatrix.Tables["relTable"].Rows[i][j]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[i][k]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[k][j]) != " "))
{
Double a = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);//obtain value from the matrix
Double b = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][k]);//obtain value from the matrix
//PROBLEM
Double c = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[k][j]);//obtain value from the matrix
inconsistency[num] = (Class1.Min(System.Math.Abs(1 - a / (b * c)), System.Math.Abs(1 - (b * c) / a)));//calculate the inconsistency value and store in the inconsistency array
//Get the biggest inconsistency number
if (inconsistency[num] >= maxincon)
{
maxincon = inconsistency[num];
mi = i;
mj = j;
mk = k;
}
num++;
}
}
}
}
Class1.sort(inconsistency);//sort inconsistency array
while (inconsistency[0] > 0.3333333)
{
for (int i = 0; i < nodersnum - 2; i++)
{
for (int k = i + 1; k < nodersnum - 1; k++)
{
for (int j = k + 1; j < nodersnum; j++)
{
if ((Convert.ToString(relmatrix.Tables["relTable"].Rows[i][j]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[i][k]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[k][j]) != " "))
{
Double a = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);//obtain value from the matrix
Double b = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][k]);//obtain value from the matrix
// PROBLEM
Double c = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[k][j]);//obtain value from the matrix
if (inconsistency[0] == (Class1.Min(System.Math.Abs(1 - a / (b * c)), System.Math.Abs(1 - (b * c) / a))))//calculate the inconsistency value and store in the inconsistency array
{
if ((b * c) < a)
{
double A = (b * c) / ((a + b + c) * (a + b + c));
double B = (a + 2 * b * c) / (a + b + c);
double C = b * c - a;
double m = B * B - 4 * A * C;
if (m < 0)
{
Console.Write("error");
break;
}
else
{
double x1 = (-1 * B + System.Math.Sqrt(m)) / (2 * A);
double x2 = (-1 * B - Math.Sqrt(m)) / (2 * A);
if ((x1 > 0) && (x2 < 0))
{
b = (float)(b + (b * x1) / (a + b + c));
c = (float)(c + (c * x1) / (a + b + c));
a = (float)(a - (a * x1) / (a + b + c));
}
else if ((x1 < 0) && (x2 > 0))
{
b = (float)(b + (b * x2) / (a + b + c));
c = (float)(c + (c * x2) / (a + b + c));
a = (float)(a - (a * x2) / (a + b + c));
}
else if ((x1 > 0) && (x2 > 0))
{
double x = Class1.Min((float)x1, (float)x2);
b = (float)(b + (b * x) / (a + b + c));
c = (float)(c + (c * x) / (a + b + c));
a = (float)(a - (a * x) / (a + b + c));
}
else if ((x1 < 0) && (x2 < 0))
{
break;
}
}
}
else if ((b * c) > a)
{
double A = (b * c) / ((a + b + c) * (a + b + c));
double B = -1 * (a + 2 * b * c) / (a + b + c);
double C = b * c - a;
double m = B * B - 4 * A * C;
if (m < 0)
{
Console.Write("error");
break;
}
else
{
double x1 = (-1 * B + Math.Sqrt(m)) / (2 * A);
double x2 = (-1 * B - Math.Sqrt(m)) / (2 * A);
if ((x1 > 0) && (x2 < 0))
{
b = (float)(b - (b * x1) / (a + b + c));
c = (float)(c - (c * x1) / (a + b + c));
a = (float)(a + (a * x1) / (a + b + c));
}
else if ((x1 < 0) && (x2 > 0))
{
b = (float)(b - (b * x2) / (a + b + c));
c = (float)(c - (c * x2) / (a + b + c));
a = (float)(a + (a * x2) / (a + b + c));
}
else if ((x1 > 0) && (x2 > 0))
{
double x = Class1.Min((float)x1, (float)x2);
b = (float)(b - (b * x) / (a + b + c));
c = (float)(c - (c * x) / (a + b + c));
a = (float)(a + (a * x) / (a + b + c));
}
else if ((x1 < 0) && (x2 < 0))
{
break;
}
}
}
}
relmatrix.Tables["relTable"].Rows[i][j] = Convert.ToString(a);
relmatrix.Tables["relTable"].Rows[i][k] = Convert.ToString(b);
relmatrix.Tables["relTable"].Rows[k][j] = Convert.ToString(c);
}
}
}
}
num = 0;
maxincon = 0.0;
mi = 0;
mj = 0;
mk = 0;
for (int i = 0; i < nodersnum - 2; i++)
{
for (int k = i + 1; k < nodersnum - 1; k++)
{
for (int j = k + 1; j < nodersnum; j++)
{
if ((Convert.ToString(relmatrix.Tables["relTable"].Rows[i][j]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[i][k]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[k][j]) != " "))
{
Double a = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);//obtain value from the matrix
Double b = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][k]);//obtain value from the matrix
Double c = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[k][j]);//obtain value from the matrix
inconsistency[num] = (Class1.Min(System.Math.Abs(1 - a / (b * c)), System.Math.Abs(1 - (b * c) / a)));//calculate the inconsistency value and store in the inconsistency array
// Get the biggest inconsistency number
if (inconsistency[num] >= maxincon)
{
maxincon = inconsistency[num];
mi = i;
mj = j;
mk = k;
}
num++;
}
}
}
}
//sort inconsistency array
Class1.sort(inconsistency);
}
//Fill up the whole pairwise comparsion matrix, when row=col, the value =1, when row<col the vaule[col][row] is "1/"[col][row]
//nodersnum is how many nodes, row is the matrix row, col is column of matrix
for (int row = 0; row < nodersnum; row++)
{
for (int col = row; col < nodersnum; col++)
{
if (row < col)
{
//set the the value of lower matrix
relmatrix.Tables["relTable"].Rows[col][row] = "1/" + relmatrix.Tables["relTable"].Rows[row][col];
}
//set the value of diagnoal
else if (row == col)
{
relmatrix.Tables["relTable"].Rows[row][col] = "1";
}
}
}
//compute the weight of each element
Double[] rowproduct;
rowproduct = new Double[7] { 1, 1, 1, 1, 1, 1, 1 };
for (int i = 0; i < nodersnum; i++)
{
for (int j = 0; j < nodersnum; j++)
{
if (i >= j)
{
rowproduct[i] = rowproduct[i] / Convert.ToDouble(relmatrix.Tables["relTable"].Rows[j][i]);
}
else
{
rowproduct[i] = rowproduct[i] * Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);
}
}
}
Double[] num1;
num1 = new Double[7] { 0, 0, 0, 0, 0, 0, 0 };
double numsum = 0;
//compute each row total number(power of node number)
for (int i = 0; i < nodersnum; i++)
{
num1[i] = Math.Pow(rowproduct[i], 1 / (double)nodersnum);
numsum = numsum + num1[i];
}
//transfer into the number of percentage
Double[] weight;
weight = new Double[7] { 0, 0, 0, 0, 0, 0, 0 };
for (int i = 0; i < nodersnum; i++)
{
weight[i] = (int)Math.Round(100 * num1[i] / numsum * 100) / 100f;
Console.WriteLine(weight[i]);
}
GridView2.DataSource = relmatrix;
GridView2.DataBind();
this.GridView2.Rows[mi].Cells[mj].BackColor = System.Drawing.Color.Red;
this.GridView2.Rows[mi].Cells[mk].BackColor = System.Drawing.Color.Red;
this.GridView2.Rows[mk].Cells[mj].BackColor = System.Drawing.Color.Red;
Label3.Text = "Maximum Inconsistency after Reduction: " + Convert.ToString(inconsistency[0]);
TextBox1.Text = Convert.ToString(weight[0]);
TextBox2.Text = Convert.ToString(weight[1]);
TextBox3.Text = Convert.ToString(weight[2]);
TextBox4.Text = Convert.ToString(weight[3]);
TextBox5.Text = Convert.ToString(weight[4]);
TextBox6.Text = Convert.ToString(weight[5]);
TextBox7.Text = Convert.ToString(weight[6]);
}
}
Looks like your matrix contains null values. You cannot cast them into Double.
Check like this:
if (relmatrix.Tables["relTable"].Rows[i][k] != DBNull.Value)
Or
if (relmatrix.Tables["relTable"].Rows[i][k] != null)
Use nullable double as shown below:
var mynull = DBNull.Value;
Double? c = Convert.IsDBNull(mynull) ? (double?)null : Convert.ToDouble(mynull);
In your case:
Double? c = Convert.IsDBNull(relmatrix.tables["relTable"].Rows[k][j]) ? (double?)null : Convert.ToDouble(relmatrix.tables["relTable"].Rows[k][j]);;
Alternatively using if-else:
if(Convert.IsDBNull(relmatrix.tables["relTable"].Rows[k][j]) == true)
{
Double? c = (double?)null;
}
else
{
Double? c = Convert.ToDouble(relmatrix.tables["relTable"].Rows[k][j]);
}
I am trying to calculate the value of a single dimensional Array, here is my code:
So when I click "Detect", it should start a threshold through my Image, beginning from i = 0 to Image height and from j = 0 to Image width:
public void detektieren_Click(object sender, RoutedEventArgs e)
{
for (i = 0; i < bitmap.Height; i++)
{
for (j = 0; j < bitmap.Width; j++)
{
stride = bitmap.PixelWidth * (bitmap.Format.BitsPerPixel / 8);
data = new byte[stride * bitmap.PixelHeight];
bitmap.CopyPixels(data, stride, 0);
index = i * stride + 4 * j;
Now accessing the ARGB data:
byte A = data[index + 3];
byte R = data[index + 2];
byte G = data[index + 1];
byte B = data[index];
After the threshold, if there are any Pixels meet the condition R=0 & G=0 & B=255:
if (Convert.ToInt32(R) == 0 && Convert.ToInt32(G) == 0 && Convert.ToInt32(B) == 255)
{
// Create a writer and open the file:
StreamWriter Messdaten;
if (!File.Exists("C:/Users/.../Messdaten.csv"))
{
Messdaten = new StreamWriter("C:/Users/.../Messdaten.csv");
}
else
{
Messdaten = File.AppendText("C:/Users/.../Messdaten.csv");
}
// Write to the file:
Messdaten.WriteLine(j + ";" + i);
// Close the stream:
Messdaten.Close();
for (y = 0; y < bitmap.Height; y++)
{
for (x = 0; x < bitmap.Width; x++)
{
double x_mw = 0; double y_mw = 0;
int[] x_array = new int[(int)bitmap.Width];
int[] y_array = new int[(int)bitmap.Height];
x_array[x] = j;
x_mw = x_array.Average();
y_array[y] = i;
y_mw = y_array.Average();
xy_coord.Content = (int) x_mw + ";" + (int) y_mw;
}
}
}
}
}
}
Everything works perfectly in the CSV file, I can detect a Pixel (e.g. blue with R=0 G=0 B=255). But I also want to copy the data of each single Pixel into Array. But apparently it doesn't really deliver what I want. It doesn't calculate the average value of sum of blue Pixels (= the centroid of the blue Pixels scatter), instead it just Shows x_mw = 0 and y_mw = 0. What did I do wrong?
After I did some modification it works. So this is the code:
public void detektieren_Click(object sender, RoutedEventArgs e)
{
int x_sum = 0; int y_sum = 0; int x_count = 0; int y_count = 0; int x_mw; int y_mw;
int[] x_array = new int[(int)bitmap.Width];
int[] y_array = new int[(int)bitmap.Height];
int[] x_array_copy = new int[(int)bitmap.Width];
int[] y_array_copy = new int[(int)bitmap.Height];
stride = bitmap.PixelWidth * (bitmap.Format.BitsPerPixel / 8);
data = new byte[stride * bitmap.PixelHeight];
bitmap.CopyPixels(data, stride, 0);
for (i = 0; i < (int) bitmap.Height; i++)
{
for (j = 0; j < (int) bitmap.Width; j++)
{
index = i * stride + 4 * j;
byte A = data[index + 3];
byte R = data[index + 2];
byte G = data[index + 1];
byte B = data[index];
if (Convert.ToInt32(R) == 0 && Convert.ToInt32(G) == 0 && Convert.ToInt32(B) == 255)
{
x_array[j] = j;
x_count++;
x_array_copy[j] = x_array_copy[j] + j;
x_sum = (int) x_array_copy.Sum();
x_mw = x_sum / x_count;
y_array[i] = i;
y_count++;
y_array_copy[i] = y_array_copy[i] + i;
y_sum = (int) y_array_copy.Sum();
y_mw = y_sum / y_count;
xy_coord.Content = x_mw + ";" + y_mw;
}
}
}
}