zl程序教程

您现在的位置是:首页 >  其他

当前栏目

openCV图像变换

2023-03-14 22:49:58 时间

一. 边缘检测


1.实现步骤:
1 消除噪声 ,使用高斯平滑滤波器卷积降噪。
2 计算梯度幅值和反向
3 非极大值抑制
4 阈值

2.Canny边缘检测:Canny()函数

c++:
    void Canny(InputArray image,OutputArray edges, double threshold1,
    double threshold2,int apertureSize = 3,bool L2gradient = flase)
1.第一个参数,输入的图像
2. 第二个参数,输出的图像
3. 第三个参数,阈值1
4. 第四个参数,阈值2
5. 第五个参数,Sobel算子的孔径大小,默认为3
6. 第六个参数,一个计算图像梯度幅值的标识,某类为false

3.程序实例

#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace cv;

int main(void)
{
    Mat src = imread("1.jpg");
    Mat dst,src1=src.clone();
    
    Mat edge,gray,edge1;
    dst.create(src1.size(),src1.type());
    cvtColor(src1,gray,COLOR_BGR2GRAY);     //转换成灰度图片
    blur(gray,edge,Size(3,3); //降噪
    Canny(edge, edge1,3,9,3);
    dst = Scalar::all(0);       // 将所有元素都设置为0
    src1.copyTo(dst,edge);
    imshow("B",dst);
    waitKey(0);
    return 0;
}

二. sobel算子


  1. 计算过程
    1 分别在x和y两个方向求导
    2 在图像的每一点,结合以上两个求出近似梯度
  2. Sobel算子:Sobel()函数
c++ :
void Sobel(InputArray src,OutputArray dst,int ddepth,int dx,int dy,int ksize=3,
double scale=1,double delta = 0,int borderType=BORDER_DEFAULT);
1. 第一个参数:输入的图片
2. 第二个参数:输出的图片
3. 第三个参数:输出图像的深度
4. 第四个参数:int类型dx,x方向上的差分阶数
5. 第五个参数:int类型dy,y方向上的差分阶数
6. 第六个参数:int类型的ksize,默认为3,表示Sobel核的大小,必须取1,3,5,7
7. 第七个参数:double类型的scale,计算时的缩放因子
8. 第八个参数:double类型的delta,表示偏移值。
9. 第九个参数:边界模式,默认为BORDER_DEFAULT

3.程序实例

#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace cv;

int main(void)
{
    Mat grad_x,grad_y;
    Mat abs_grad_x,abs_grad_y,dst;
    Mat src = imread("1.jpg");
    imshow("AA",src);
    // 计算x方向的梯度
    Sobel(src,grad_x,CV_16S,1,0,3,1,1,BORDER_DEFAULT);
    convertScaleAbs(grad_x,abs_grad_x);
    imshow("BB",abs_grad_x);
    // 计算y方向的梯度。
    Sobel(src,grad_y,CV_16S,0,1,3,1,1,BORDER_DEFAULT);
    convertScaleAbs(grad_y,abs_grad_y);
    imshow("CC",abs_grad_y);

    waitkey(0);
    return 0;
}


三. 拉普斯拉变换


  1. 1.Laplaceian()函数
c++:
void Laplaceian(InputArray src,OutputArray dst,int ddepth,int ksize=1,
double scale=1,double delta=0,intborderType=BORDER_DEFAULT);
1. 第一个参数:输入的图片
2. 第二个参数:输出的图片
3. 第三个参数:目标图像的深度
4. 第四个参数:计算二阶导数的滤波器的孔径尺寸
5. 第五个参数:缩放因子
6. 第六个参数:偏移值
7. 第七个参数:边界模式

2.程序实例

#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace cv;

int main(void)
{
    Mat src,src1,src_gray,dst,abs_gray;
    src = imread("1.jpg");
    
    GaussianBlur(src,src1,Size(3,3),0,0,BORDER_DEFAULT);
    cvtColor(src1,src_gray,COLOR_RGB2GRAY);
    
    Laplaceian(src_gray,dst,CV_16S,3,1,0,BORDER_DEFAULT);
    convertScaleAbs(dst,abs_dst);
    
    imshow("A",abs_dst);
    waitKey(0);
    return 0;


}

三. scharr滤波器


使用Scharr滤波器分别计算x或y方向的图像差分。

c++:
void Scharr(InputArray src,OutputArray dst,int ddepth,int dx,int dy,
double scale=1,double delta=0,intborderType = BORDER_DEFAULT);
1. 第一个参数:输入图像
2. 第二个参数:输出图像
3. 第三个参数:输出图像深度
4. 第四、五个参数:计算x,y方向的梯度
5. 第六个参数:缩放因子
6. 第七个参数:偏移值
7. 第八个参数:边界模式

2.程序实例

#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace cv;

int main(void)
{
    Mat grad_x,grad_y;
    Mat abs_grad_x,abs_grad_y,dst;
    Mat src = imread("1.jpg");
    
    Scharr(src,grad_x,CV_16S,1,0,1,0,BORDER_DEFAULT);
    convertScaleAbs(grad_x,abs_grad_x);
    imshow("A",abs_grad_x);
    
    Scharr(src,grad_y,CV_16S,0,1,1,0,BORDER_DEFAULT);
    convertScaleAbs(grad_y,abs_grad_y);
    imshow("B",abs_grad_y);

    waitKey(0);
    return 0;

}

公众号:FPGA之旅