2011-03-19 113 views
0

运用下面的代码从一个灰度图像转换为阈值,但输出将是一个全黑画面的任何想法了一个错误:在处理代码

PImage toThreshhold(PImage sourcePic) 
{ 
    PImage thresh = new PImage(sourcePic.width,sourcePic.height); 

    sourcePic.loadPixels(); 
    for(int i = 0; i < sourcePic.width; i++) 
    { 
     for(int j = 0; j<sourcePic.height; j++) 
     { 
      int pixPosition = i*sourcePic.width +j; 
      if(sourcePic.pixels[pixPosition] > 127) 
      { 
       sourcePic.pixels[pixPosition] = color(255); 
      } 
      else 
      { 
       sourcePic.pixels[pixPosition] = color(0); 
      } 
     } 
    } 
    sourcePic.updatePixels(); 

    return thresh; 
} 
+0

您应指定使用标签的语言。另外,当忘记从浮点数转换为整数时,我经常遇到这种常见问题,反之亦然。 – 2011-03-19 18:36:40

+0

你得到的输出是什么,它与你所期待的有什么不同?张贴截图。 – misha 2011-03-20 10:10:06

回答

0
int pixPosition = i*t.width +j; 

是错误的。它应该是

int pixPosition = i*t.height +j; 

并且您不以任何方式修改tresh。这应该是好的:

thresh = sourcePic; 
thresh.loadPixels(); 
for(int i = 0; i < t.width; i++) 
{ 
    for(int j = 0; j<t.height; j++) 
    { 
     int pixPosition = i*t.height +j; 
     if(thresh.pixels[pixPosition] > 127) 
     { 
      thresh.pixels[pixPosition] = color(255); 
     } 
     else 
     { 
      thresh.pixels[pixPosition] = color(0); 
     } 
    } 
} 
thresh.updatePixels(); 

return thresh; 
+0

它仍然给出相同的结果,,,,黑色图片 – 2011-03-19 18:42:55

0

这是什么语言? C还是C++?什么是loadPixels()和updatePixels()?

你可能想int pixPosition = j*sourcePic.width +i;

+0

其处理...基于Java的语言 – 2011-03-19 21:27:54