在C#中迭代填充数组列表 [英] iteratively filling array list in C#
问题描述
嗨我想迭代填写c#中的arraylist。实际上我正在进行图像处理中的小程序(去除图像中的噪点),其中我有大小为3 * 3的掩码,这意味着它将收集图像上的每个像素9值。例如,考虑我们有10 * 10大小的图像,所以总数没有。像素将为100,现在对于每个像素,它将收集其9个邻居值并将其填入数组列表中。代码是
Hi I want to fill arraylist in c# iteratively.actually I am doing small program in image processing (to remove noise from image) in which I have mask of size 3*3 that mean for each pixel on image it will collect 9 values. for example consider we have image of size 10*10 so total no. of pixels would be 100, now for each pixel it will collect its 9 neighbor value and fill it in array list. code is
for (int ii = 0; ii < img.Width; ii++)
{
for (int jj = 0; jj < img.Height; jj++)
{
if (ii - 1 >= 0 && jj - 1 >= 0)
{
c = img.GetPixel(ii - 1, jj - 1);
mask[0] = Convert.ToInt16(c.R);
}
else
{
mask[0] = 0;
}
if (jj - 1 >= 0 && ii + 1 < img.Width)
{
c = img.GetPixel(ii + 1, jj - 1);
mask[1] = Convert.ToInt16(c.R);
}
else
mask[1] = 0;
if (jj - 1 >= 0)
{
c = img.GetPixel(ii, jj - 1);
mask[2] = Convert.ToInt16(c.R);
}
else
mask[2] = 0;
if (ii + 1 < img.Width)
{
c = img.GetPixel(ii + 1, jj);
mask[3] = Convert.ToInt16(c.R);
}
else
mask[3] = 0;
if (ii - 1 >= 0)
{
c = img.GetPixel(ii - 1, jj);
mask[4] = Convert.ToInt16(c.R);
}
else
mask[4] = 0;
if (ii - 1 >= 0 && jj + 1 < img.Height)
{
c = img.GetPixel(ii - 1, jj + 1);
mask[5] = Convert.ToInt16(c.R);
}
else
mask[5] = 0;
if (jj + 1 < img.Height)
{
c = img.GetPixel(ii, jj + 1);
mask[6] = Convert.ToInt16(c.R);
}
else
mask[6] = 0;
if (ii + 1 < img.Width && jj + 1 < img.Height)
{
c = img.GetPixel(ii + 1, jj + 1);
mask[7] = Convert.ToInt16(c.R);
}
else
mask[7] = 0;
c = img.GetPixel(ii, jj);
mask[8] = Convert.ToInt16(c.R);
}
double[][] collection_input ={
new double[9]{mask[0],mask[1],mask[2],mask[3],mask[4],mask[5],mask[6],mask[7],mask[8]}
};
这里我想要collection_输入包含每个像素的9个值,在这种情况下,每个像素的总值为9将是9个值。如果你知道这个,请帮助我
添加了代码块 - OriginalGriff [/ edit]
here I want collection_ input contain 9 value for each pixel in this case it would be 9 value for each pixel total would be 900. plz help me if u know this
[edit]Code block added - OriginalGriff[/edit]
推荐答案
如果我理解正确,
你可以使用通用列表(请参阅此示例),其中通用列表中的每个对象可以是9个元素的数组或二维的数组3X3。
在迭代过程中,使用表示所需9个值的对象填充通用列表。
[请参阅此代码项目文章,了解一个简单的通用示例,以获取事物的内容]
简单如此...
Lemme知道您是否需要澄清,或者您认为我不理解您的问题。
If I understand you correctly,
you can use a Generic List (see this e.g.), where each object in the generic list can be a 9-elements array or a two dimensional array 3X3.
As you go along the iteration fill the Generic List with objects that represent the 9 values you need.
[See this Code Project article for a simple generic example to get the hang of things]
Simple as that...
Lemme know if you need clarification, or if you think I didn''t understand your question.
这篇关于在C#中迭代填充数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!