图 像 梯 度 、 图 像 边 缘 、 U S M 锐 化 图像梯度、图像边缘、USM锐化 图像梯度、图像边缘、USM锐化
图像卷积:
1.模糊
2.梯度
3.边缘
4.锐化
1.视频教程:
B站、网易云课堂、腾讯课堂
2.代码地址:
Gitee
Github
3.存储地址:
Google云
百度云:
提取码:
1.图像梯度
1.1 梯度算子
利用梯度算子进行梯度的计算
常见的图像梯度算子
Sobel、Scharr算子,计算x和y方向的差异
一阶导数算子
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat src = imread("E:/cats.jpg", IMREAD_COLOR);
if (src.empty()) {
printf("image is empty!!!");
return -1;
}
imshow("src", src);
// 非均值滤波
Mat robot_x = (Mat_ <int>(2, 2) << 1, 0, 0, -1);
Mat robot_y = (Mat_ <int>(2, 2) << 0, 1, -1, 0);
Mat grad_x,grad_y;
filter2D(src, grad_x, CV_32F, robot_x, Point(-1, -1), 0, BORDER_DEFAULT);
filter2D(src, grad_y, CV_32F, robot_y, Point(-1, -1), 0, BORDER_DEFAULT);
convertScaleAbs(grad_x, grad_x);
convertScaleAbs(grad_y, grad_y);
Mat result;
add(grad_x, grad_y, result);
imshow("robot gradient", result);
waitKey(0);
destroyAllWindows();
return 0;
}
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat src = imread("E:/cats.jpg", IMREAD_COLOR);
if (src.empty()) {
printf("image is empty!!!");
return -1;
}
imshow("src", src);
// robot算子
Mat robot_x = (Mat_ <int>(2, 2) << 1, 0, 0, -1);
Mat robot_y = (Mat_ <int>(2, 2) << 0, 1, -1, 0);
Mat grad_x,grad_y;
filter2D(src, grad_x, CV_32F, robot_x, Point(-1, -1), 0, BORDER_DEFAULT);
filter2D(src, grad_y, CV_32F, robot_y, Point(-1, -1), 0, BORDER_DEFAULT);
convertScaleAbs(grad_x, grad_x);
convertScaleAbs(grad_y, grad_y);
Mat result;
add(grad_x, grad_y, result);
imshow("robot gradient", result);
// Sobel算子
Sobel(src, grad_x, CV_32F, 1, 0);
Sobel(src, grad_y, CV_32F, 0, 1);
convertScaleAbs(grad_x, grad_x);
convertScaleAbs(grad_y, grad_y);
Mat result2;
addWeighted(grad_x, 0.5, grad_y, 0.5, 0, result2);
imshow("sobel gradient", result2);
// Scharr
Scharr(src, grad_x, CV_32F, 1, 0);
Scharr(src, grad_y, CV_32F, 0, 1);
convertScaleAbs(grad_x, grad_x);
convertScaleAbs(grad_y, grad_y);
Mat result3;
addWeighted(grad_x, 0.5, grad_y, 0.5, 0, result3);
imshow("Scharr gradient", result3);
waitKey(0);
destroyAllWindows();
return 0;
}
2.图像边缘
2.1 二阶导数算子
锐化的常用梯度算子:拉普拉斯算子
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat src = imread("E:/cats.jpg", IMREAD_COLOR);
if (src.empty()) {
printf("image is empty!!!");
return -1;
}
imshow("src", src);
Mat dst;
Laplacian(src, dst, -1, 3, 1.0, 0, BORDER_DEFAULT);
imshow("laplacian", dst);
waitKey(0);
destroyAllWindows();
return 0;
}
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat src = imread("E:/cats.jpg", IMREAD_COLOR);
if (src.empty()) {
printf("image is empty!!!");
return -1;
}
imshow("src", src);
// 拉普拉斯算子
Mat dst;
Laplacian(src, dst, -1, 3, 1.0, 0, BORDER_DEFAULT);
imshow("laplacian", dst);
// 锐化
Mat sh_op = (Mat_ <int>(3,3) <<0,-1,0,
-1,5,-1,
0,-1,0);
Mat result;
filter2D(src, result, CV_32F, sh_op, Point(-1, -1), 0, BORDER_DEFAULT);
convertScaleAbs(result, result);
imshow("sharpen",result);
waitKey(0);
destroyAllWindows();
return 0;
}
拉普拉斯容易被噪声影响,所以在使用之前,需要进行去噪
3.USM锐化
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat src = imread("E:/cats.jpg", IMREAD_COLOR);
if (src.empty()) {
printf("image is empty!!!");
return -1;
}
imshow("src", src);
// Unsharpen mask filter
Mat blur_image, dst;
GaussianBlur(src, blur_image, Size(3, 3), 0);
Laplacian(src, dst, -1, 3, 1.0, 0, BORDER_DEFAULT);
imshow("laplacian", dst);
Mat usm_image;
addWeighted(blur_image, 1.0, dst, -1.0, 0, usm_image);
imshow("usm filter", usm_image);
waitKey(0);
destroyAllWindows();
return 0;
}