我正在尝试使用java在一个".pgm“文件上实现各种图像处理过滤器。下面是最小过滤器的代码:
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“图像转换为矩阵以进行操作。
我非常确定错误是来自注释行之后的那一行。我不能正确地将最小值像素放置在正确的位置。我该怎么办?
发布于 2018-06-29 23:05:31
您应该将matrix[i/2][j/2]的索引替换为matrix[c+size/2][k+size/2],或者您可以编写如下代码:
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);
}
}
}https://stackoverflow.com/questions/51090245
复制相似问题