目录
Sobel算子
cv::Sobel ( InputArray Src // 输入图像 OutputArray dst// 输出图像,大小与输入图像一致 int depth // 输出图像深度. Int dx. // X方向,几阶导数 int dy // Y方向,几阶导数. int ksize, SOBEL算子kernel大小,必须是1、3、5、7、 double scale = 1 double delta = 0 int borderType = BORDER_DEFAULT )
cv::Scharr ( InputArray Src // 输入图像 OutputArray dst// 输出图像,大小与输入图像一致 int depth // 输出图像深度. Int dx. // X方向,几阶导数 int dy // Y方向,几阶导数. double scale = 1 double delta = 0 int borderType = BORDER_DEFAULT )
#include<iostream>
#include<opencv2/opencv.hpp>
#include<cmath>
using namespace std;
using namespace cv;
Mat src, dst, gray_src, temp;
int main()
{
src = imread("D:/opencv/2/a.jpg");
if (!src.data)
{
printf("could not load image...\n");
return -1;
}
imshow("input", src);
//高斯模糊,进行降噪
GaussianBlur(src, temp, Size(3, 3), 0, 0, BORDER_DEFAULT);
//转灰度图
cvtColor(temp, gray_src, COLOR_BGR2GRAY);
Mat xgrad, ygrad;
//Sobel算子 -1 0 1 -2 0 2 -1 0 1
Sobel(gray_src, xgrad, CV_16S, 1, 0, 3);
Sobel(gray_src, ygrad, CV_16S, 0, 1, 3);
//opencv改进算法 Scharr算子 -3 0 3 -10 0 10 -3 0 -3 优势 降低噪点带来的影响
// Scharr(gray_src,xgrad,CV_16S,1,0);
// Scharr(gray_src,ygrad,CV_16S,0,1);
//将像素点限制在0-255之间 进行绝对值
convertScaleAbs(xgrad, xgrad);
convertScaleAbs(ygrad, ygrad);
//混合处理 //对融合后的像素值额外添加rgb值
add(xgrad, ygrad, dst);
imshow("output1", dst);
addWeighted(xgrad, 0.5, ygrad, 0.5, 0, dst);
imshow("output2", dst);
dst = Mat(xgrad.size(), xgrad.type());
int rows = xgrad.rows;
int cols = xgrad.cols;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
int x = xgrad.at<uchar>(row, col);
int y = ygrad.at<uchar>(row, col);
dst.at<uchar>(row, col) = saturate_cast<uchar>(sqrt(x * x + y * y));
}
}
imshow("output3", dst);
waitKey(0);
return 0;
}
Laplance算子
处理流程
高斯模糊 – 去噪声GaussianBlur()
转换为灰度图像cvtColor()
拉普拉斯 – 二阶导数计算Laplacian()
取绝对值convertScaleAbs()
显示结果
cv::Laplacian( InputArray src, OutputArray dst, int depth, //深度CV_16S int kisze, // 3 double scale = 1, double delta =0.0, int borderType = 4 )
#include<iostream>
#include<opencv2/opencv.hpp>
#include<cmath>
using namespace std;
using namespace cv;
Mat src,dst,gray_src,temp;
int main(){
src=imread("img/2.jpg");
if(!src.data){
printf("could not load image...\n");
return -1;
}
imshow("input",src);
GaussianBlur(src,temp,Size(3,3),0,0);
cvtColor(temp,gray_src,COLOR_BGR2GRAY);
//拉普拉斯算子 用于边缘提取
Laplacian(gray_src,dst,CV_16S,3);
//取像素点的绝对值
convertScaleAbs(dst,dst);
threshold(dst,dst,0,255,THRESH_OTSU | cv::THRESH_BINARY);
Mat kernel= getStructuringElement(MORPH_RECT,Size(7,7),Point(-1,-1));
Mat kernel1= getStructuringElement(MORPH_RECT,Size(3,3),Point(-1,-1));
morphologyEx(dst,dst,MORPH_CLOSE,kernel,Point(-1,-1));
morphologyEx(dst,dst,MORPH_OPEN,kernel1,Point(-1,-1));
morphologyEx(dst,dst,MORPH_CLOSE,(getStructuringElement(MORPH_RECT,Size(5,5),Point(-1,-1))),Point(-1,-1));
imshow("laplacian",dst);
waitKey(0);
return 0;
}