PDA

View Full Version : Calculate each pixel RGB (C#)



lichtenstein
December 6, 2010, 08:38:33
As the title. i want to ask about how to calculate each pixel in image buffer and find the RGB for each pixel then take average for each RGB. i already try to understand pixel format but still don't get it how to calculate all the pixel. please help with the code :)

thanks

regards

lichtenstein

Stefan Geissler
December 6, 2010, 10:44:09
Lichtenstein,

I must admit, I do not understand what you want. Can you please give more details?

lichtenstein
December 6, 2010, 11:27:13
in the PixelFormat example they just output the RGB value for pixel 1 and 2 only. and now i want output RGB from all of the pixel. please help with the code :-(

forget one thing. the format that i want to calculate is RGB24 and RGB32

Stefan Geissler
December 7, 2010, 09:07:17
Array handling is basic knowledge of programming. If you use a twodimensional array you may have an x and an y varialbe for row and column.
Thus you may write

buf[x * 3 + 2, y] = 255;
buf[x * 3 + 1, y] = 0;
buf[x * 3 + 0, y] = 0;

instead of

buf[0 * 3 + 2, y] = 255;
buf[0 * 3 + 1, y] = 0;
buf[0 * 3 + 0, y] = 0;

where x >= 0 and x < buf.PixelPerLine and y >= 0 and y < buf.Lines.

lichtenstein
December 7, 2010, 17:37:25
i'm sorry stefan. but i already try the code that u give. still dont get it :(

this is my code. hope u can help fix



int x = buf.PixelPerLine;
int y = buf.Lines;

int[,] array = new int[640, 480];

for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
txtOutput.Text = "Output all of the pixel\r\n";
txtOutput.Text += "R=" + buf[x * 3 + 2, y] + ", ";
txtOutput.Text += "G=" + buf[x * 3 + 1, y] + ", ";
txtOutput.Text += "B=" + buf[x * 3 + 0, y] + "\r\n";
}


could you help me please with the code. i'm really weak in programming, but i want learn it..

Stefan Geissler
December 8, 2010, 09:24:45
Well,

please have a look at your code. Check, how the variables x and y change as indexes for your arrays. (They do not.)




nt x = buf.PixelPerLine;
int y = buf.Lines;

int[,] array = new int[640, 480];

for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
txtOutput.Text = "Output all of the pixel\r\n";
txtOutput.Text += "R=" + buf[i * 3 + 2, j] + ", ";
txtOutput.Text += "G=" + buf[i * 3 + 1, j] + ", ";
txtOutput.Text += "B=" + buf[i * 3 + 0, j] + "\r\n";
}

lichtenstein
December 9, 2010, 02:57:16
ok i get it now. thank you very much stefan :)