2011-04-12 84 views
0

所以,我想要做的是通过一个图片是1024x768(或,popMap.getWidth x popMap.getHeight),抓住它的蓝色,比较它到目前为止最高的蓝色,如果它更多,那蓝色变成新的'newBlue'。基本上,找到图像中最高的蓝色值,最接近255蓝色。嵌套for循环存储到一个数组,只有一列嵌套循环到数组

此外,我试图将整个事情的蓝色值存储到一个数组popMapArray,这是一个3列2d数组,存储[blueValue] [x] [y]。然后,我将排序,以获得从高到低的蓝色值列表。

我的问题是,用下面的代码,它只是在列= 767时存储到数组中。

我得到1024 [蓝色,行,767],然后剩下的全是[0,0,0]

任何线索,为什么?顺便说一句,Java。

for (int row = 0; row < popMap.getWidth(); row++) 
    { 
     for (int column = 0; column < popMap.getHeight(); column++) 
     { 
      System.out.println(column); 
      //Find a Pixel 
      int c = popMap.getRGB(row, column); 
      int red = (c & 0x00ff0000) >> 16; 
      //int green = (c & 0x0000ff00) >> 8; 
      //int blue = c & 0x000000ff; 
      // and the Java Color is ... 
      Color color = new Color(red); 
      int newBlue = color.getBlue(); 
      int oldBlue = lastColor.getBlue(); 
      switch(popArrayRow) 
      { 
       case 0: 
       { 
        arrayVar = newBlue; 
        popArrayRow = 1; 
        break; 
       } 
       case 1: 
       { 
        arrayVar = row; 
        popArrayRow = 2; 
        break; 
       } 
       case 2: 
       { 
        arrayVar = column; 
        popArrayRow = 0; 
        break; 
       } 
      } 
      popArray[row][popArrayColumn] = arrayVar; 
      //System.out.println(popArray[row][popArrayColumn]); 
      switch(popArrayColumn) 
      { 
       case 0: 
       { 
        popArrayColumn = 1; 
        break; 
       } 
       case 1: 
       { 
        popArrayColumn = 2; 
        break; 
       } 
       case 2: 
       { 
        popArrayColumn = 0; 
        break; 
       } 
      } 


      if(newBlue > oldBlue) 
      { 
       startX = row; 
       startY = column; 
       //System.out.print(row); 
       //System.out.print(","); 
       //System.out.println(column); 
       System.out.print("The oldBlue is "); 
       System.out.println(oldBlue); 
       lastColor = color; 
      } 



     } 
    } 
+0

什么y?我不明白你的问题 – smas 2011-04-12 21:07:44

+0

对不起,当列= 767. – 2011-04-12 21:10:31

回答

0

您没有显示声明去popArray(以及我假设的其他一些变量是已初始化为0的整数)。您将其描述为“具有3列的2d阵列”。我猜你已经宣布它为int[1024][3],所以它每行有一行popMap,那么你的3个“列”就是为了存储蓝色值,原始x坐标和原始y坐标。

因此,首先不清楚您希望如何在原始图像中为每个像素存储一个条目。但也许我猜你是如何宣布它是错误的。

在任何情况下,通过内循环每次基本上要设置

popArray[currentPixel] = {blueValue, origX, origY}

而是你只三个值中的一个通过循环每次分配。所以,你正在做的事情一样

popArray[0][0] = blueValue //first iteration; blueValue from row 0 col 0 
popArray[0][1] = 0 //second iteration; row from row 0 col 1 
popArray[0][2] = 2 //third iteration; column from row 0 col 2 

所以希望你也能看到,什么是错的,因为你是填充,都应该从循环的不同迭代值一起去“列”。更糟的是,你然后开始在内部循环的下一次迭代中(其将前row增量迭代共768次)重写这些值:

popArray[0][0] = blueValue // fourth iteration; blueValue from row 0 col 4; overwrite value assigned on first iteration 
etc... 

而不是使用3阵列“列”具有不同的含义,以掌握这些数据元素,让一个拥有三个价值观的班级明确什么是明智的。 popArray将包含此对象类型。此外,我将它作为List而不是阵列,因为它更灵活,最后你可以简单地调用Collections.sort();或@jsegal有一个很好的建议,即使用插入项目时排序的数据结构。哪一个更好可能取决于你以后想要做什么。

+0

对于数组的第一部分,它实际上是MAP_HEIGHT * MAP_WIDTH-1,因为它需要是每个像素。我没有把所有的代码放在变量声明和排序中,因为这段代码比这节更多。 随着'popArray [currentPixel] = {blueValue,origX,origY}',这实际上是我想要做的,我怎么一次设置三个值到一行?我相信这是完全明显的,但我最近已经对此产生了兴趣。 – 2011-04-12 21:38:19

+0

固定:现在简单: \t \t \t \t popArray [currentRow] [0] = newBlue; \t \t \t \t popArray [currentRow] [1] = row; \t \t \t \t popArray [currentRow] [2] = column; \t \t \t \t currentRow ++; \t \t \t \t如果(currentRow> =(BOX_WIDTH * BOX_HEIGHT-1)) \t \t \t \t { \t \t \t \t \t中断; \t \t \t \t} 设置每个行只有一次,将它们全部保存,并给我一个充满每一个点的数组,它的颜色,x和y。感谢您指出我是如何彻底失败的二维数组,现在变得更有意义! – 2011-04-12 22:05:18

1
int red = (c & 0x00ff0000) >> 16; 
     //int green = (c & 0x0000ff00) >> 8; 
     //int blue = c & 0x000000ff; 
     // and the Java Color is ... 
     Color color = new Color(red); 
     int newBlue = color.getBlue(); 

你的意思是 “Colour彩色=新的色彩(C)”?您的newBlue值将始终为0 ...

另外,您准备用popArray构造做什么?让每个像素的状态变量自己调整一次可能不会达到您想要的效果......听起来您想要一个SortedMap<int,Point>,键入blueValue,其值是点的x,y坐标(作为数组或Point对象)。然后你会得到你的数据结构,按蓝色值排序,你可以直接阅读你的点数。

祝你好运!

+0

那么现在它是灰度,所以蓝色或红色并不重要,它出来是一样的。它会保持灰色,我只是随意选择蓝色,因为为什么不。 我会看看SortedMap,我知道必须做些什么才能做我想做的事情。 – 2011-04-12 21:17:37

+0

这似乎是做到这一点的最佳方式,只需要几行就能做到我想要的大部分。只需要弄清楚如何访问SortedMap,因为我之前从未使用过。 – 2011-04-12 21:54:52