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 8709697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:25:46+00:00 2026-06-13T04:25:46+00:00

For college, we have been given an assignment where, given an image, we have

  • 0

For college, we have been given an assignment where, given an image, we have to identify the “figures”, their color, and the amount of “pixel-groups” inside them. Let me explain:

enter image description here

The image above has one figure (in the image there can be multiple figures, but let us forget about that for now).

  • The background color of the canvas is the pixel at 0,0 (in this case, yellow)
  • The border color of the figure is black (it can be any color other than the canvas’ background color).
  • The figure’s background color is white (it can also be the same as the canvas’ background color).
  • A figure can only have one background color.
  • There are two pixel groups in the figure. One is a pool of blue pixels, and the other is a pool of red with some green inside. As you can see, it doesn’t matter the color of the pixel group’s pixels (it is just different than the figure’s background color). What matters is the fact that they’re in contact (even diagonally). So despite having two different colors, such group is considered as just one anyway.
  • As you can see, the border can be as irregular as you wish. It only has, however, one color.
  • It is known that a pixel group will not touch the border.
  • I was told that a pixel group’s colors can be any except the figure’s background color. I assume that then it can be the same as the figure’s border color (black).

We have been given a class capable of taking images and converting them to a matrix (each element being an integer representing the color of the pixel).

And that’s it. I’m doing it with Java.

WHAT HAVE I DONE SO FAR

  • Iterate through each pixel in the matrix
  • If I find a pixel that is different from the background color, I will assume it belongs to the border of the figure. I will call this pixel initialPixel from now on.
  • Note that the initialPixel in the image I provided is that black pixel in the top-left corner of the figure. I made a sharp cut there purposefully to illustrate it.
  • My mission now is to find the background color of the figure (in this case white).

But I’m having quite a great deal of trouble to find such background color (white). This is the closest method I did, which worked for some cases – but not with this image:

  • Since I know the color of the border, I could find the first different color that is to the south of the initialPixel. Did sound like a good idea – it did work sometimes, but it would not work with the image provided: it will return yellow in this case, since initialPixel is quite away from the figure’s contents.

Assuming I did find the figure’s background color (white), my next task would be to realize that there exist two pixel groups within the figure. This one seems easier:

  • Since I now know the figure’s background color (white), I can try iterating through each pixel within the figure and, if I find one that does not belong to the border and is not part of the figure’s background, I can already tell there is one pixel group. I can begin a recursive function to find all pixels related to such group and “flag” them so that in the future iterations I can completely ignore such pixels.

WHAT I NEED

Yes, my problem is about how to find the figure’s background color (keep in mind it can be the same as the whole image’s background color – for now it is yellow, but it can be white as well) based on what I described before.

I don’t need any code – I’m just having trouble thinking a proper algorithm for such. The fact that the border can have such weird irregular lines is killing me.

Or even better: have I been doing it wrong all along? Maybe I shouldn’t have focused so much on that initialPixel at all. Maybe a different kind of initial method would have worked? Are there any documents/examples about topics like this? I realize there is a lot of research on “computer vision” and such, but I can’t find much about this particular problem.

SOME CODE

My function to retrieve a vector with all the figures:
*Note: Figure is just a class that contains some values like the background color and the number of elements.

public Figure[] getFiguresFromImage(Image image) {
    Figure[] tempFigures = new Figure[100];
    int numberOfFigures = 0;
    matrixOfImage = image.getMatrix();
    int imageBackgroundColor = matrixOfImage[0][0];
    int pixel = 0;

    for (int y = 0; y < matrixOfImage.length; ++y) {
        for (int x = 0; x < matrixOfImage[0].length; ++x) {
            pixel = matrixOfImage[y][x];
            if (!exploredPixels[y][x]) {
                // This pixel has not been evaluated yet
                if (pixel != imageBackgroundColor ) {
                    // This pixel is different than the background color
                    // Since it is a new pixel, I assume it is the initial pixel of a new figure
                    // Get the figure based on the initial pixel found
                    tempFigures[numberOfFigures] = retrieveFigure(y,x);
                    ++numberOfFigures;
                }
            }
        }   
    }

    // ** Do some work here after getting my figures **

    return null;
}

Then, clearly, the function retrieveFigure(y,x) is what I am being unable to do.

Notes:

  • For learning purposes, I should not be using any external libraries.
  • 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-06-13T04:25:49+00:00Added an answer on June 13, 2026 at 4:25 am

    A good way to solve this problem is to treat the image as a graph, where there is one node (‘component’ in this answer) for each color filled area.

    Here is one way to implement this approach:

    1. Mark all pixels as unvisited.

    2. For each pixel, if the pixel is unvisited, perform the flood fill algorithm on it. During the flood fill mark each connected pixel as visited.

      Now you should have a list of solid color areas in your image (or ‘components’), so you just have to figure out how they are connected to each other:

    3. Find the component that has pixels adjacent to the background color component – this is your figure border. Note that you can find the background color component by finding the component with the 0,0 pixel.

    4. Now find the components with pixels adjacent to the newly found ‘figure border’ component. There will be two such components – pick the one that isn’t the background (ie that doesn’t have the 0,0 pixel). This is your figure background.

    5. To find the pixel groups, simply count the number of components with pixels adjacent to the figure background component (ignoring of course the figure border component)

    Advantages of this approach:

    • runs in O(# pixels) time.
    • easy to understand and implement.
    • doesn’t assume the background color and figure background color are different.

    To make sure you understand how iterating through the components and their neighbors might work, here’s an example pseudocode implementation for step 5:

    List<Component> allComponents; // created in step 2
    Component background; // found in step 3 (this is the component with the 0,0 pixel)
    Component figureBorder; // found in step 4
    List<Component> pixelGroups = new List<Component>(); // list of pixel groups
    
    for each Component c in allComponents:
        if c == background:
            continue;
        for each Pixel pixel in c.pixelList:
            for each Pixel neighbor in pixel.neighbors:
                if neighbor.getComponent() == figureBorder:
                    c.isPixelGroup = true;
    
    int numPixelGroups = 0;
    for each Component c in allComponents:
        if (c.isPixelGroup)
            numPixelGroups++;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on a college C assignment, and have been trying to
I have been trying to complete the following assignment for my college. So far,
For an assignment in college, we have to make a script in Perl that
I did a little GP (note:very little) work in college and have been playing
I recently redesigned my college newspaper's website and have been doing everything I can
I've studied C++ as a college course. And I have been working on it
I am a college student and have been asked to familiarize myself with PCCTS,
All my college years I have been using public , and would like to
I have been asked to setup a course leaflet system for a college. For
I have a college assignment where I need to print out items sold by

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.