图像锐化处理之一阶微分算子

  图像锐化是通过增强图像的边缘和细节来提高图像的清晰度的操作。这种操作通常用于将模糊或不清晰的图像改进为更清晰的图像。由于微分是对函数局部变化率的一种描述,因此图像锐化算法的实现可基于空间微分。

一阶微分算子

  对任意一阶微分的定义必须满足两点:灰度不变的区域微分值为0;在灰度变化的区域微分值非0。由于处理的是离散情况,微分用差分近似。对于一维函数f(x,y),一阶微分基本定义:

                                              \frac{\partial f}{\partial x}=f(x+1)-f(x)

 一维微分可用导数符号。二维图像f(x,y) 将沿着两个空间坐标轴求解一阶微分,分别对x,y求偏导:

               g_{x}=\frac{\partial f}{\partial x}=f(x+1,y)-f(x,y)  , g_{y}=\frac{\partial f}{\partial y}=f(x,y+1)-f(x,y)

  二维图像的梯度是一个二维向量:

                    \Delta f=grad(f)=\begin{bmatrix} g_{x}\\ g_{y} \end{bmatrix}

  梯度的幅值:

                      M(x,y)=mag(\Delta f)=\sqrt{g_{x}^{2}+g_{y}^{2}}

  M(x,y)是梯度向量方向变化率在(x,y)处的值。

  

    这里使用3*3邻域灰度值表示z1,z2...z9,其中z5表示f(x,y),依次类推。

二维图像最简单的一阶微分近似:

  g_{x}=z_{9}-z_{5}             g_{y}=z_{8}-z_{6}

  根据罗伯特观点,边缘检测器应具有:

产生边缘清晰;背景尽可能减小噪声;边缘强度应尽可能接近人类的感知。并提出两个交叉差分表示:

  g_{x}=z_{9}-z_{5}       g_{y}=z_{8}-z_{6}

梯度幅值为:

M(x,y)=mag(\Delta f)=\sqrt{g_{x}^{2}+g_{y}^{2}} =\sqrt{(z_{9}-z_{5})^{2}+(z_{8}-z_{6})^{2}}

 正负对角线梯度算子:

  罗伯特边缘图像和梯度图像代码:

from skimage import io,filters
from matplotlib import pyplot as plt
from skimage.color import rgb2gray

#原始图像
I=io.imread('demo.jpg')
I_gray=rgb2gray(I) #灰度转换
#robert交叉梯度算子
I_rob_pos=filters.roberts_pos_diag(I_gray)
I_rob_neg=filters.roberts_neg_diag(I_gray)
I_rob=filters.roberts(I_gray)
#显示
plt.subplot(121)
plt.title("gray")
plt.imshow(I_gray,cmap='gray')
plt.subplot(122)
plt.title("Pos")
plt.imshow(I_rob_pos,cmap='gray')
plt.show()
plt.subplot(121)
plt.title("Neg")
plt.imshow(I_rob_neg,cmap='gray')
plt.subplot(122)
plt.title("Rob")
plt.imshow(I_rob,cmap='gray')
plt.show()

 原始图像和正对角线边缘图像:

负对角先边缘图像和罗伯特梯度图像:

 

  使用交叉梯度算子可以得到梯度图像M(x,y),将梯度图像以一定比例叠加到原始图像f(x,y)即可得到锐化图像:

     g(x,y)=f(x,y)+c*M(x,y)

 c为锐化强度系数。

  由于奇数模板有对称中心,更易于实现。使用3*3模板对g_{x} ,g_{y}进行表达:

g_{x} =\frac{\partial f}{\partial x}=(z_{7}+2z_{8}+z_{9})-(z_{1}+2z_{2}+z_{3})

g_{y}\frac{\partial f}{\partial y}=(z_{3}+2z_{6}+z_{9})-(z_{1}+2z_{4}+z_{7})

 

Sobel边缘图像和梯度图像:

from skimage import io,filters
from matplotlib import pyplot as plt
from skimage.color import rgb2gray

#原始图像
I=io.imread('demo.jpg')
I_gray=rgb2gray(I) #灰度转换
#sobel算子
I_h=filters.sobel_h(I_gray)
I_v=filters.sobel_v(I_gray)
I_sobel=filters.sobel(I_gray)

#显示
plt.subplot(121)
plt.title("gray")
plt.imshow(I_gray,cmap='gray')
plt.subplot(122)
plt.title("H")
plt.imshow(I_h,cmap='gray')
plt.show()
plt.subplot(121)
plt.title("V")
plt.imshow(I_v,cmap='gray')
plt.subplot(122)
plt.title("Sobel")
plt.imshow(I_sobel,cmap='gray')
plt.show()

原图与水平sobel边缘图像:

竖直sobel边缘图像和sobel梯度图像:

 

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一阶微分算子锐化图像可以使用Sobel算子或Prewitt算子来实现。 以Sobel算子为例,Sobel算子分为水平和垂直方向的两个卷积核,分别对图像进行卷积运算后再取它们的平方和再开方即可得到锐化后的图像。 具体实现步骤如下: 1. 定义Sobel算子的两个卷积核,分别表示水平和垂直方向的微分算子。 ```c++ int sobel_x[3][3] = { {-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1} }; int sobel_y[3][3] = { {-1, -2, -1}, {0, 0, 0}, {1, 2, 1} }; ``` 2. 对图像进行卷积运算。 ```c++ int width = img_width - 2; int height = img_height - 2; for (int x = 1; x < width; x++) { for (int y = 1; y < height; y++) { int gx = 0, gy = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { int pixel = get_pixel_gray(img, img_width, x + i, y + j); gx += sobel_x[i + 1][j + 1] * pixel; gy += sobel_y[i + 1][j + 1] * pixel; } } int val = sqrt(gx * gx + gy * gy); set_pixel_gray(result, img_width, x, y, val); } } ``` 3. 对卷积运算后的结果进行阈值处理。 ```c++ for (int i = 0; i < img_width * img_height; i++) { if (result[i] < threshold) { result[i] = 0; } else { result[i] = 255; } } ``` 完整代码如下: ```c++ #include <iostream> #include <cmath> using namespace std; int get_pixel_gray(unsigned char *img, int width, int x, int y) { return img[y * width + x]; } void set_pixel_gray(unsigned char *img, int width, int x, int y, int val) { img[y * width + x] = val; } void sobel(unsigned char *img, unsigned char *result, int img_width, int img_height, int threshold) { int sobel_x[3][3] = { {-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1} }; int sobel_y[3][3] = { {-1, -2, -1}, {0, 0, 0}, {1, 2, 1} }; int width = img_width - 2; int height = img_height - 2; for (int x = 1; x < width; x++) { for (int y = 1; y < height; y++) { int gx = 0, gy = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { int pixel = get_pixel_gray(img, img_width, x + i, y + j); gx += sobel_x[i + 1][j + 1] * pixel; gy += sobel_y[i + 1][j + 1] * pixel; } } int val = sqrt(gx * gx + gy * gy); set_pixel_gray(result, img_width, x, y, val); } } for (int i = 0; i < img_width * img_height; i++) { if (result[i] < threshold) { result[i] = 0; } else { result[i] = 255; } } } int main() { // 读入图像 FILE *f = fopen("lena.raw", "rb"); unsigned char img[512 * 512]; fread(img, 1, 512 * 512, f); fclose(f); // Sobel锐化 unsigned char result[512 * 512]; sobel(img, result, 512, 512, 128); // 保存结果 f = fopen("lena_sobel.raw", "wb"); fwrite(result, 1, 512 * 512, f); fclose(f); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值