数字图像处理之图像的梯度倒数加权平滑:用Java+OpenCV实现,附源码
OpenCV如何配置在IDEA中自行百度
环境:win10+IDEA2021.2.3+jdk11.0.1+OpenCV-460.jar
一、简介
背景:梯度倒数加权平滑是图像平滑算法的一种,在图像产生、传输和复制过程中,常常会因为多方面原因被噪声干扰或出现数据丢失,降低了图像的质量,需要对图像进行一定的增强处理以减小这些缺陷带来的影响
梯度倒数加权平滑:在一帧离散图像中,相邻区域的变化大于区域内部的变化,在同一区域中中间像素的变化小于边沿像素的变化。梯度值正比于邻域像素灰度级差值,即在图像变化缓慢的区域,梯度值小,反之则大。现在取梯度倒数,该倒数的大小正好与梯度相反,以梯度倒数做权重因子,则区域内部的邻点权重就大于边沿近旁或区域外的邻点。也就是说,这种平滑其贡献主要来自于区域内部的像素,平滑后图像边沿和细节不会受到明显损害
常用的平滑算法有:均值滤波,中值滤波,高斯低通滤波,梯度倒数加权平滑
梯度倒数加权平滑的优点:图像边沿和细节不会受到明显损害;
二、算法流程
(以单波段图像为例)
1.利用OpenCV读入图像,将像素存储在数组里
2.边缘像素不做处理,以3×3模板为例,模板为
规定w( i ,j ) = 1/2,其余系数之和为1/2,定义除中心像素外的其他系数为
对于中心像素与其他像素的差值为0的情况,用如下公式:
通过下面公式将w总和归一化为1/2:
3.进行卷积运算:新像素=0.022×21+0.218×30+0.073×28+0.031×24+0.5×31+0.027×23+0.044×36+0.031×38+0.054×27≈30
4.将经过梯度倒数加权平滑后的像素值存入数组合成图像并存储
三、具体实现
import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs;/*** @Author: HNUST_jue_chen* @Date: 2022/11/02/ 19:40* @Attention: 转载, 引用请注明出处*/public class GradientReciprocalWeighting {//加载本地动态链接库static {System.loadLibrary(Core.NATIVE_LIBRARY_NAME);}//梯度倒数加权public Mat gradientReciprocalWeightingFilter(String path) {//使用Mat类存储图像信息Mat mat = Imgcodecs.imread(path);//图像的大小int rows = mat.rows();int cols = mat.cols();//获得原图像像素数组int[][] mat_arr = new int[rows][cols];for (int i = 0; i < rows; i++) {for (int j = 0; j < cols; j++) {mat_arr[i][j] = (int) mat.get(i, j)[0];}}//用3×3窗口进行梯度倒数加权平滑int[][] mat_arr_gradientReciWeight = new int[rows][cols];for (int i = 0; i < rows; i++) {for (int j = 0; j < cols; j++) {//处理非边缘像素if (i != 0 && i != rows - 1 && j != 0 && j != cols - 1) {//3×3窗口的原始图像梯度倒数double[][] temp = new double[3][3];temp[0][0] = judgment(mat_arr[i - 1][j - 1] , mat_arr[i][j]);temp[0][1] = judgment(mat_arr[i - 1][j] , mat_arr[i][j]);temp[0][2] = judgment(mat_arr[i - 1][j + 1] , mat_arr[i][j]);temp[1][0] = judgment(mat_arr[i][j - 1] , mat_arr[i][j]);temp[1][1] = 0;temp[1][2] = judgment(mat_arr[i][j + 1] , mat_arr[i][j]);temp[2][0] = judgment(mat_arr[i + 1][j - 1] , mat_arr[i][j]);temp[2][1] = judgment(mat_arr[i + 1][j] , mat_arr[i][j]);temp[2][2] = judgment(mat_arr[i + 1][j + 1] , mat_arr[i][j]);//得到梯度倒数之和double temp_sum = 0.0;for (double[] m : temp) {for (double n : m) {temp_sum += n;}}//归一化后的梯度权重矩阵double[][] temp_normalize = new double[3][3];for (int p = 0; p < temp_normalize.length; p++) {for (int q = 0; q < temp_normalize[0].length; q++) {if (p == 1 && q == 1) {temp_normalize[p][q] = 0.5;} else {temp_normalize[p][q] = temp[p][q] / (2 * temp_sum);}}}//得到新中心像素mat_arr_gradientReciWeight[i][j] = (int) (temp_normalize[0][0] * mat_arr[i - 1][j - 1]+ temp_normalize[0][1] * mat_arr[i - 1][j]+ temp_normalize[0][2] * mat_arr[i - 1][j + 1]+ temp_normalize[1][0] * mat_arr[i][j - 1]+ temp_normalize[1][1] * mat_arr[i][j]+ temp_normalize[1][2] * mat_arr[i][j + 1]+ temp_normalize[2][0] * mat_arr[i + 1][j - 1]+ temp_normalize[2][1] * mat_arr[i + 1][j]+ temp_normalize[2][2] * mat_arr[i + 1][j + 1]);} else { //处理边缘像素mat_arr_gradientReciWeight[i][j] = mat_arr[i][j];}}}//合成图像Mat mat_gradientReciWeight = new Mat(rows, cols, CvType.CV_32SC1);//将像素放入图像for (int i = 0; i < rows; i++) {//一次放入一行像素值mat_gradientReciWeight.put(i, 0, mat_arr_gradientReciWeight[i]);}return mat_gradientReciWeight;}//进行判断中心像素与边缘像素的差值是否为0public static double judgment(int a, int b) {double c;if (a - b == 0) {c = 1.0;} else {c = 1.0 / (Math.abs(a - b));}return c;}public static void main(String[] args) {GradientReciprocalWeighting grw = new GradientReciprocalWeighting();Mat mat = grw.gradientReciprocalWeightingFilter("D:\\Project\\IDEA_Project\\RS01\\src\\rs01\\img\\2_gray1.png");//将经过梯度倒数加权平滑后的图像写入文件Imgcodecs.imwrite("D:\\Project\\IDEA_Project\\RS01\\src\\rs01\\img\\2_gray1_grw.png", mat);} }四、结果
1.读入的图像
2.经过梯度倒数加权平滑后的图像
总结
以上是生活随笔为你收集整理的数字图像处理之图像的梯度倒数加权平滑:用Java+OpenCV实现,附源码的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: C语言山东春考技能模拟,1-2020年山
- 下一篇: 【Java】文件上传及下载、限制文件大小