Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Median filter

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Median filter

    I have to implement the median filter. I found an example on the internet but it does not run, I do not see the image, where am I wrong?

    public void median_RGB(Immagine img) {
     
            int maskSize = 3;
            int width = img.getW();
            int height = img.getH();
            int outputPixels[] = new int[width * height];
     
            int red[], green[], blue[];
            int xMin, xMax, yMin, yMax;
     
            int argb, reD, greenN, bluE;
     
            /** Median Filter operation */
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    int a = img.getAlpha(x, y);
                    red = new int[maskSize * maskSize];
                    green = new int[maskSize * maskSize];
                    blue = new int[maskSize * maskSize];
                    int count = 0;
                    xMin = x - (maskSize / 2);
                    xMax = x + (maskSize / 2);
                    yMin = y - (maskSize / 2);
                    yMax = y + (maskSize / 2);
                    for (int r = yMin; r <= yMax; r++) {
                        for (int c = xMin; c <= xMax; c++) {
                            if (r < 0 || r >= height || c < 0 || c >= width) {
                                /** Some portion of the mask is outside the image. */
                                continue;
                            } else {
                                argb = img.getOriginalImage().getRGB(c, r);
                                reD = (argb >> 16) & 0xff;
                                red[count] = reD;
                                greenN = (argb >> 8) & 0xff;
                                green[count] = greenN;
                                bluE = (argb) & 0xFF;
                                blue[count] = bluE;
                                count++;
                            }
                        }
                    }
     
                    /** sort red, green, blue array */
                    java.util.Arrays.sort(red);
                    java.util.Arrays.sort(green);
                    java.util.Arrays.sort(blue);
     
                    /** save median value in outputPixels array */
                    int index = (count % 2 == 0) ? count / 2 - 1 : count / 2;
                    logger.info("valore mediano " + index);
                    int p = (a << 24) | (red[index] << 16) | (green[index] << 8) | blue[index];
                    outputPixels[x + y * width] = p;
                }
            }
            /** Write the output pixels to the image pixels */
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    img.getOriginalImage().setRGB(x, y, outputPixels[x + y * width]);
                }
            }
        }
    Thanks


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Median filter

    What do you mean when you say "it does not run"? You have to be more precise, right now we cant help you.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Median filter

    I do not see the image
    What image? Show us the code relevant to the problem with any error messages you're receiving.

  4. #4
    Junior Member
    Join Date
    May 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Median filter

    What do you mean when you say "it does not run"? You have to be more precise, right now we cant help you.
    The application successfully loads the images, apply various filters such as this LookUp table and everything works fine. If I apply the noise filter I can still see the image, then when I apply the median filter to the image no longer see it, is no longer displayed. The problem may be in the calculation of median filter but I can not figure out where, is more clear now? can you help?

    What image? Show us the code relevant to the problem with any error messages you're receiving.
    I have no real error messages, do not I see more correctly the image
    thanks

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Median filter

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to get Median/Middle value in an array?
    By deluxe7 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2014, 03:56 PM
  2. MEDIAN,(BY SORT ARRAY)
    By manahil in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 12th, 2013, 03:07 PM
  3. Quicksort with median-of-3 partitioning
    By ueg1990 in forum Algorithms & Recursion
    Replies: 0
    Last Post: October 27th, 2012, 03:19 PM
  4. Iterative quick sort using median of three values
    By omfgz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 30th, 2012, 09:59 PM
  5. recursive program to find median
    By TeamRival in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 26th, 2011, 10:40 PM