Click here to Skip to main content
12,354,574 members (58,835 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Hi.
I think I have gone very wrong with my code.

I have loaded two images in Bitmap form into picture boxes in C# which can be used for processing as I want to align them on the y-axis perfectly. I have created a 'for' loop to scan through the first image. I need to get the values of every pixel and sum them up and average them so I can locate the shift in the y-axis by finding the minimum difference between the original and test images of different y-axis locations. To do this, should I be using the Get / SetPixel methods?


The GetPixel function retrieves the red, green, blue (RGB) color value of the pixel at the specified coordinates.

These are .jpeg color images I am working with.

Here is the code so far

namespace imageAlign
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        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
                       
                         for (int i = 0; i < Image.Width; i++)
                         {
                             for (int j = 0; j < Image.Height; i++)
                            {
                                int[] img1 = new int[i,j];
                                Color pixel = img.GetPixel(i,j);
                                 
                                 //sum and average pixel values in array
                    }
                }
            }
 
        }

Or should I forget about the GetPixel and just sum the array

int sum = 0;
sum += img1[i,j];

Thanks for advice
Posted 3-Feb-13 2:05am
Edited 3-Feb-13 5:37am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

The purpose of your operation is unclear. Why do you think that by getting some pixel statistics, you will be able to find some y-shift? You did not properly explain the idea, but I suspect it's based on some misconception.

I just can explain what's wrong with implementation. You see, GetPixel/SetPixel is prohibitively slow. With Bitmap, you should use fast approach based on LockBits instead:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits.aspx[^].

Maybe, your other problem is using PictureBox. I don't know your purpose, but abusing this control which is only good for most simple tasks, is the usual thing. Please see my past answers:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^].

—SA
  Permalink  
Comments
Abhinav S 3-Feb-13 23:19pm
   
My 5. Someone downvoted me and without and explanation. ugly.
   
Thank you, Abhinav.
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

ok. I should apoplogise, as perhaps I haven't been clear enough. Let me explain in better detail.

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?
namespace imageAlign
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        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];
                                MessageBox.Show(sum);
                                 //Color pixel = img1.GetPixel(i,j);
                                 //sum and average pixel values in array
                    }
                }
            }
 
        }

Thank you all.
  Permalink  
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

Hi.
I have a project which opens an image in PictureBox1, no problem. But the Public Static Color method at the bottom of the code is not working. I was hoping it would display the average value of the pixels in the image. Can anybody help me to understand why it is not working please? Thank you.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing.Imaging;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 

namespace imageAlign
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        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);
                Image k = Image.FromFile(ofd1.FileName);
                Graphics g = Graphics.FromImage(k);
                g.FillRectangle(Brushes.White, 155, 235, 120, 70);
                g.DrawRectangle(Pens.YellowGreen, 155, 235, 120, 70);
                StringFormat sf = (StringFormat)StringFormat.GenericTypographic.Clone();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
 
                g.DrawString(DateTime.Now.ToShortDateString(), new
                Font("Arial", 14, GraphicsUnit.Point), Brushes.Black, new RectangleF(157, 237, 114, 65), sf);
 
                g.Dispose();
                k.Save("C:\\testimage.jpeg", ImageFormat.Jpeg);
 

 
                Image image_rect = Image.FromFile("C:\\testimage.jpeg");
                pictureBox3.Image = image_rect;
                //pictureBox3.Height = image_rect.Height;
                //pictureBox3.Width = image_rect.Width;

                k.Dispose();
            }
        }
 
             public static Color getDominantColor(Bitmap myImage1)
             {
                  //Used for tally
            int i = 0;
            int j = 0;
            int r = 0;
            int g = 0;
            int b = 0;
 
            int total = 0;
 
            for (i = 0; i < myImage1.Width; i++)
            {
                for (j = 0; j < myImage1.Height; j++)
                {
                    Color clr = myImage1.GetPixel(i, j);
 
                    r += clr.R;
                    g += clr.G;
                    b += clr.B;
 
                    total++;
                }
            }
 
            //Calculate average
            r /= total;
            g /= total;
            b /= total;
 
            
            Console.WriteLine(j.ToString() + " " + i.ToString());
            return Color.FromArgb(r, g, b);
            }
    
 

           
        private void button2_Click(object sender, EventArgs e)
        {
            Bitmap myImage2 = (Bitmap)pictureBox2.Image;
            OpenFileDialog ofd2 = new OpenFileDialog();
            if (ofd2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                pictureBox2.Image = Image.FromFile(ofd2.FileName);
            }
        }
 
    }
 
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You will not be able to compare the colors if you are not going to use GetPixel to get the color values.
  Permalink  
Comments
   
Abhinav,
Sorry for disturbing you by not explaining my vote immediately; I've been interrupted...
The thing is: this statement is something which is not true. In fact, to get or compared colors, using this function is not required at all. One can compare colors by considering bit maps directly; and this is a preferred method because of performance: did you ever try to call this method many times, to read many pixels. If you said that I would still need to read pixels, I would not agree: first, not always (imaging you compare only read component or only alpha), second, you are not talking about reading pixels; you are talking about GetPixel method specifically.

So, not true is not true. Sorry,
—SA
Abhinav S 4-Feb-13 0:53am
   
How would compare bitmaps directly?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160621.1 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100