Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7524321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:04:13+00:00 2026-05-30T03:04:13+00:00

I know their are already some optimize for loop questions, but I think this

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:04:16+00:00Added an answer on May 30, 2026 at 3:04 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My apologies if similar questions already exist (I know they do), but I didn't
This question has been already asked, but never answered. I want to write some
The Situation As some of you might already know from my previous questions, I'm
I know it have been discussed already at some point. But after searching there
Please let me know if this should be on Server Fault... I've got some
Git is implemented as a directed acyclic graph. Children know their parents but not
I know that systems are using their own time zone databases at different levels.
Seems that lot of people already know that issue but I can not find
I have some (log4j generated) logfiles to go through; I know their format pretty
As some of you may know, Microsoft banned memcpy() from their Security Development Lifecycle

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.