Note that there are some explanatory texts on larger screens.

plurals
  1. POEdge Detection with Lockbits C#
    primarykey
    data
    text
    <p>i make a program that implements an edge detection algorithm. but, it takes a long time for the process. i've read about use lockbits and unsafe state instead of getpixel and setpixel, but i still understand how to use it.</p> <p>this is my example code :</p> <pre><code>private Bitmap SobelEdgeDetect(Bitmap original) { Bitmap b = original; Bitmap bb = original; int width = b.Width; int height = b.Height; int[,] gx = new int[,] { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } }; int[,] gy = new int[,] { { 1, 2, 1 }, { 0, 0, 0 }, { -1, -2, -1 } }; int[,] allPixR = new int[width, height]; int[,] allPixG = new int[width, height]; int[,] allPixB = new int[width, height]; int limit = 128 * 128; for (int i = 0; i &lt; width; i++) { for (int j = 0; j &lt; height; j++) { allPixR[i, j] = b.GetPixel(i, j).R; allPixG[i, j] = b.GetPixel(i, j).G; allPixB[i, j] = b.GetPixel(i, j).B; } } int new_rx = 0, new_ry = 0; int new_gx = 0, new_gy = 0; int new_bx = 0, new_by = 0; int rc, gc, bc; for (int i = 1; i &lt; b.Width - 1; i++) { for (int j = 1; j &lt; b.Height - 1; j++) { new_rx = 0; new_ry = 0; new_gx = 0; new_gy = 0; new_bx = 0; new_by = 0; rc = 0; gc = 0; bc = 0; for (int wi = -1; wi &lt; 2; wi++) { for (int hw = -1; hw &lt; 2; hw++) { rc = allPixR[i + hw, j + wi]; new_rx += gx[wi + 1, hw + 1] * rc; new_ry += gy[wi + 1, hw + 1] * rc; gc = allPixG[i + hw, j + wi]; new_gx += gx[wi + 1, hw + 1] * gc; new_gy += gy[wi + 1, hw + 1] * gc; bc = allPixB[i + hw, j + wi]; new_bx += gx[wi + 1, hw + 1] * bc; new_by += gy[wi + 1, hw + 1] * bc; } } if (new_rx * new_rx + new_ry * new_ry &gt; limit || new_gx * new_gx + new_gy * new_gy &gt; limit || new_bx * new_bx + new_by * new_by &gt; limit) bb.SetPixel(i, j, Color.Black); else bb.SetPixel(i, j, Color.Transparent); } } return bb; } </code></pre> <p>i am using fastbitmap class, and i implement it like this :</p> <pre><code>private Bitmap SobelEdgeDetectTwo(Bitmap original) { int width = original.Width; int height = original.Height; Bitmap result = new Bitmap(width,height); FastBitmap b = new FastBitmap(original); FastBitmap bb = new FastBitmap(result); b.LockBitmap(); bb.LockBitmap(); int[,] gx = new int[,] { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } }; int[,] gy = new int[,] { { 1, 2, 1 }, { 0, 0, 0 }, { -1, -2, -1 } }; int[,] allPixR = new int[width, height]; int[,] allPixG = new int[width, height]; int[,] allPixB = new int[width, height]; int limit = 128 * 128; for (int i = 0; i &lt; width; i++) { for (int j = 0; j &lt; height; j++) { var pixel = b.GetPixel(i,j); allPixR[i, j] = pixel.Red; allPixG[i, j] = pixel.Green; allPixB[i, j] = pixel.Blue; } } int new_rx = 0, new_ry = 0; int new_gx = 0, new_gy = 0; int new_bx = 0, new_by = 0; int rc, gc, bc; for (int i = 1; i &lt; width - 1; i++) { for (int j = 1; j &lt; height - 1; j++) { new_rx = 0; new_ry = 0; new_gx = 0; new_gy = 0; new_bx = 0; new_by = 0; rc = 0; gc = 0; bc = 0; for (int wi = -1; wi &lt; 2; wi++) { for (int hw = -1; hw &lt; 2; hw++) { rc = allPixR[i + hw, j + wi]; new_rx += gx[wi + 1, hw + 1] * rc; new_ry += gy[wi + 1, hw + 1] * rc; gc = allPixG[i + hw, j + wi]; new_gx += gx[wi + 1, hw + 1] * gc; new_gy += gy[wi + 1, hw + 1] * gc; bc = allPixB[i + hw, j + wi]; new_bx += gx[wi + 1, hw + 1] * bc; new_by += gy[wi + 1, hw + 1] * bc; } } if (new_rx * new_rx + new_ry * new_ry &gt; limit || new_gx * new_gx + new_gy * new_gy &gt; limit || new_bx * new_bx + new_by * new_by &gt; limit) { PixelData p = new PixelData(Color.Black); bb.SetPixel(i, j, p); } else { PixelData p = new PixelData(Color.Transparent); bb.SetPixel(i, j, p); } } } b.UnlockBitmap(); bb.UnlockBitmap(); return result; } </code></pre> <p>but, the image doesn't change at all. could you give an advice to me about which part of my code that was mistake?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload