处理代码时出错

时间:2011-03-19 18:33:25

标签: image-processing

应用以下代码将灰度图像转换为阈值,但输出将是一个完整的黑色图片任何想法:

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

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

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

您可能需要int pixPosition = j*sourcePic.width +i;