循环通过图像 [英] looping through image

查看:102
本文介绍了循环通过图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

我想我的代码出了问题。



我已经将两张图片以Bitmap形式加载到图片中C#中的框可以用于处理,因为我想在y轴上完美地对齐它们。我创建了一个''for''循环来扫描第一个图像。我需要得到每个像素的值并将它们相加并对它们求平均值,这样我就可以通过找到不同y轴位置的原始图像和测试图像之间的最小差异来定位y轴的偏移。为此,我应该使用Get / SetPixel方法吗?





GetPixel函数检索指定坐标处像素的红色,绿色,蓝色(RGB)颜色值。



这些是我正在使用的.jpeg彩色图像。



这是迄今为止的代码



 命名空间 imageAlign 
{
public partial class Form1:表格
{
public Form1()
{
InitializeComponent();
}
私有 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);

// 获取像素值并将其存储在数组中,求和并对其求平均值以便它可以与image2进行比较

for int i = < span class =code-digit> 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);

// 数组中的和和平均像素值
}
}
}

}





或者我应该忘记GetPixel和只需数组总和



int sum = 0;

sum + = img1 [i,j];



感谢您的建议

解决方案

您的操作目的尚不清楚。为什么你认为通过获得一些像素统计数据,你将能够找到一些y-shift?你没有正确解释这个想法,但我怀疑这是基于一些误解。



我只能解释实施的错误。你看, GetPixel / SetPixel 非常慢。使用位图,您应该使用基于 LockBits 的快速方法:

http://msdn.microsoft.com/en-us/library/system.drawing.bitmap .lockbits.aspx [ ^ ]。



也许,你的另一个问题是使用 PictureBox 。我不知道你的目的,但滥用这种只对大多数简单任务有利的控制是常见的事情。请看我过去的答案:

如何从旧图纸中清除面板 [ ^ ],

在C#中绘制一个矩形 [ ^ ],

在图片框中附加图片 [ ^ ]。



-SA

确定。我应该提出异议,因为也许我还不够清楚。让我更详细地解释一下。



我在PictureBox1中有image_1。我想扫描一下该图像中间的矩形截面,比如x轴上约为300到500,y轴上为300到450。当我浏览这一部分时,我想要将所有像素的值相加并得到它们的平均值。



然后我在PictureBox2中有了image_2,即测试图像。这是第一张图片的副本,但PictureBox2上有一个滚动条。然后,如果我在与第一张图像相同的位置扫描第二张图像,请对该矩形内的每个像素值求和,得到它们的平均值,这些值应该与我使用image_1得到的值完全相同。 />


现在,我将使用PictureBox2上的滚动条将image_2移动到垂直,y轴稍微向上,并在与之前相同的固定位置重做扫描,加起来像素值并得到它们的平均值。这些值应该是不同的,因为我正在扫描图像的不同部分。

然后我再重复这个过程,在image_2上的y轴有不同程度的差异,以获得数值读数。



这只是为了证明一个概念。在固定的矩形位置扫描图像,求和和平均值足以最终缩小值,以便可以找到对象的开头。



是否这被认为是可能的,我想尝试一下。

请告知最简单的方法。在这个阶段没有必要尝试最快或最有效的方法,纯粹最简单的是目前最好的方式



这是我可以这样做的吗?

 命名空间 imageAlign 
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
私有 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);

// 获取像素值并将其存储在数组中,求和并对其求平均值以便它可以与image2进行比较
int sum = 0 ;
for int i = 0 ; i > 300 && i < 500 ; i ++)
{
for (< span class =code-keyword> int j = 0 ; j > < span class =code-digit> 300 && j < 450 ; i ++)
{
int [] img1 = new INT [I];
sum + = img1 [i];
MessageBox.Show(sum);
// Color pixel = img1.GetPixel(i,j);
// 数组中的和和平均像素值
}
}
}

}





谢谢大家。


你好。

我有一个在PictureBox1中打开图像的项目,没问题。但是代码底部的Public Static Color方法不起作用。我希望它能显示图像中像素的平均值。任何人都可以帮助我理解为什么它不起作用吗?谢谢。



 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing.Imaging;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;


命名空间 imageAlign
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
私有 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);
图片k = Image.FromFile(ofd1.FileName);
图形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
字体( Arial 14 ,GraphicsUnit.Point),Brushes.Black, new RectangleF( 157 237 114 65 ),sf);

g.Dispose();
k.Save( C:\\testimage.jpeg,ImageFormat。 JPEG);



图像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 颜色getDominantColor (位图myImage1)
{
// 用于计数
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;

总++;
}
}

// 计算平均值
r / =总计;
g / =总计;
b / =总计;


Console.WriteLine(j.ToString()+ + i.ToString());
return Color.FromArgb(r,g,b);
}




私人 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);
}
}

}

}


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

解决方案

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


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.


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);
            }
        }
 
    }

}


这篇关于循环通过图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆