February 4th, 2013, 09:57 AM
Loop and add pixel values c#
I have image_1 in PictureBox1. I want to scan through a rectangular section in the middle of this image, say about 300 to 500 on the x-axis and 300 to 450 on the y-axis. When I scan through this section I want to add up the values of all the pixels and get an average value for them.
I then have image_2 in PictureBox2, the test image. This is a copy of the first image but the PictureBox2 has a scroll bar on it. If I then scan through the second image at the same locations as I did with the first image, sum every pixel value within that rectangle and get an average of them the values should be exactly the same as the value I got with image_1.
Now, I will move image_2 with the scroll bar on PictureBox2 slightly up in the vertical, y-axis, and redo the scan in the same fixed locations as previously, add up the pixel values and get their average. These values should be different because I am scanning a different section of the image.
I then repeat this process some more with varying degrees of difference in the y-axis on image_2 to get value readings.
This is to prove a concept only. That scanning the images at a fixed rectangular location, summing and averaging values is enough to eventually narrow down the values so that the beginning of an object can be located.
Whether this is thought to be possible or not I would like to attempt it.
Please advise the simplest method. It is not necessary at this stage to attempt the fastest or most efficient method, purely the simplest is best at present
Is it something I can simply do like this?
Collapse | Copy Code
public partial class Form1 : Form
private void button1_Click(object sender, EventArgs e)
Bitmap myImage1 = (Bitmap)pictureBox1.Image;
OpenFileDialog ofd1 = new OpenFileDialog();
if (ofd1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
pictureBox1.Image = Image.FromFile(ofd1.FileName);
// get pixel value and store it in array, sum and average it so it can Be compared to image2
int sum = 0;
for (int i = 0; i > 300 && i < 500; i++)
for (int j = 0; j > 300 && j < 450; i++)
int img1 = new int[i];
sum += img1[i];
//Color pixel = img1.GetPixel(i,j);
//sum and average pixel values in array
Thank you all.