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;
    }
  • 相关阅读:
    .NET System.Web.HttpContext.Current.Request报索引超出数组界限。
    Jq将字符串复制粘贴到剪贴板
    设置VS以管理员身份运行
    http遇到的那些坑,iis上传文件报413错误 asp.net MVC
    百度地图api使用,简单搜索+经纬度定位+自定义消息窗口
    常见的sql server 链接问题------持续更新
    解决电脑不能访问局域网共享,局域网共享中找不到。
    博文图片挂了临时解决办法
    博客声明
    06. redis cluster
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2687143.html
Copyright © 2011-2022 走看看