[英]Stack Overflow error with ArrayLists
我正在嘗試編寫一些代碼以通過圖像文件查找具有相同顏色的像素組。
我這樣做的方法是逐像素遍歷圖像(以帶有顏色哈希碼的1d整數數組的形式),以查找要搜索的顏色的像素。 找到一個像素后,我將執行dfs來查找相同顏色的相鄰像素,並將其添加到一個稱為Blob的新對象中。 我使用布爾數組來跟蹤已經添加了哪些像素,因此我不會添加相同的斑點。
我為每個Blob對象使用ArrayList來跟蹤像素數。 然后,我使用另一個Blob的ArrayList來存儲不同的組。
當我嘗試在一個簡單的示例(具有上半部白色和下半部底部的圖片)上運行時,當嘗試使用太大的圖片時出現堆棧溢出錯誤。 具體來說,當我嘗試使用320x240圖像執行此操作時,一旦將2752像素添加到Blob中,就會出現stackoverflow。
我只是在為自己想做的事情使用正確的數據結構嗎? 我讀到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;
}
}
}
堆棧溢出錯誤與使用List
, Map
或任何其他特定數據結構無關。 這些結構在堆上分配。 您正在看到堆棧溢出錯誤,因為您進行了遞歸函數調用。 每個遞歸函數調用都會在堆棧上分配內存。 您可以增加-Xss的值(eg java -Xss8m HelloWorld)
,也可以將算法重新編寫為非遞歸的(假設您的算法正確)。
這看起來與洪水填充算法非常相似。 遞歸實現可能會使大型Blob崩潰(例如,進行過多的遞歸調用),僅僅是因為您必須為每個像素探索4個鄰居。 最壞的情況是圖像全部在同一斑點中!
我會嘗試使堆棧明確。 您要避免遞歸,而應使用基於循環的簡單方法。
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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.