首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Java (Min Filter)进行图像处理;在2D矩阵的中心放置一个元素

使用Java (Min Filter)进行图像处理;在2D矩阵的中心放置一个元素
EN

Stack Overflow用户
提问于 2018-06-29 03:42:24
回答 1查看 581关注 0票数 0

我正在尝试使用java在一个".pgm“文件上实现各种图像处理过滤器。下面是最小过滤器的代码:

代码语言:javascript
复制
void applyMinFilter() {
    int [] array = new int[size*size];
    int t = 0, i = 0, j = 0;
    for(int c = 0; c<h-size+1; c++) {
        for(int k = 0; k<w-size+1; k++) {
            t = 0;
            for(i = c; i<c+size; i++) { 
                for(j = k; j<k+size; j++) {
                    array[t++] = matrix[i][j];
                }
            }
            //placing the minimum value in the centre of the considered grid
            matrix[i/2][j/2] = minimum(array);
        }
    }
}

注意:这里,size = 5,w=h= 400

使用这种方法,我得到了一个输出,其中我想要的图像在照片的一角。You can see the output image by clicking here.在我的代码中,c循环和k循环帮助我们遍历整个图像,而i循环和j循环为我们提供了应用最小滤波器所需的小窗口。我已经将".pgm“图像转换为矩阵以进行操作。

我非常确定错误是来自注释行之后的那一行。我不能正确地将最小值像素放置在正确的位置。我该怎么办?

EN

回答 1

Stack Overflow用户

发布于 2018-06-29 23:05:31

您应该将matrix[i/2][j/2]的索引替换为matrix[c+size/2][k+size/2],或者您可以编写如下代码:

代码语言:javascript
复制
void applyMinFilter() {
    int [] array = new int[size*size];
    int t = 0, i = 0, j = 0;
    // for size = 5 you can use h-2 or w-2
    // to make it general replace with h-size/2 and w-size/2
    for(int c = 2; c < h-2; c++) {
        for(int k = 2; k < w-2; k++) {
            t = 0;
            for(i = c-2; i < c+2; i++) { 
                for(j = k-2; j < k+2; j++) {
                    array[t++] = matrix[i][j];
                }
            }
            //placing the minimum value in the centre of the considered grid
            matrix[c][k] = minimum(array);
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51090245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档