I know their are already some “optimize for loop” questions, but I think this one is a little different.
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.
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++;
}
}
}
}
}
}
}
the j & k variable is the size of the image.
and “aantal” is a dutch word, it means “amount” in english.
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
}
So when cRed is already higher then cGreen he can skip the rest.
Then it is faster, but far away from fast enough.
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.
Already thanks!
Edit:
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.
I found a fix, the code is a total change however.
I hope it will help other people too.