zoukankan      html  css  js  c++  java
  • hdu Median Filter

    Median Filter

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 74    Accepted Submission(s): 31
     
    Problem Description
    Median filter is a cornerstone of modern image processing and is used extensively in

    Given a black and white image of n by n pixels, the algorithm works as follow: For each pixel p at the i‐th row and the j‐th column (1+ r <= i, j <= n – r), its gray level g[i,j] is replace by the median of gray levels of pixels in the (2r+1) by (2r+1) square centered at p. The square is called the filtering window and r is its radius.

    Considering the above example, the gray level of the pixel at center will be changed from 150 to 124, which is the median of a filtering window of radius 1.
    Note that the algorithm won’t be applied on the pixels near the boundary, for the filtering window lies outside the image. So you are actually asked to output the filtered sub-image which contains the pixels from (r+1, r+1) to (n-r, n-r).
     
    Input
    The input contains several test cases. For each test case:
      (a) The first line contains two integers, n and r, meaning that the size of image is n * n (3 <= n <= 500) and the radius of filtering window is r ( 3 <= 2r + 1 <= n).
      (b) The following n lines contains the n by n gray level matrix presenting the image
      (c) The gray level ranges from 0 to 1000000 The input ends by double 0s.
     
    Output
                For each test case output a (n – 2r) by (n – 2r) matrix presenting the sub-image after filtered.
     
    Sample Input
    3 1
    1 1 1
    1 1 1
    1 1 1
    3 1
    1 9 6
    4 5 2
    3 7 8
    3 1
    0 0 0
    255 255 255
    0 255 0
    5 1
    0 0 1 1 0
    1 0 1 0 1
    0 0 1 1 1
    1 1 1 0 1
    1 0 0 0 1
    0 0
     
    Sample Output
    1
    5
    0
    0 1 1
    1 1 1
    1 0 1
    Hint
    The definition of “median”: One type of average, found by arranging the values in order and then selecting the one in the middle. If the total number of values in the sample is even, then the median is the mean of the two middle numbers. The median is a useful number in cases where the distribution has very large extreme values which would otherwise skew the data.
     
     
    Source
    The 35th ACM/ICPC Asia Regional Hangzhou Site —— Online Contest
     
    Recommend
    lcy
     

    分析:树状数组求第k大的数,采用S型路线节约时间,另外调用很多的函数要写成inline

    #include<cstdio>
    #include<cstring>
    #define maxn 1000002
    int s[maxn];
    int map[505][505];
    int ans[505][505];
    int MAX;
    
    inline int lowbit(int x) {
        return x & (-x);
    }
    
    int sum(int a) {
        int ans = 0;
        while (a > 0) {
            ans += s[a];
            a -= lowbit(a);
        }
        return ans;
    }
    
    void add(int a, int c) {
        while (a <= MAX) {
            s[a] += c;
            a += lowbit(a);
        }
    }
    
    int find_kth(int k) {
        int sum = 0, pos = 0, i;
        for (i = 20; i >= 0; i--) {
            pos += (1 << i);
            if (pos > MAX || sum + s[pos] >= k) {
                pos -= (1 << i);
            } else {
                sum += s[pos];
            }
        }
        return pos + 1;
    }
    
    int main() {
        int n, i, j, r, k, R, t;
        while (scanf("%d%d", &n, &r) != EOF && n) {
            MAX = -1;
            memset(s, 0, sizeof (s));
            for (i = 1; i <= n; ++i)
                for (j = 1; j <= n; ++j) {
                    scanf("%d", &map[i][j]);
                    map[i][j]++;
                    if (map[i][j] > MAX)
                        MAX = map[i][j];
                }
            R = 2 * r + 1;
            k = R * R / 2 + 1;
            for (i = 1; i <= R; ++i) {
                for (j = 1; j <= R; ++j) {
                    add(map[i][j], 1);
                }
            }
            for (i = 1+r; i <= n -r; ++i) {
                if ((i-r) & 1) {
                    if (i != 1+r) {
                        for (j = 1; j <= R; ++j) {
                            add(map[i -r- 1][j], -1);
                            add(map[i +r][j], 1);
                        }
                    }
                    ans[i][1 + r] = find_kth(k);
                    for (j = 2+r; j <= n - r; ++j) {
                        for (t = i-r; t <= i + r; ++t) {
                            add(map[t][j - r-1], -1);
                            add(map[t][j + r], 1);
                        }
                        ans[i][j] = find_kth(k);
                    }
                } else {
                    for (j = n; j > n - R; --j) {
                        add(map[i - r-1][j], -1);
                        add(map[i + r][j], 1);
                    }
                    ans[i][n - r] = find_kth(k);
                    for (j = n - r-1; j >= 1+r; --j) {
                        for (t = i-r; t <= i + r; ++t) {
                            add(map[t][j+r+1], -1);
                            add(map[t][j-r], 1);
                        }
                        ans[i][j] = find_kth(k);
                    }
                }
            }
            for (i = 1 + r; i <= n - r; ++i) {
                for (j = 1 + r; j < n - r; ++j)
                    printf("%d ", ans[i][j] - 1);
                printf("%d \n", ans[i][j] - 1);
            }
        }
        return 0;
    }
  • 相关阅读:
    Silverlight 动态调用 WebService
    一步一步学Silverlight 2系列(28):图片处理
    一步一步学Silverlight 2系列(27):使用Brush进行填充
    HTML5 Web Applications
    分享31个非常有用的 HTML5 教程
    数学分析原理 定理 6.5
    数学分析原理 定理 6.9
    RiemannStieltjes积分存在的充分条件(按照Tom M.Apostol的《数学分析》上的定义)
    数学分析原理 定理 6.4
    数学分析原理 定理 6.8
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2687143.html
Copyright © 2011-2022 走看看