2017-09-13 20 views
-1

我想知道第31行上loc位置的公式,但我无法弄清楚。任何人都可以指导我解决问题吗?处理:由于嵌套循环而在阵列中定义位置时遇到的困难

PImage theImage; 
int cellSize = 6; // dimension of a cell where logic is applied 
int cols, rows; // number of rows and columns based on cellsize 
int col; // column in the cell 
int row; // row in the cell 
float edgeR; // (amount of red in the corners) 
float edgeB; // (amount of blue in the corners) 
float edgeG; // (amount of green in the corners) 
float middleR; // (amount of red in the center) 
float middleB; // (amount of blue in the center) 
float middleG; // (amount of green in the center) 

void setup() 
{ 
    size(1570,1112); // fits the image 
    theImage = loadImage("im.png"); // load the image 
    cols = width/cellSize ; // number of colums in the grid of the image 
    rows = height/cellSize ; // number of rows in the grid of the image 
} 

void draw() 
{ 
    background(0); 
    loadPixels(); 
    theImage.loadPixels(); // load every pixel of the image in an array 
    image(theImage,0,0); // display the image at pos 0 0 
    for (int i=0; i< rows;i++) { 
    for (int j = 0; j< cols;j++) { // loop matrix of cells   
     for (int x = i*cellSize; x<i*cellSize+cellSize;x++) { 
     for (int y = j*cellSize; y<j*cellSize+cellSize;y++) { // 
      loop every pixel of the cell 

      ---> int loc = (i*cellSize+x) + 
      (j*cellSize*width+y*cellSize*width); // grrrrr <----- 
       float r = red(theImage.pixels[loc]); // red value 
       float g = green(theImage.pixels[loc]); // green value 
       float b = blue(theImage.pixels[loc]); // blue value    
       if (x - i*cellSize <= cellSize /3) { // it is in a column 1 
      col = 1; 
      } 
      else if (x - i*cellSize >= cellSize*2/3) { // it is in a column 3 
      col = 3; 
      } 
      else { 
      col = 2; // it is in a column 2 
      } 
      if (y - j*cellSize <= cellSize /3) { // it is in row 1 
      row = 1; 
      } 

      else if (y - j*cellSize >= cellSize*2/3) { // it is in row 3 
      row = 3; 
      } 
      else { // in row 2 
      row = 2; 
      }      
      if (col == 1 & row ==1 || (col == 3 & row == 3) || (col == 1 & row == 3) || (col == 3 & row == 1)) { 
       edgeR = edgeR + r; // aggregate the edge color values 
       edgeG = edgeG + g; 
       edgeB = edgeB + b;    
      } 
     else { 
       middleR = middleR + r; // aggregate the center color values 
       middleG = middleG + g; 
       middleB = middleB + b; 

      // here some magic will be applied if there would be no ArrayIndexOutOfBoundsException: 1745840 error 

     }  
    }  
    } 
} 
} 
} 

当我运行代码的输出窗口冻结,我得到一个ArrayIndexOutOfBoundsException:1752120错误/如果我把调试就这还要不停地说:调试忙。

我敢肯定禄位置不正确,但我没有一个知道如何解决公式。此外,嵌套for循环也可能是问题。

非常感谢您的帮助,非常感谢。

一切顺利,

+0

四重嵌套循环...有什么可能出错! :)严肃地说,注释掉除最外层循环以外的所有内容,并且放入一些调试打印语句来测试你期望的循环边界。然后每次添加一个循环来诊断问题的真实位置。 – Jeremy

+0

谢谢Jeremy,但我的诊断是在第31行,我尝试给r赋值,根据位置loc,因为loc是计算loc的公式不正确,它会得到一个worong值并导致错误。或者你的意思是说这个诊断是不正确的? –

+1

要与下面的答案对话...从较小的图像开始。比如5 x 5或10 x 10 ...可调试的东西。使用纸或excel来计算您的期望值的上限,然后验证您的公式是否给您预期的结果。 – Jeremy

回答

-1

这是太多的代码来问我们来调试你。

你需要break your problem down into smaller steps,并做一些debugging隔离您的问题。请注意,调试的第一步是用一张纸和一支铅笔遍历代码,而不是调试器本身!

你必须能够回答这些类型的问题:当你的错误是什么每一个变量的值? i,j,xy有什么值? loc有什么价值? theImage.pixels有多少个索引?

请注意,我不是要求你告诉我这些问题的答案。你需要自己回答,所以你可以开始找出哪一行代码不符合你的预期。

如果您仍然无法弄清楚,你需要缩小您的问题降到MCVE

而且,只是一个侧面说明:你不应该写这样整个程序,然后只测试它,当它完成。你需要在更小的块中工作,并确保块在继续前正常工作。在你的例子中,你可能已经开始了最外层的for循环,并确保迭代你期望的值。然后第二个for循环等。你应该也可能重构你的代码,所以你没有四重嵌套的for循环。

0

我改变

int loc = (i*cellSize+x) + 
     (j*cellSize*width+y*cellSize*width); 

int loc = x + y*width; 

然后它的作品。唯一的问题是,如果我使用一个大的图像,我会得到ArrayIndexOutOfBoundsException,这可能是由超过数组中索引的最大数量引起的。谢谢你的协助!