opencv中inshow,waitkey,erode,blur,canny,cvtColor函数的用法
时间: 2024-05-18 13:16:18 浏览: 22
1. imshow函数:用于在窗口中显示图像,其函数原型为:
`void imshow(const string& winname, InputArray mat);`
参数 winname 表示窗口名称,mat 表示要显示的图像。示例代码如下:
```c++
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("lena.jpg");
imshow("lena", img);
waitKey(0);
return 0;
}
```
2. waitKey函数:用于等待键盘输入,其函数原型为:
`int waitKey(int delay = 0);`
参数 delay 表示等待时间(毫秒),如果 delay 为 0,则表示一直等待键盘输入。示例代码如上。
3. erode函数:用于腐蚀图像,其函数原型为:
`void erode(InputArray src, OutputArray dst, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar& borderValue=morphologyDefaultBorderValue() );`
参数 src 表示输入图像,dst 表示输出图像,kernel 表示腐蚀的核,anchor 表示核的锚点,iterations 表示腐蚀次数,borderType 表示边界类型,borderValue 表示边界值。示例代码如下:
```c++
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("lena.jpg");
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
Mat dst;
erode(img, dst, kernel);
imshow("erode", dst);
waitKey(0);
return 0;
}
```
4. blur函数:用于平滑图像,其函数原型为:
`void blur(InputArray src, OutputArray dst, Size ksize, Point anchor=Point(-1,-1), int borderType=BORDER_DEFAULT );`
参数 src 表示输入图像,dst 表示输出图像,ksize 表示平滑核的大小,anchor 表示核的锚点,borderType 表示边界类型。示例代码如下:
```c++
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("lena.jpg");
Mat dst;
blur(img, dst, Size(7, 7));
imshow("blur", dst);
waitKey(0);
return 0;
}
```
5. canny函数:用于边缘检测,其函数原型为:
`void Canny(InputArray image, OutputArray edges, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false );`
参数 image 表示输入图像,edges 表示输出的边缘图像,threshold1 和 threshold2 表示边缘检测的两个阈值,apertureSize 表示 Sobel 滤波器的大小,L2gradient 表示是否使用 L2 范数计算梯度。示例代码如下:
```c++
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("lena.jpg", IMREAD_GRAYSCALE);
Mat edges;
Canny(img, edges, 50, 150);
imshow("canny", edges);
waitKey(0);
return 0;
}
```
6. cvtColor函数:用于颜色空间转换,其函数原型为:
`void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 );`
参数 src 表示输入图像,dst 表示输出图像,code 表示转换的类型,dstCn 表示输出图像的通道数。示例代码如下:
```c++
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("lena.jpg");
Mat hsv_img;
cvtColor(img, hsv_img, COLOR_BGR2HSV);
imshow("hsv", hsv_img);
waitKey(0);
return 0;
}
```
以上就是 OpenCV 中 inshow、waitkey、erode、blur、canny、cvtColor 函数的用法。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)