优化色彩范围检查嵌套循环 [英] Optimize Color range check with nested for loop

查看:137
本文介绍了优化色彩范围检查嵌套循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道他们已经有一些优化循环的问题,但我认为这个人是有点不同。

I know their are already some "optimize for loop" questions, but I think this one is a little different.

我得到了code,从图像读取所有像素。
从每个像素我必须有RGB颜色,这也已经工作。
然后,我要检查,如果绿色是更大然后是红带蓝色,这也适用。
当绿色的是更大然后红色和蓝色,它必须做一些事情。
这所有的作品,但它是目前非常缓慢。

I got a code that reads all pixels from a image. From each pixel I must have the RGB colors, this also already works. Then I have to check if the green color is bigger then the red en blue color, this also works. And when green is bigger then red and blue, it has to do something. This all works but, it is really slow at the moment.

我也知道为什么它是因为它有做检查的数以百万计的嵌套循环缓慢,
这是我的code:

I also know why it is slow, because of the nested loops it has to do millions of checks, This is my code:

for (int j = 0; j < 200; j++){
            for (int k = 0; k < 200; k++){
                Log.i("Pixel Value", "pixel x="+j +k + Integer.toHexString(bmp.getPixel(j, k)));

                    for (cGreen = 50; cGreen < 254; cGreen++){
                        for (cRed = 0; cRed < 254; cRed++){
                            for (cBlue = 0; cBlue < 254; cBlue++){

                                if (Color.rgb(cRed, cGreen, cBlue) == bmp.getPixel(j, k)){   // magic method
                                    if ((cGreen > cRed)&&(cGreen > cBlue)){
                                        // this pixel is some sort of green
                                        aantal++;
                                    }
                                }
                            }
                        }
                    }
            }
        }

第j放大器; ķ变量是图像的大小。
和aantal是一个荷兰词,它意味着在英文的量。

the j & k variable is the size of the image. and "aantal" is a dutch word, it means "amount" in english.

时的一种方法,使这个code快(方案)?
我尝试了很多东西,但它并没有变成好。

Is their a way to make this code faster (for program)? I tried a lot of things, but it didn't turn out well.

我也试过已经做了检查是这样的:

I also tried already to do a check something like:

if (cGreen < cRed){
    // skip the rest
}

所以,当名气已经高于cGreen他可以跳过休息。
然后,它是速度快,但远离速度不够快。

So when cRed is already higher then cGreen he can skip the rest. Then it is faster, but far away from fast enough.

那么,是他们的一个聪明的办法,使code运行更快?
或其它类型的彩色检查是快了很多,或其他类型的滤镜。
希望你们能想到的东西。

So, is their a "smart" way to make this code faster to run? Or an other type of color check that is a lot faster, or an other type of "filter". Hope you guys can think of something.

已经非常感谢!

编辑:
我做了另一个跳过检查,该程序现在需要4秒对每一个像素进行检查,而不是6,但它必须是几个像素,在1秒内。

I made another skip check, the program now takes 4 secs for each pixel to check, instead of 6, but it has to be a few pixels, within 1 sec.

推荐答案

我找到了解决,在code是一个总的变化不过。

I found a fix, the code is a total change however.

bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.four_colors);
        System.out.println("START");
        int orgWidth = bmp.getWidth();
        int orgHeight = bmp.getHeight();
        //define the array size
        int[] pixels = new int[orgWidth * orgHeight];

                 bmp.getPixels(pixels, 0, orgWidth, 0, 0, orgWidth, orgHeight);

                 for (int y = 0; y < orgHeight; y++){
                     for (int x = 0; x < orgWidth; x++){
                         int index = y * orgWidth + x;
                         int R = (pixels[index] >> 16) & 0xff;     //bitwise shifting
                         int G = (pixels[index] >> 8) & 0xff;
                         int B = pixels[index] & 0xff;
                         total++;
                         if ((G > R)&&(G > B)){
                             counter++;
                             // do something
                        }
                     }
                 }

我希望这将帮助其他人了。

I hope it will help other people too.

这篇关于优化色彩范围检查嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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