實驗四:圖像空間域銳化
[實驗目的]
· 理解圖像空間域銳化的目的和意義;
· 瞭解圖像空間域銳化的各種方法及優缺點;
· 掌握圖像空間域的鄰域運算方法;
· 掌握圖像銳化處理算法及流程;
· 編程實現圖像 duck.jpg 的銳化;
· 理解空間高通濾波法與其他各種圖象銳化的區別和聯繫。
· 總結實驗過程。
[實驗要求]
\1. 編寫圖像空間域銳化程序:
· 編程對圖像作各種銳化處理,編寫圖像空間域銳化子程序,子程序包括:梯度銳化、Roberts 銳化、Prewitt 銳化、Sobel 銳化、 Laplace 銳化及高通濾波法;
· 用不同的銳化方法處理圖像,觀察不同的圖像銳化方法,不同模板對圖像銳化的處理效果。
\2. 觀察圖像銳化處理結果
· 利用Photoshop 對圖像實施銳化處理。分析對比 Photoshop 和自己編寫的圖像銳化程序處理效果不同之處,並設法改經自己的圖像處理程序。
代碼及處理結果
過程中採用的filter2D函數,官方文檔中提及:“The function does actually compute correlation, not the convolution”,經考慮後我認爲在此不考慮相關和卷積操作的具體差異,僅僅將filter2D作爲代替實現核與圖像相乘的手段。
不過官方文檔中同樣提及了轉化方法:
That is, the kernel is not mirrored around the anchor point. If you need a real convolution, flip the kernel using flip and set the new anchor to (kernel.cols - anchor.x - 1, kernel.rows - anchor.y - 1)
.
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
// 銳化方式
//int method { 1.Highboost, 2.PrewittH1, 3.PrewittH2, 4.RobertsH1, 5.RobertsH2, 6.Laplace1, 7.Laplace2 , 8.SobelH1, 9.SobelH2};
void Sharpen(Mat& src, Mat& dst, int method = 1)
{
Mat kern = (Mat_<char>(3, 3)
<< 0, 0, 0,
0, 1, 0,
0, 0, 0);
// 根據方法確定核
if (method == 1)//高頻增強模板1
{
kern = (Mat_<char>(3, 3)
<< 0, -1, 0,
-1, 5, -1,
0, -1, 0);
}
else if (method == 2)//Prewitt算子H1
{
kern = (Mat_<char>(3, 3)
<< -1, 0, 1,
-1, 0, 1,
-1, 0, 1);
}
else if (method == 3)//Prewitt算子H2
{
kern = (Mat_<char>(3, 3)
<< -1, -1, -1,
0, 0, 0,
1, 1, 1);
}
else if (method == 4)//Roberts算子H1
{
kern = (Mat_<char>(2, 2)
<< 0, -1,
1, 0);
}
else if (method == 5)//Roberts算子H2
{
kern = (Mat_<char>(2, 2)
<< -1, 0,
0, 1);
}
else if (method == 6)//Laplace模板1
{
kern = (Mat_<char>(3, 3)
<< 0, -1, 0,
-1, 4, -1,
0, -1, 0);
}
else if (method == 7)//Laplace模板2
{
kern = (Mat_<char>(3, 3)
<< -1, -1, -1,
-1, 8, -1,
-1, -1, -1);
}
else if (method == 8)//SobelH1
{
kern = (Mat_<char>(3, 3)
<< -1, 0, 1,
-2, 0, 2,
-1, 0, 1);
}
else if (method == 9)//SobelH2
{
kern = (Mat_<char>(3, 3)
<< -1, -2, -1,
0, 0, 0,
1, 2, 1);
}
// 處理
filter2D(src, dst, src.depth(), kern);
}
int main()
{
//讀入數據
Mat src = imread("duck.jpg");
//含兩個模板(H1,H2)的需要將兩個結果相加,故有dst_1和dst_2
Mat dst_1 = src.clone();
Mat dst_2 = src.clone();
Mat dst = src.clone();
cout << "多模板銳化演示開始:按任意鍵繼續……" << endl;
//高通濾波法
Sharpen(src, dst, 1);
namedWindow("高通濾波法");
imshow("高通濾波法", dst);
waitKey(0);
//Prewitt
Sharpen(src, dst_1, 2);
Sharpen(src, dst_2, 3);
dst = dst_1 + dst_2;
namedWindow("PreWitt");
imshow("Prewitt", dst);
waitKey(0);
//Roberts
Sharpen(src, dst_1, 4);
Sharpen(src, dst_2, 5);
dst = dst_1 + dst_2;
namedWindow("Roberts");
imshow("Roberts", dst);
waitKey(0);
//Laplace1
Sharpen(src, dst, 6);
namedWindow("Laplace-模板一");
imshow("Laplace-模板一", dst);
waitKey(0);
//Laplace2
Sharpen(src, dst, 7);
namedWindow("Laplace-模板二");
imshow("Laplace-模板二", dst);
waitKey(0);
//Sobel
Sharpen(src, dst_1, 8);
Sharpen(src, dst_1, 9);
dst = dst_1 + dst_2;
namedWindow("Sobel");
imshow("Sobel", dst);
cout << "展示結束,按任意鍵關閉程序。" << endl;
waitKey(0);
destroyAllWindows();
return 0;
}
Ps中有USM銳化,銳化,進一步銳化,防抖,銳化邊緣等功能,網上多從使用角度解讀,我暫不能分清楚除了USM(Unsharp Mask)以外的銳化具體是哪一種。
duck(ps中USM銳化後):
USM(Unsharp Mask)參數
參考資料
- Opencv官方文檔(filter2D):https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#ga27c049795ce870216ddfb366086b5a04
- 圖像處理中濾波(filtering)與卷積(convolution)的區別:https://blog.csdn.net/q6324266/article/details/52374234
- Photoshop的USM銳化功能詳解:http://www.sj33.cn/jc/pmjc/ps/201001/21915.html
mgproc__filter.html#ga27c049795ce870216ddfb366086b5a04 - 圖像處理中濾波(filtering)與卷積(convolution)的區別:https://blog.csdn.net/q6324266/article/details/52374234
- Photoshop的USM銳化功能詳解:http://www.sj33.cn/jc/pmjc/ps/201001/21915.html
- 【OpenCV學習筆記】之卷積及卷積算子(convolution):https://blog.csdn.net/zhu_hongji/article/details/81562746