我想要创建一个方法模糊24位图像使用3x3高斯核。
我得到了以下的东西。
3x3高斯核:

A是原始图像,B是结果图像。
B(i,j) =
1/16 * A(i-1,j-1) +1/8 * A(i,j-1) +1/16 * A(i+1,j-1) +1/8 * A(i-1,j) +1/4 * A(i,j) +1/8 *A(i+1,j) +1/16 * A(i-1,j+1) +1/8 * A(i,j+1) +1/16 * A(i+1,j+1) 方法:
public static BufferedImage gaussianBlur(Image img)其中img是输入图像的引用变量。
返回的值是结果图像的对象的地址。
我应该把图像分成9部分来实现这个方法吗?
发布于 2013-12-23 15:14:33
你不需要把它分成9部分。至少我看不出有什么好的理由这么做。
但是您最好在此过程中小心,记住将图像数据复制到某个地方,并始终使用这些数据来计算新图像,避免使用新的图像数据来计算新图像。
另外,我不明白为什么你需要写你自己的函数高斯模糊图像。这可以很容易地做到以下几点:
float[] matrix = {
1/16f, 1/8f, 1/16f,
1/8f, 1/4f, 1/8f,
1/16f, 1/8f, 1/16f,
};
BufferedImageOp op = new ConvolveOp( new Kernel(3, 3, matrix) );
blurredImage = op.filter(sourceImage, destImage);发布于 2016-05-08 02:38:34
别把它分成几个部分。如果你有大形象呢。您应该做的是首先编写一个函数,检查过滤器是否在图像边界内。在C语言中是这样的:
int filterWithinImage(Matrix m1, Matrix m2, int i, int j) {
int b; //min number of pixels that the center of the filter needs to be
// away from any border of the image to be inbounds
/***********************odd size filter only*************************/
//when filter size is odd there is well defined convenient center
// of the filter
if (isOdd(m2.height) && isOdd(m2.width)) {
//to check the bounds subtract 1 from the width and divide by 2
b = (m2.width - 1) / 2;
//look at the left border
if ((j - b)<0) return 0;
//top border
if ((i - b)<0) return 0;
//right border
if ((j + b)>(m1.width-1)) return 0;
//bottom border
if ((i + b)>(m1.height -1)) return 0;
}
return 1;
}而不是编写单独的函数来计算强度:
double calculateValue(Matrix m1,Matrix m2,int imagei, int imagej) {
double out = 0;//return value
int i, j, fli, flj; //for iterating over the filter
int b = (m2.height -1) / 2;//max number that we add to the center coordinates
//to get to the edge of the filter
fli = 0; flj = 0;
for(i = imagei - b; i < imagei + b +1; i++) {
for(j = imagej - b; j < imagej + b +1; j++) {
// if (i == 599)
//printf("calc func image i: %d, image j %d, b %d, filter i %d, filter j %d\n",
// i,j,b,fli,flj);
out += m1.map[i][j] * m2.map[fli][flj++];
}
fli++;
flj=0;
}
return out;
}applyFilter m2是你需要旋转180度的滤波器。矩阵applyFilter(矩阵m1,矩阵m2) { int x,y;//旋转滤波器首先是矩阵rotFilter =createMatrix(m2.高度,m2.宽度);对于(x = 0;x
Matrix mOut = createMatrix(m1.height, m1.width);
int i,j;
for (i = 0; i < m1.height; i++) {
for (j = 0; j < m1.width; j++) {
if (!filterWithinImage(m1,rotFilter,i,j)) { //filter is out of bounds
mOut.map[i][j] = 0;
}
else {
mOut.map[i][j] = calculateValue(m1,rotFilter,i,j);
}
}
}
return mOut;
}
这是一种必须修改以适应java数据结构的通用方法,但算法是相同的。
https://stackoverflow.com/questions/20746172
复制相似问题