OpenCV学习笔记2

1. 图像腐蚀与膨胀

    这两个操作都是形态学的变化,将图像与任意的卷积核进行卷积,通常我们使用的卷积核为正方形或者圆形。

    同时在内核中我们也要设置一个锚点,一般为内核的中心点。

    进行腐蚀操作时,将内核 B 划过图像,将内核 B 覆盖区域的最小相素值提取,并代替锚点位置的相素。

    进行膨胀操作时,将内核 B 划过图像,将内核 B 覆盖区域的最大相素值提取,并代替锚点位置的相素。显然,这一最大化操作将会导致图像中的亮区开始”扩展” (因此有了术语膨胀 dilation )。

    1.1卷积核

    因此我们在OpenCV·中使用erode和dilate时要首先设置一个卷积核,我们使用getStructuringElement函数。

Mat  getStructuringElement (int  shape, Size  esize, Point  anchor=Point(-1, -1) )

Returns the structuring element of the specified size and shape for morphological operations

  第一个参数是卷积核的形状,有三个选择MORPH_RECT、MORPH_ELLIPSE、MORPH_CROSS。

   第二个参数是卷积核的大小,用size()函数设置。

    第三个参数是卷积核的锚点,default的point为point(-1,-1).

    1.2 图像的腐蚀

        使用erode()函数,第一个参数为原图像,第二个参数为腐蚀之后存入的Mat,第三个参数为卷积核即我们之前从getStructuringElement返回的值。

            图像腐蚀的作用:

                边缘检测、形态骨架的提取、噪声的消除

     1.3 图像的膨胀

            使用dilate()函数,参数和erode()的参数设计是一样的。

#include<opencv.hpp>
using namespace cv;
int main(){
	Mat img = imread("1.jpg");
	//imshow(" ",img);


	Mat element = getStructuringElement(MORPH_RECT, Size(6, 6));
	Mat dis;
	erode(img, dis, element);
	imshow("", dis);
	dilate(dis, dis, element);
	imshow("dilate", dis);
	waitKey(6000);

}


2. 图像模糊滤波

    一般来说图像中,噪声表现为高频的部分;为了消除图像的噪声,我们可以使用卷积核对图像进行处理,卷积的过程表现为使用一个矩阵对图像的矩阵进行卷积运算,可参照下图。

常用的滤波器有均值滤波器,高斯滤波器等。


#include<opencv.hpp>

using namespace cv;
int main(){
	Mat img = imread("1.jpg");
	imshow(" ",img);

	Mat element = getStructuringElement(MORPH_RECT, Size(6, 6));
	Mat dis;
	blur(img, dis, Size(5, 5));
	imshow("blur", dis);
	GaussianBlur(img, dis, Size(5, 5),0,0);
	imshow("Gaussion", dis);

	waitKey(6000);
}

3. canny边缘检测

先看下文档中的介绍:

void  Canny (const  Mat&  imageMat&  edges, double  threshold1, double  threshold2, int  apertureSize=3, bool  L2gradient=false )

Finds edges in an image using Canny algorithm.

Parameters:
  • image – Single-channel 8-bit input image
  • edges – The output edge map. It will have the same size and the same type as image
  • threshold1 – The first threshold for the hysteresis procedure
  • threshold2 – The second threshold for the hysteresis procedure
  • apertureSize – Aperture size for the Sobel()operator
  • L2gradient – Indicates, whether the more accurateL_2 norm =\sqrt{(dI/dx)^2 + (dI/dy)^2} should be used to compute the image gradient magnitude ( L2gradient=true ), or a faster default L_1 norm=|dI/dx|+|dI/dy| is enough ( L2gradient=false )

#include<opencv.hpp>

using namespace cv;
int main(){
	Mat img = imread("1.jpg");

	Mat dstImage, edge, grayImage;
	cvtColor(img, grayImage, COLOR_RGB2GRAY);
	//imshow("", grayImage);
	blur(grayImage, edge,Size(3,3));
	Canny(edge, edge, 3, 9, 3);
	imshow("canny", edge);
	waitKey(6000);
}

4.将canny运用于视频图像上

#include<opencv.hpp>

using namespace cv;
int main(){
	VideoCapture cap(0);
	Mat edges;
	if (!cap.isOpened())  // check if we succeeded
		return -1;
	while (1){
		Mat frame;
		cap >> frame;
		cvtColor(frame, frame, COLOR_RGB2GRAY);
		blur(frame, frame, Size(3, 3));
		Canny(frame, frame, 0, 30, 3);
		imshow("", frame);
		if (waitKey(30) >= 0)break;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值