java实现图像处理高通滤波,图像处理入门——滤波 - leo_de_macondo的个人页面 - OSCHINA - 中文开源技术交流社区...

f6a6b64994fd9e5e73e5d457163c260b.png

模糊

图像模糊的方法可以将每个像素的rgb值用周围像素的加权平均值来代替。比如用周围的9个像素来计算加权平均值,权值可以用一个3x3的矩阵来表示:

| 1   2   1 |

| 2   4   2 |   *  (1/16)

| 1   2   1 |

中间的像素是要处理的像素,越靠近中间权值越大;所有权值的和为1。用平均值代替原像素的rgb值之后,每个像素于周围像素的差异程度变小了;该矩阵相当于一个二维的低通滤波器。下面是c#的代码

public Bitmap FilterBitmap(Bitmap bmp)

{

// 创建如下3x3矩阵:

// | 1 2 1 |

// (1/16) * | 2 4 2 |

// | 1 2 1 |

double[,] core = new double[,] { { 1, 2, 1 }, { 2, 4, 2 }, { 1, 2, 1 } };

int scale = 16;

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

core[i, j] /= scale;

Bitmap retBmp = new Bitmap(bmp.Width, bmp.Height);

double r, g, b;

int width = bmp.Width - 1;

int height = bmp.Height - 1;

// 循环处理第2行开始到倒数第2行;第2列开始到倒数第2列的像素,

// 因为第最外面一圈的像素周围没有8个像素

for (int y = 1; y < height; y++)

{

for (int x = 1; x < width; x++)

{

// 将周围8个像素的rgb加权平均值作为被处理像素的值

r = g = b = 0;

for (int j = -1; j <= 1; j++)

{

for (int i = -1; i <= 1; i++)

{

Color pix = bmp.GetPixel(x + i, y + j);

r += core[i + 1, j + 1] * pix.R;

g += core[i + 1, j + 1] * pix.G;

b += core[i + 1, j + 1] * pix.B;

}

}

// 确保rgb的值在0到255

if (r < 0) r = 0; if (r > 255) r = 255;

if (b < 0) b = 0; if (b > 255) b = 255;

if (g < 0) g = 0; if (g > 255) g = 255;

retBmp.SetPixel(x, y, Color.FromArgb((int)r, (int)g, (int)b));

}

}

return retBmp;

}

动态模糊或径向模糊等处理方法也是用加权平均值代替原像素的值,只是用于计算平均值的像素是一条曲线或射线上的像素而不是周围一圈的像素。

锐化

相对于模糊,锐化可以让图像看似更加清晰。方法是把每一个像素与周围像素的差异放大。同样可以用一个3x3矩阵来表示周围9个像素的权值,权值相加为1;与用于模糊的矩阵不同的是,用于锐化的矩阵中间像素的权值是正的,而周围的权值是负的。以下面的矩阵为例

| 0   –1    0 |

| –1   5   -1 |

| 0   –1    0 |

中间像素的rgb值先增大5倍,再与周围4个像素的rgb值做差,即:该像素 +(该像素-上面像素)+(该像素-下面像素)+(该像素-左边像素)+(该像素-右边像素)。结果是,如果周围像素相对该像素较暗,则差值为正,使得该像素更亮;反之如果周围像素相对较亮,则使得该像素更暗。该矩阵具有动态增加对比度的效果,相当于一个高通滤波器,

浮雕效果

浮雕效果与锐化的方法相似,稍不同的是矩阵中的权值相加为0,比如把上面矩阵中间的5改成4,用该像素与周围像素的差值代替该像素:

| 0    –1    0 |

| –1    4   -1 |

| 0    –1    0 |

不过一般的浮雕效果是有光照方向感的,比如东南方向(只与东南方向的像素对比):

| 0     0    0 |

| 0    2   -1 |

| 0   –1    0 |

效果是,如果该像素与周围相同,则该像素rgb的值变成0;如果该像素比周围像素亮,则差值为正;否则差值为负。但因为rgb的值只能是0~255,所以要把处理后的rgb值加上一个正的偏移量。

public Bitmap FilterBitmap(Bitmap bmp)

{

// 创建如下3x3矩阵:

// | 0 0 0 |

// | 0 2 -1 |

// | 0 -1 0 |

int[,] core = new int[,] { { 0, 0, 0 }, { 0, 2, -1 }, { 0, -1, 0 } };

// 偏移量

int offset = 100;

Bitmap retBmp = new Bitmap(bmp.Width, bmp.Height);

int width = bmp.Width - 1;

int height = bmp.Height - 1;

int r, g, b;

for (int y = 1; y < height; y++)

{

for (int x = 1; x < width; x++)

{

r = g = b = 0;

for (int j = -1; j <= 1; j++)

{

for (int i = -1; i <= 1; i++)

{

Color pix = bmp.GetPixel(x + i, y + j);

r += core[i + 1, j + 1] * pix.R;

g += core[i + 1, j + 1] * pix.G;

b += core[i + 1, j + 1] * pix.B;

}

}

// 加上偏移量

r += offset;

g += offset;

b += offset;

if (r < 0) r = 0; if (r > 255) r = 255;

if (g < 0) g = 0; if (g > 255) g = 255;

if (b < 0) b = 0; if (b > 255) b = 255;

retBmp.SetPixel(x, y, Color.FromArgb(r, g, b));

}

}

return retBmp;

}

锐化与浮雕处理可以应用于图像的边缘检测

以上主要参考《图像编程精髓——从开发自己的Photoshop开始》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 SQL 实现图书管理系统的完整代码: 创建书籍信息表: ```sql CREATE TABLE books ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, author VARCHAR(255) NOT NULL, publisher VARCHAR(255) NOT NULL, publish_date DATE NOT NULL, price DECIMAL(7, 2) NOT NULL, isbn VARCHAR(20) NOT NULL, summary TEXT, status ENUM('available', 'borrowed') NOT NULL DEFAULT 'available' ); ``` 创建借阅记录表: ```sql CREATE TABLE borrow_records ( id INT AUTO_INCREMENT PRIMARY KEY, book_id INT NOT NULL, borrower_name VARCHAR(255) NOT NULL, borrow_date DATE NOT NULL, return_date DATE, FOREIGN KEY (book_id) REFERENCES books(id) ); ``` 插入一些书籍信息: ```sql INSERT INTO books (title, author, publisher, publish_date, price, isbn, summary) VALUES ('The Great Gatsby', 'F. Scott Fitzgerald', 'Charles Scribner\'s Sons', '1925-04-10', 12.99, '978-0743273565', 'The story primarily concerns the young and mysterious millionaire Jay Gatsby and his quixotic passion and obsession for the beautiful former debutante Daisy Buchanan.'), ('To Kill a Mockingbird', 'Harper Lee', 'J. B. Lippincott & Co.', '1960-07-11', 10.99, '978-0061120084', 'The novel is renowned for its warmth and humor, despite dealing with serious issues of rape and racial inequality.'), ('1984', 'George Orwell', 'Secker and Warburg', '1949-06-08', 9.99, '978-0451524935', 'The novel is set in Airstrip One, a province of the superstate Oceania in a world of perpetual war, omnipresent government surveillance, and public manipulation.'), ('Pride and Prejudice', 'Jane Austen', 'T. Egerton', '1813-01-28', 7.99, '978-0486284736', 'The novel follows the character development of Elizabeth Bennet, the dynamic protagonist of the book who learns about the repercussions of hasty judgments and comes to appreciate the difference between superficial goodness and actual goodness.'), ('One Hundred Years of Solitude', 'Gabriel García Márquez', 'Harper & Row', '1967-05-30', 14.99, '978-0060883287', 'The novel tells the multi-generational story of the Buendía family, whose patriarch, José Arcadio Buendía, founds the town of Macondo, the metaphoric Colombia.'); ``` 借阅一本书: ```sql INSERT INTO borrow_records (book_id, borrower_name, borrow_date) VALUES (1, 'John Smith', '2021-01-01'); ``` 归还一本书: ```sql UPDATE borrow_records SET return_date = '2021-01-10' WHERE book_id = 1; ``` 查找所有可借阅的书籍: ```sql SELECT * FROM books WHERE status = 'available'; ``` 查找某个作者的所有书籍: ```sql SELECT * FROM books WHERE author = 'Jane Austen'; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值