【发布时间】:2015-10-28 01:16:58
【问题描述】:
我的问题不是指我需要使用哪些运算符来操作矩阵,而是指通过执行此过程实际寻求什么。
例如,我有一个矩阵形式的图像,我需要对其执行多个操作(此过滤器就是其中之一)。将所述图像转换为灰度后,我需要应用以下过滤器
float[][] smoothKernel = {
{0.1f,0.1f,0.1f},
{0.1f,0.2f,0.1f},
{0.1f,0.1f,0.1f}
};
就可以了。
分配文件给出了这个例子,所以我假设当被要求“平滑”图像时,我必须用其相邻像素的平均值替换每个单独的像素(同时还要确保特殊情况,如角落或侧处理得当)。
基本思路是这样的:
public static float[][] filter(float[][] gray, float[][] kernel) {
// gray is the image matrix, and kernel is the array I specifed above
float current = 0.0f;
float around = 0.0f;
float[][] smooth = new float[gray.length][gray[0].length];
for (int col = 0; col < gray.length; col++) {
for (int row = 0; row < gray[0].length; row++) {
//first two for loops are used to do this procedure on every single pixel
//the next two call upon the respective pixels around the one in question
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
around = at(gray, i + col, j + row); //This calls a method which checks for the
//pixels around the one being modified
current += around * kernel[i+1][j+1];
//after the application of the filter these are then added to the new value
}
}
smooth[col][row] = current;
current = 0.0f;
//The new value is now set into the smooth matrix
}
}
return smooth;
}
我的困境在于是否必须创建这个新数组float[][] smooth; 以避免覆盖原始值(在这种情况下输出的图像全是白色......)。从我上面链接的示例中的最终产品中,我无法理解发生了什么。
应用过滤器的正确方法是什么?这是一种通用方法还是因不同的过滤器而异?
感谢您花时间澄清这一点。
编辑:我发现了我在下面的 cmets 中详述的两个错误,并在代码中实现,现在一切正常。
我还能够验证示例中的某些值计算不正确(从而导致我的困惑),所以我一定会在下一节课中指出这一点。
【问题讨论】:
-
创建一个新数组对我来说似乎是正确的。我也会这样做。你有什么问题?
-
@markspace 我得到一个白色图像,当它应该输出带有一些模糊的灰度图像时
-
@markspace 我刚刚意识到我必须在每个
smooth[col][row]之后设置current = 0.0f,但是现在图像都是灰色的 -
@markspace 好的,我发现没关系,有索引问题,我会在代码中修复它