Question : is there any way to do image processing without using get/set pixel functions in c sharp or vb.net?

I am developing a set of spatial domain filters on grayscale images ( Format8bppIndexed )
each pixel of an image has the operation of taking it's value and it's neighbors and then make some equation between that 3x3 set of pixels..
now the problem that I don't want to use get pixel, set pixel functions, because it takes too much time, I was trying to use ColorMatrix class but I am unable to get each pixel and it's neighbors..
is there any way to do this but with out using get/set pixel functions in c sharp or vb.net ?

note: I was having an idea to declare a global color matrix ( Color[,] c ) and on loading image for fist time I will use get pixel function to fill this matrix of colors, then at any time I can copy a matrix and do the operations needed on the copy, but the problem I am sucked on point of how to go back from a matrix of colors to a bitmap or image.

Answer : is there any way to do image processing without using get/set pixel functions in c sharp or vb.net?

For an 8bbp image, there is one byte per pixel. As you are using neighbouring pixels for the calculations, you have to first copy the data from the image, so that you don't overwrite the data that you are doing calculations on.

I have just finished a project to sharpen 24bbp images, I'll make an attemt to convert it to 8bpp:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
int center = 16;
int split = 4;
int x0 = rect.Left;
int y0 = rect.Top;
int w = rect.Width;
int h = rect.Height;
 
Rectangle rect = new Rectangle(0, 0, w, h);
 
BitmapData info = new BitmapData();
image.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed, info);
try {
 
	// copy original data
 
	byte[] g = new byte[w * h];
	int ofs = 0;
	int gap = info.Stride - w; // gap between lines
	byte* p = (byte*)info.Scan0;
	for (int y = h; y > 0; y--) {
		// read pixels
		for (int x = w; x > 0; x--) {
			g[ofs] = *(p++);
		}
		// move to next line
		p += gap;
	}
 
	// sharpen
 
	p = (byte*)info.Scan0;
	ofs = 0;
 
	// first line - just copy
	for (int x = w; x > 0; x--) {
		*(p++) = g[ofs++];
	}
	// move to next line
	p += gap;
 
	// middle lines
	for (int y = h - 2; y > 0; y--) {
 
		// first pixel - just copy
		*(p++) = g[ofs++];
 
		// middle lixels
		for (int x = w - 2; x > 0; x--) {
			int gray = (center * g[ofs] - g[ofs + w + 1] - 2 * g[ofs - w] - g[ofs - w + 1] - 2 * g[ofs - 1] - 2 * g[ofs + 1] - g[ofs + w - 1] - 2 * g[ofs + w] - g[ofs - w - 1]) / split;
			*(p++) = (byte)Math.Max(Math.Min(gray, 255), 0);
			ofs++;
		}
 
		// last pixel - just copy
		*(p++) = g[ofs++];
 
		// move to next line
		p += gap;
 
	}
 
	// last line - just copy
	for (int x = w; x > 0; x--) {
		*(p++) = g[ofs++];
	}
 
} finally {
	image.UnlockBits(info);
}
Open in New Window Select All
Random Solutions  
 
programming4us programming4us