【问题标题】:Confused about setting median values of a coloured image对设置彩色图像的中值感到困惑
【发布时间】:2013-07-26 03:34:45
【问题描述】:

我正在使用位图图像在 Java 中实现中值过滤器。我正在使用以另一种编程语言实现的算法,然后将其转换为 Java 用于我的实现。我不明白的算法部分如下:

for(int x = 0; x < w; x++) 
for(int y = 0; y < h; y++) 
{ 
    int n = 0;
    //set the color values in the arrays
    for(int filterX = 0; filterX < filterWidth; filterX++)
    for(int filterY = 0; filterY < filterHeight; filterY++)
    {
        int imageX = (x - filterWidth / 2 + filterX + w) % w;
        int imageY = (y - filterHeight / 2 + filterY + h) % h;
        red[n] = image[imageX][imageY].r;
        green[n] = image[imageX][imageY].g;
        blue[n] = image[imageX][imageY].b;
        n++;
    }        

这是Median Algorithm 的链接

我自己实现了以下代码,但我认为它不正确。如果有人可以帮助我,我将不胜感激。

for(int x = 0; x < width; x++)
    {
        for(int y = 0; y < height; y++) 
        { 
            int n = 0;
            for(int filterX = 0; filterX < filterWidth; filterX++)

                for(int filterY = 0; filterY < filterHeight; filterY++)
                {

                    pixel = image.getPixel(x,y);  
                    A = (pixel>>24) & 0xFF;
                    R = (pixel>>16) & 0xFF;
                    G = (pixel>>8) & 0xFF;
                    B = pixel & 0xFF;

                    RArray[n] = R;
                    GArray[n] = G;
                    BArray[n] = B;
                    n++;

            }        

【问题讨论】:

    标签: java android algorithm sorting bitmap


    【解决方案1】:

    我看不到您在哪里选择输入值的中位数。正如 Jim 提到的,一定要处理边缘情况,例如......

    for(int x = 0; x < width; x++)
    {
        for(int y = 0; y < height; y++) 
        { 
            int index = 0;
            for(int filterX = -filterWidth/2; filterX < filterWidth/2; filterX++)
                for(int filterY = -filterHeight/2; 
                     filterY < filterHeight/2; filterY++)
            {
                int pixelX = x+filterX;
                int pixelY = y+filterY;
    
                //ensure we're in bounds.
                if(pixelX>-1 && pixelY>-1 && pixelX<w && pixelY<h){
                    pixel = image.getPixel(x,y);
                    A = (pixel>>24) & 0xFF;
                    R = (pixel>>16) & 0xFF;
                    G = (pixel>>8) & 0xFF;
                    B = pixel & 0xFF;
    
                    RArray[index] = R;
                    GArray[index] = G;
                    BArray[index] = B;
                    ++index;
            }
       }
       //only sort the pieces of the array we've put data into,
       //remember we could be on an edge of the image.
       Arrays.sort(RArray,0,index);
       Arrays.sort(BArray,0,index);
       Arrays.sort(GArray,0,index);
       int medianR = RArray[RArray.length/2];
       int medianB = BArray[BArray.length/2];
       int medianG = GArray[GArray.length/2];
    
       //last step is to combine medians back into a single integer
    

    【讨论】:

    • 嗨,你是正确的,它是一个 3x3 内核,但链接中提供的算法是一个中值,这就是我想要实现的,这就是我不明白的部分。
    • 我的实现可能是错误的,但是链接中提供的算法应该是中值滤波器。
    • 您好,谢谢您,您的实现是中值算法还是卷积算法
    • 是卷积,我改了。 @RigaKdot
    • 好吧,我会选择这个答案作为正确答案,但是请您使用我提供的所有代码粘贴它。会更好,所以没有人必须继续上下滚动=)非常感谢
    【解决方案2】:

    你需要分析N个像素,找到中间值得到中位数。
    您需要使用不断变化的 filterX 和 filterY 值作为偏移量来获取除 x 和 y 之外的其他像素。

    getPixel(x+filterX,y+filterY)

    【讨论】:

    • 如何将它与 RGB 颜色空间一起使用?请问可以放一些代码吗?它只会帮助我更好地理解。
    • 你所拥有的一切都很好。你得到像素(3个波段)和计算。每个的中位数。您必须对每个数组求和并除以 n。上面的示例偏移了过滤器,使像素 x,y 位于中值的中心。你可能想这样做。另外,我不确定您是如何处理边缘效果的,因为偏移量可能会使您超出图像的边缘。
    • @Jim 不,您描述的是平均值。我最初在回答中犯了同样的错误。除法让你得到平均值。中位数受异常值的影响较小,在去除噪声时非常理想。
    • valx = max(0,min(x+filterX,width); valy = max(0,min(y+filterY,height); //这(粗略地)处理边缘效应,所以你访问超出图像区域的内容不会出现段错误
    • @jim 是的,哈哈,我们都在这里有点得意忘形。 +1 是一个好人......呃......溢出。 :)
    猜你喜欢
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多