前面说过高斯滤波,这里介绍线性滤波
线性滤波
输出图像fo(x,y)= T[ fi(x,y) ],T是线性算子,即:输出图像上每个像素点的值都是由输入图像各像素点值加权求和的结果。
非线性滤波的算子中包含了取绝对值、置零等非线性运算。
函数
Imgproc.filter2D(src, dst, -1, kernel, new Point(-1,-1));

代码实现:
public static void main(String[] args) {
try {
ResourceBundle bundle = ResourceBundle.getBundle("opencv");
String opencvDllName = bundle.getString("opencv.dllpath");
System.load(opencvDllName);
} catch (Exception e) {
e.printStackTrace();
}
String filename = "D:\\360MoveData\\Users\\lxn\\Desktop\\opencvtest\\cph.png";
Mat src = Imgcodecs.imread(filename);// 加载原图
Mat dst=new Mat();
Mat kernel=new Mat(new Size(3,3),CvType.CV_8SC1);//创建滤波核心
kernel.put(0,0,1,0,1);
kernel.put(1,0,0,1,0);
kernel.put(2,0,1,0,1);
System.out.println(kernel.dump());
//Imgproc.filter2D(src, dst,-1, kernel);
//Imgproc.filter2D(src, dst, -1, kernel, new Point(-1,-1));
Imgproc.filter2D(src, dst, -1, kernel, new Point(-1,-1));
Point p1=new Point(500, 100);
Point p2=new Point(520,120);
System.out.println(src.submat(new Rect(p1,p2)).dump());
System.out.println(dst.submat(new Rect(p1,p2)).dump());
HighGui.imshow("原图", src);
HighGui.imshow("线性滤波之后", dst);
HighGui.waitKey();
}
图像的处理效果和卷积核有着很大的关系
当卷积核:
[ 1, 0, 1;
0, 1, 0;
1, 0, 1]
滤波前后的图像对比:

改变卷积核:
[ -1, 0, -1;
0, 4, 0;
-1, 0, -1]
上面是对filter2D 这个线性滤波函数的基本的操作
希望对你有所帮助
本文介绍了线性滤波的基本概念及其在图像处理中的应用。通过具体的Java代码示例展示了如何使用OpenCV库中的filter2D函数进行图像的线性滤波处理,并比较了不同卷积核对图像滤波效果的影响。

1329

被折叠的 条评论
为什么被折叠?



