一阶微分算子锐化图像c++
时间: 2023-07-19 13:12:07 浏览: 139
一阶微分算子锐化图像可以使用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;
}
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)