0
点赞
收藏
分享

微信扫一扫

opencv c++学习三(模糊 边缘检测 膨胀 腐蚀)

googlefrank 2022-01-31 阅读 67

目录

1.模糊

1.1 均值模糊

1.2 高斯滤波

2.canny边缘检测

3.膨胀与侵蚀


1.模糊

Smooth/Blur 是图像处理中最常用和最简单的操作之一

使用该操作的原因主要是为了 在图像预处理时降低噪声

简单的原理:

1.1 均值模糊

1.2 高斯滤波

高斯滤波的原理

int main(int argc, char** argv) {
	/*先显示一下图片*/
	string path = "resouces\\test.png";
	Mat src = imread(path);
	if (src.empty()) {
		cout << "could not load image!" << endl;
	}
	namedWindow("input", CV_WINDOW_AUTOSIZE);
	imshow("input", src);

	
	Mat blur_dst,Gaussianblur_dst;
	blur(src, blur_dst, Size(5, 5), Point(-1, -1));                  //均值模糊
	GaussianBlur(src, Gaussianblur_dst, Size(5, 5), 11, 11);         //高斯模糊
	imshow("blur", blur_dst);
	imshow("Gaussianblur", Gaussianblur_dst);
	waitKey(0);
	return 0;
}

2.canny边缘检测

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

/// Basic Functions //

void main() {

	string path = "resouces\\test.png";
	Mat img = imread(path);
	Mat imgGray, imgBlur, imgCanny;

	cvtColor(img, imgGray, COLOR_BGR2GRAY);                         //变成灰度图
	GaussianBlur(imgGray, imgBlur, Size(7, 7), 5, 0);               //高斯模糊
	Canny(imgBlur, imgCanny, 25, 75);								//canny边缘检测

	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));

	imshow("Image", img);
	imshow("Image Gray", imgGray);
	imshow("Image Blur", imgBlur);
	imshow("Image Canny", imgCanny);
	waitKey(0);
}

 

3.膨胀与侵蚀

#include<opencv2/imgcodecs.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<math.h>
#include<iostream>
using namespace cv;
using namespace std;

int main(int argc, char** argv) {
	/*先显示一下图片*/
	string path = "resouces\\test.png";
	Mat src = imread(path);
	if (src.empty()) {
		cout << "could not load image!" << endl;
	}
	namedWindow("input", CV_WINDOW_AUTOSIZE);
	imshow("input", src);


	Mat Gray_dst, Gaussianblur_dst,Canny_dst,Dil_dst,Ero_dst,kernel;
	cvtColor(src, Gray_dst, CV_BGR2GRAY);
	GaussianBlur(Gray_dst, Gaussianblur_dst, Size(5, 5), 11, 11);  //高斯模糊
	Canny(Gaussianblur_dst, Canny_dst, 25, 75);                    //Canny边缘测试
	kernel = getStructuringElement(MORPH_RECT, Size(3, 3));        //做一个kernel内核
	dilate(Canny_dst, Dil_dst, kernel);                            //膨胀
	erode(Dil_dst, Ero_dst, kernel);                               //侵蚀
	imshow("Gaussianblur", Gaussianblur_dst);
	imshow("Canny", Canny_dst);
	imshow("Dil", Dil_dst);
	imshow("Ero", Ero_dst);
	waitKey(0);
	return 0;
}

举报

相关推荐

0 条评论