2011-04-14 143 views
2

我正在尝试编写一些代码来查看图像文件是否有相同颜色的像素组。ArrayLists堆栈溢出错误

我这样做的方式是我通过像素遍历图像(以颜色的哈希代码的1d整数数组的形式)来查找我正在搜索的颜色的像素。一旦找到一个,我做一个dfs来找到相同颜色的相邻像素,并将它们添加到一个我称为Blob的新对象中。我使用布尔数组来跟踪哪些像素已被添加,因此我不添加相同的斑点。

我使用ArrayList为每个Blob对象跟踪像素数量。然后我使用另一个Blob ArrayList来存储不同的组。

当我试图运行一个简单的例子,上半部分白色和下半部分底部的图片时,当我尝试使用太大的图片时,出现堆栈溢出错误。具体来说,当我尝试使用320x240图像进行此操作时,一旦将2752像素添加到blob,我就会获得堆栈溢出。

我只是没有使用正确的数据结构来做我想做的事情?我读过ArrayLists可以在其中存储Integer.maxValue对象。

我的代码粘贴在下面。任何帮助是极大的赞赏。

//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; 
    } 
} 

} 

回答

4

堆栈溢出错误无关,与是否使用List,或Map,或任何其他特定的数据结构。这些结构被分配在堆上。你看到你的堆栈溢出错误,因为你进行递归函数调用。每个递归函数调用都会在堆栈中分配内存。你可以增加你的-Xss值(e.g java -Xss8m HelloWorld)或者你可以重写你的算法是非递归的(假设你的算法是正确的)。

1

这看起来非常类似于flood-fill算法。递归实现可能会导致堆栈(例如,进行太多的递归调用)出现大块,这是因为您必须为每个像素探索4个邻居。最坏的情况是一个图像都在同一个blob!

我会尝试使堆栈显式。您想要避免递归,并使用简单的基于循环的方法。

public void dfs() { 
    Stack<Pixel> pixels = new Stack<Pixel>(); 
    pixels.push(currentPixel); 

    while (!pixels.isEmpty()) { 
     Pixel x = pixels.pop(); 

     // Do whatever processing on this pixel 
     Pixel upPixel = getUpPixel(); 
     if (upPixel == colorCode) { 
      pixels.push(upPixel); 
     } 

     // And so on 
    } 

} 
+0

这个问题并不涉及“每个像素4个邻居”,因为这涉及到调用树的宽度,而不是深度。对于相同颜色的1x76800图像也会发生同样的情况。 – 2011-04-15 00:09:05