对数组计数求和的问题 [英] Problem with summing array count

查看:135
本文介绍了对数组计数求和的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我正在尝试比较视频中的像素的黑色/白色,然后求和是否存在差异并将它们放入数组中.然后将检查总和以提供另一个输出,该输出将用作神经网络的输入神经元.

问题是当我尝试显示总和时,输出显示为0,我进行了两次检查以查看for循环背后的逻辑,并且我找不到任何错误(我知道).因此,如果有人可以帮助我解决此问题将不胜感激.
这是我的代码:

Hello,
I am trying to compare pixels from video for black/white and then sums if there exist differences and put them into an array. The total sum will then be checked to give another output which will be used as input neurons for neural network.

The problem is that when I tried displaying the sums, the output shows 0, I doubled check to see the logic behind the for loops and I couldn''t find any mistakes(which I know of)..So if anyone can help me fix this would be really appreciated.
Here is my code:

private void calculate(Bitmap bimg)
        {
            int boundA = 100;
            int boundB = 75;
            int initi = 0;
            int initj = 0;
for (int s = 0; s < differentCount.Length; s++)
{
    for (int i = initi; i < boundA; i++)
    {
        for (int j = initj; j < boundB; j++)
        {
            if (bimg.GetPixel(j, i).B.ToString() == "255"
                && bimg.GetPixel(j, i).G.ToString() == "255"
                && bimg.GetPixel(j, i).R.ToString() == "255"
                && bimg.GetPixel(j,i).A.ToString() == "255")
            {
                if (pixCompare[j, i] == 0)
                {
                    differentCount[s] += 1;
                    pixCompare[j,i] = 1;
                }
            }
            else
            {
                if (pixCompare[j, i] == 1)
                {
                    differentCount[s] += 1;
                    pixCompare[j,i] = 0;
                }
            }
            j++;
        }
        i ++;
    }
    updateText(differentCount[s].ToString());
    inputNeuron[s] = (differentCount[s] > 0) ? 1 : 0;
    if (boundB < 300)
    {
        if (boundA < 200)
        {
            boundB += 75;
            initj += 75;
        }
        else
        {
            boundB += 75;
            initj += 75;
        }
    }
    else
    {
        if (boundA < 200)
        {
            boundA += 100;
            boundB = 75;
            initi += 100;
            initj = 0;
        }
    }
}
//kohonen(inputNeuron);
initialDiffCount(


);
}


影片属性:300x200
分为8段:4x2


);
}


video properties: 300x200
split into 8 segments: 4x2

推荐答案

您确定
differentCount[s] += 1;

正在执行?您是否在该行的两个实例上都设置了断点?

你为什么一直在做字符串比较?你不知道那是多么低效吗?
而是进行数字比较...

实际上:稍微看一下您的代码:您应该使用:

Is being executed? Have you put a breakpoint on both instances of the line?

Why are you doing string compares all the time? Do you not realize how inefficient that is?
Do numeric comparisons instead...

In fact: looking at your code slightly more: should you use:

if (bimg.GetPixel(j, i).B.ToString() == "255"
    && bimg.GetPixel(j, i).B.ToString() == "255"
    && bimg.GetPixel(j, i).B.ToString() == "255")

或将其更改为

Or change it to

if (bimg.GetPixel(j, i).R.ToString() == "255"
    && bimg.GetPixel(j, i).G.ToString() == "255"
    && bimg.GetPixel(j, i).B.ToString() == "255")

不管怎样,这可能会使您的代码工作得更好一些……尤其是当您摆脱了字符串转换时.

[edit]添加了"R","G"和"B"-OriginalGriff [/edit]


[edit2]下面的文字[/edit2]


设置断点的方法有很多,但是大多数没有图片很难描述.
试试这个:
1)右键点击"differentCount [s] + = 1;"行
2)从弹出菜单中,选择断点"
3)从子菜单中,选择插入断点"
您是否在行的左侧看到一个红色圆圈?那是一个断点标记.
如果单击它,将删除断点.单击原来的位置,然后将设置一个断点(这是最快的方法-只需单击可以在断点标记处的位置).

现在,将这两行均设置为单击":

Which might make your code work slightly better anyway... especially when you get rid of the string conversions.

[edit]Added "R", "G" and "B" - OriginalGriff[/edit]


[edit2]Text below[/edit2]


There are quite a few ways to set a breakpoint, but most are hard to describe without pictures.
Try this:
1) Right click on the line "differentCount[s] += 1;"
2) From the pop up menu, select "Breakpoint"
3) From the sub menu, select "Insert BreakPoint"
Do you see to the left of your line, a red circle has appeared? That is a breakpoint marker.
If you click it, the breakpoint will be removed. Click where it was, and a breakpoint will be set (that is the quickest way - just click where a breakpoint marker can be).

Now click as set one for both of the lines:

differentCount[s] += 1;

当调试器中的执行达到断点时,执行将停止,并让您查看正在发生的事情并根据需要进行更改.非常方便!

When the execution in the debugger reaches a breakpoint, the execution stops, and lets you look at what is happening and make changes if you have to. Very handy!


1.您没有在代码中检查黑色(ARGB:255,0,0,0).
2.您仅检查"B",而不检查RGB的白色.
3.不确定为什么要跳过for循环中的备用值. (行:i + = 1;和j + = 1).如果正确,则将for循环中的i ++和j ++更改为该循环.
4.以下代码:

1. You are not checking for Black (ARGB: 255,0,0,0) in the code.
2. You are checking only "B" and not RGB for white.
3. Not sure why you are skipping alternate values in the for loops. (Lines: i += 1; and j += 1). If that is correct, change the i++ and j++ in for loop to that.
4. Following code:

if (differentCount[s] > 0)
                    inputNeuron[s] = 1;
                else
                    inputNeuron[s] = 0;


可以写成:


can be written as:

inputNeuron[s] = (differentCount[s] > 0)? 1:0;



您需要考虑这些问题,然后调试代码,看看出了什么问题.



You need to think about these points and then debug the code see what is going wrong.


OriginalGriff是正确的.在代码中使用断点以确保输入条件.

修复循环索引.为什么写i += 1j += 1? ijfor语句中已经增加.

如果可以进行int比较,请不要使用ToString.

GetPixel非常慢.如果可以摆脱它,那就更好了(请查看此链接以了解如何访问位图内部数据:
OriginalGriff is right. Use break points into your code to make sure you are entering your conditions.

Fix your loop indices. Why did you write i += 1 and j += 1? i and j are already incremented in the for statement.

Do not use ToString when int comparisons are possible.

GetPixel is VERY slow. If you can get rid of it, it would be better (have look to this link to know how to access Bitmap internal data: http://msdn.microsoft.com/fr-fr/library/5ey6h79d.aspx[^])
If you prefer to use GetPixel anyway, then do something like that:
//initialize the black and white colors at the begining of your function
int black = Color.Black.ToArgb();
int white = Color.White.ToArgb();

...

// get the pixel only once and get its colors
int pixel = bimg.GetPixel(i, j).ToArgb();
//compare the pixel to black and white
if (pixel == black)
{
    ...
}
else if (pixel == white)
{
   ...
}


这篇关于对数组计数求和的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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