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

  • Home
  • SEARCH
  • 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 4272918
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:38:34+00:00 2026-05-21T07:38:34+00:00

I’m trying to write some code to look through an image file for groups

  • 0

I’m trying to write some code to look through an image file for groups of pixels that are the same color.

The way I do this is I iterate through the image (in the form of a 1d integer array with the color’s hash code) by pixel to find a pixel of the color that I’m searching for. Once one is found, I do a dfs to find adjacent pixels of the same color and add them to a new object I called a Blob. I use a boolean array to keep track of which pixels have already been added so I don’t add identical blobs.

I’m using ArrayList for each Blob object to keep track of the pixel numbers. And then I use another ArrayList of Blobs to store the different groups.

When I try to run this on a simple example, a picture with the top half white and the bottom half bottom, I get a stack overflow error when I try to use a picture that’s too large. Specifically, when I try to do this with a 320×240 image, I get the stackoverflow once 2752 pixels are added to the blob.

Am I just not using the right data structure for what I want to do? I read that ArrayLists can store Integer.maxValue objects in them.

My code is pasted below. Any help is greatly appreciated.

//blobfind tests code to find similar pixels of a minimum size and groups them together for analysis later  
//purpose is to identify color coded objects through the webcam  

//util for ArrayList  
import java.util.*;  
import java.awt.Color;  
import java.io.*;  

public class Blobfind2 {  

  //width and height of image in pixels  
  private int width;  
  private int height;  
  //hash code for the color being searched for  
  private int colorCode;  
  //minimum blob size to be added  
  private int minPixels;  
  //image in form of array of hashcodes for each pixel  
  private int[] img;  
  //keeping track of which pixels have been added to a blob  
  private boolean[] added;  
  //keeping track of which pixels have been visited when looking for a new blob  
  private boolean[] visited;  

  //debugging variable  
  private int count;  

  public Blobfind2(int inwidth, int inheight, int inCCode, int inminPixels, int[] inimage)   {
    width = inwidth;  
    height = inheight;  
    colorCode = inCCode;  
    minPixels = inminPixels;  
    img = inimage;  
    count = 0;  
  }    

  //takes hashCodeof color, minimum pixel number, and an image in the form of integer array  
  public ArrayList findColor() {  
    //makes an arraylist of "blobs"  
    ArrayList bloblist = new ArrayList();  
    //keep track of which pixels have been added to a blob  
    boolean[] added = new boolean[width * height];  
    //checks through each pixel  
    for (int i = 0; i < img.length; i++) {  
      //if it matches and is not part of a blob, we run dfs to collect all the pixels in   that blob
      if ((img[i] == colorCode) && (added[i] == false)) {  
        //visited keeps track of which pixels in the blob have been visited  
        //refreshed each time a new blob is made  
        boolean[] visited = new boolean[width*height];  
        Blob currBlob = new Blob();  
        dfs(img, currBlob, i, Color.white.hashCode(), added, visited);  
        //adds the blob to the bloblist if it is of a certain size  
        if (currBlob.mass() >= minPixels) {  
          bloblist.add(currBlob);                          
        }  
      }  
    }  
    return bloblist;  
  }  

  //recursive algorithm to find other members of a blob  
  public void dfs (int[] img, Blob blob, int currPixel, int colorCode, boolean[] added,    boolean[] visited) {  
    //System.out.print(currPixel + " - " + count + " ");   
    count++;  
    //check current pixel, this only happens on the first pixel  
    if (visited[currPixel] == false) {  
      blob.add(img[currPixel]);  
      added[currPixel] = true;  
      visited[currPixel] = true;  
    }  
    //checks down pixel  
    if ((currPixel + width < height*width) && (visited[currPixel + width] == false)) {  
      if (img[currPixel + width] == colorCode) {  
        blob.add(img[currPixel + width]);  
        currPixel = currPixel + width;  
        added[currPixel] = true;  
        visited[currPixel] = true;  
        dfs(img, blob, currPixel, colorCode, added, visited);  
      }  
    }  
    //checks up pixel  
    if ((currPixel - width > 0) && (visited[currPixel - width] == false)) {  
      if (img[currPixel - width] == colorCode) {  
        blob.add(img[currPixel - width]);  
        currPixel = currPixel - width;  
        added[currPixel] = true;  
        visited[currPixel] = true;  
        dfs (img, blob, currPixel, colorCode, added, visited);  
      }  
    }  
    //checks right pixel  
    if ((currPixel + 1 < width * height) && (visited[currPixel + 1] == false) && (((currPixel + 1) % width) != 0)) {  
      if (img[currPixel + 1] == colorCode) {  
        blob.add(img[currPixel + 1]);  
        currPixel = currPixel + 1;  
        added[currPixel] = true;  
        visited[currPixel] = true;  
        dfs(img, blob, currPixel, colorCode, added, visited);  
      }  
    }  
    //checks left pixel  
    if ((currPixel - 1 > 0) && (visited[currPixel - 1] == false) && (((currPixel - 1) % width) != width - 1)) {  
      if (img[currPixel - 1] == colorCode) {  
        blob.add(img[currPixel - 1]);  
        currPixel = currPixel - 1;  
        added[currPixel] = true;  
        visited[currPixel] = true;  
        dfs(img, blob, currPixel, colorCode, added, visited);  
      }  
    }  
    return;  
  }  

  //test case, makes a new image thats half black and half white  
  //should only return one blob of size width*height/2  
  public static void main(String[] args) {  
   int width = 320;  
   int height = 240;  
   //builds the image  
   int[] img = new int[width * height];  
   for (int i = 0; i < img.length; i++) {  
     if (i < img.length/4) {  
       img[i] = Color.white.hashCode();  
     } else {  
       img[i] = Color.black.hashCode();  
     }  
   }  

   //runs blobfind  
   Blobfind2 bf = new Blobfind2(width, height, Color.white.hashCode(), 1, img);  
   ArrayList bloblist = bf.findColor();  
   System.out.println(bloblist.size());  
   //need to typecast things coming out of arraylists  
   Blob firstblob = (Blob)bloblist.get(0);  
   System.out.println(firstblob.mass());  
  }  

 private class Blob {  
   private ArrayList pixels = new ArrayList();  
   private Blob() {  
   }  
   private int mass() {  
     return pixels.size();  
   }  
   private void add(int i) {  
     pixels.add(i);  
   }  
   private ArrayList getBlob() {  
     return pixels;  
   }  
 }  

}   
  • 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-21T07:38:34+00:00Added an answer on May 21, 2026 at 7:38 am

    The stack overflow error has nothing to do with whether you use an List, or a Map, or any other particular data structure. Those constructs are allocated on the heap. You are seeing your stack overflow error because you make recursive function calls. Each recursive function call allocates memory on the stack. You can increase your -Xss value (e.g java -Xss8m HelloWorld) or you can re-write your algorithm to be non-recursive (assuming your algorithm is correct).

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

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.