Opencv entry (C ++)

OpenCV (C ++) Getting Started

After we see a new general knowledge will be very confused, I do not know where to start, do not know what software, and then today I'll write a entry OpenCV for everyone off the ground, watching the beginning basic will know how to learn the OpenCV.

In fact Opencv is a library related to visual processing, it is time to learn and use the C language math, time that a bunch of libraries, learning process is actually a library:
1. Understand the library is doing
2. Learning the library how that comes with some of the functions should be used, and what effect these functions
3. after learning several functions, by combining these functions to achieve some complex functions.

Emphasize that this article is talking about Windows on the C ++ syntax OpenCV, the next course is speaking C ++ syntax, not the python.

Then talk about what software to develop OpenCV, the general configuration is to use a VS configuration opencv library to study, attached below the required installation package link.
VS2019 extraction code: 5162
on Opencv-4.1.0 extraction code: n7lz
pay attention to grammar opencv there is a change, from the beginning of 2-3, and now updated to 4. 3 and 4 wherein the syntax are substantially the same, 2, and 3 have some differences in the parameters of some functions, so in portable code, if found move over given, there may be different versions Opencv, change it to a given time function parameters on it.

During the installation of four steps:
1. Replace the VS2019
2. installed Opencv library
3. Place Opencv library into the environment variable
4. build a project in VS, the project connected to the library on Opencv
concrete steps to give us a I used to use it every time with the link (somewhat cumbersome step, I will not write myself ......) VS2019 configuration Opencv tutorial

Because it is started, so we talk about the content of the following five are some relatively simple function, but also very interesting.
1. Image display
2. gradation image
3. blurred image
4. Edge Detection
5. Save Photos

Before proceeding, we need to put a picture into their new project at a my example, my new project is OpencvTest1 , I find the path where this project main.cpp, put a bilibili.jpg to this place.
Here Insert Picture Description
1. Show Pictures

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main()
{
 Mat src = imread("bilibili.jpg");
 
 imshow("lalala",src);
 
 waitKey(0);
 return 0;
}

Effect: the emergence of a small black box and a picture.
Here Insert Picture Description
Here we can to explain the content of this code.

#include <opencv2/opencv.hpp>  
#include <iostream> 
这两句话是分别引入opencv的库和c++的标准库
using namespace std;
using namespace cv;
这两句是引入工作空间,std是c++标准库的工作空间名,cv是opencv的工作空间名。
(工作空间的目的是避免 如果两个库有相同的函数名,在调用函数时发生冲突,
 上文这两个库里没有重复的函数名,所以直接引入工作空间不会有影响。)
int main()
{
 Mat src = imread("bilibili.jpg");
 
 imshow("lalala",src);
 
 waitKey(0);
 return 0;
}

//这一段是主函数。
Mat src = imread("bilibili.jpg");//其实是两条语句合在一起。分别为:
Mat src;//定义一个Mat类型的变量,变量名是src
src = imread("bilibili.jpg");//调用imread函数读取图片并把这张图片赋值给src。

imshow("lalala",src);//调用函数imshow展示图片,第一个参数lalala是显示图片所用的窗口的名字,src是上面刚刚被一张图片赋值的变量。

waitkey(0);//显示图片后开始等待下一步操作。如果不加这条命令的话,自己屏幕上会有一个黑框一闪而过,
//图片也不会显示出来,因为程序没有停下来,显示图片后直接关闭了程序。

return 0;//程序结束的标志,C语言上常用。因为这个主函数的返回值类型是int,所以最后返回一个0让程序结束。

In this way a simple Opencv program was written, his role is to call a picture and displayed.
PS:
1. using imread () reads a function of time if the picture is in the folder where the file main.cpp, you can enter the name of the image to read the image directly.
Of course, we can also enter an absolute path picture to read picture. As imread ( "D: \\ vs \\ OpencvTest1 \\ OpencvTest1 \\ bilibili.jpg")
Note that when I use a separate path "\\" instead of "\", because according to C / C ++ syntax, only write a "\", may be treated as an escape character, so that the "\" can not effect, so use "\\" to separate between the paths.
Of course not "\\", then you can use / separated, such as: imread: ( "D /vs/OpencvTest1/OpencvTest1/bilibili.jpg")
2. imshow () The first argument is the name of the window
Here Insert Picture Description

2. The image gradation
display gray-scale image in a conventional manner, there are two: the first is to read in the read gradation image format time. The second is to read into the picture, and then convert the picture.
the first method:

#include <opencv2/opencv.hpp>  
#include <iostream> 
using namespace std;
using namespace cv;
int main()
{
 Mat src = imread("bilibili.jpg",0);
 
 imshow("lalala",src);
 
 waitKey(0);
 return 0;
}
//可以发现其实和第一次的代码区别不大,只是修改了一下imread的参数
 Mat src = imread("bilibili.jpg",0);
 //imread()函数有两个参数,第一个参数是图片的路径或图片的名字,第二个参数是以何种格式来读取图片。
 //默认值是1,也就是原本的格式读取;如果输入参数为0的话就是以黑白格式读取。

effect:

Here Insert Picture Description
The second method:
call cvtColor () function

#include <opencv2/opencv.hpp>  
#include <iostream> 
using namespace std;
using namespace cv;
int main()
{
 Mat src = imread("bilibili.jpg");
 Mat dst;
 cvtColor(src,dst,COLOR_BGR2GRAY);
 imshow("lalala",dst);
 
 waitKey(0);
 return 0;
}
//我们的操作是新建了一个Mat对象dst,又调用了函数cvtColor()来进行图片的灰度化,最终显示的图片也变成了dst。
 cvtColor(src,dst,COLOR_BGR2GRAY);//第一个参数是输入图像,第二个参数是输出图像,第三个参数是进行何种转换。
 //BGR2GRAY的意思是图片由BGR(三原色)格式变为灰度图格式。

Effect: Here Insert Picture Description
3. fuzzy picture
presentation time blurred picture, we must first introduce three morphological operations: erosion, dilation, filtering.
Corrosion: erode () will remove a picture border point.
Expansion: dilate () on the border of the image to expand.
Follows an expansion and corrosion renderings, these two operations are just the opposite, they operated combination composition can opening operation and closing operation, the top cap, and a series of black hat morphological processing.

#include <opencv2/opencv.hpp>  
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
 Mat src = imread("bilibili.jpg");
 imshow("src", src);
 Mat dst1,dst2;
 Mat element = getStructuringElement(MORPH_RECT, Size(15, 15));
 erode(src, dst1, element);
 imshow("erode",dst1);
 dilate(src, dst2, element);
 imshow("dilate", dst2);
 
 waitKey(0);
 return 0;
}

Effect: Here Insert Picture Description
Filtering : Filtering is used to de-noising, outside interference may mask some of the features of the image, so that the image is blurred, the use of filter can make image features more obvious. (PS: is more obvious characteristics do not necessarily reflect the picture looks clearer.)

Talking picture filtering principle to talk about before some prior knowledge (planning focus, it is important here).
First, we previously defined variables that can accept the picture, it is the type Mat , this is actually a matrix type variable. In fact the picture is a large matrix, such as we see a picture pixel value is 100 * 100, in fact, that this image is a big matrix of 100 * 100, each value in the matrix of the three primary colors are mixed out of one color , by each image pixel to adjust the color of the image so that the more colorful. (If you can find that low-pixel picture, we can see that it is almost a different color too small boxes, in fact, those small square pixels.)
That said, we can talk about the principle of filtering is What, we get a picture, it is equivalent to getting a large matrix. We conducted the first large matrix partition , such as the large matrix of 100 * 100 is divided into 25 small 4 x 4 matrix. The so-called image feature is actually the eigenvalues of the matrix, we want a more obvious image features, it is hoped the pixel values of the image becomes more features. So we can for each small matrix 4 * 4 some operating values, such as looking for a median of these numbers, the average weighted value, after finding eigenvalues of the value matrix replacement is characterized, we Pictures the feature is even more obvious.

We give one of the most common mean filtering example:
If we have a large matrix need to be mean filter, in which we first find a small 3x3 matrix operate it, in fact, most middle of the squared value to the squared average of all values.
Since (75 + 193 + 37 + 121 + 80 + 189 + 45 + 94 + 232) / 9 = 118, we intermediate values 80 to 118. That is:

75 193 37
121 80 189
45 94 232

Changes to

75 193 37
121 118 189
45 94 232

This filtering principle end, we began to look at the results.

#include <opencv2/opencv.hpp>  
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
 Mat src = imread("bilibili.jpg");
 imshow("src", src);
 Mat dst;
 blur(src, dst, Size(7, 7));
 imshow("blur", dst);
 
 waitKey(0);
 return 0;
}

blur(src, dst, Size(7, 7));//1.输入图像,2.输出图像,
//3.进行滤波时选择小矩阵的大小(注意要要是n*n的矩阵,且n为奇数)。

Effect: Here Insert Picture Description
4 Edge detection
Edge detection is actually found on the edge of the image, a lot of fun, as long as edge detection is first grayscale images, if you want a sharper edge, you can add filtering step prior to detection.

If the argument to a function is not very understanding, can modify the values ​​of the parameters, see the effect of changes, you know where the influence of this parameter in the.

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
 Mat src = imread("bilibili.jpg");
 imshow("src", src);
 Mat dst;
 cvtColor(src, src, COLOR_BGR2GRAY);
 blur(src, src, Size(3, 3));
 Canny(src, dst, 3, 9, 3);
 imshow("canny", dst);
 waitKey(0);
 return 0;
}
//流程:读图,灰度化,滤波,边缘检测。
 Canny(src, dst, 3, 9, 3);//1.输入2.输出3.最小阈值4.最大阈值5.sobel算子的大小
 //最大最小阈值用来使在阈值范围内的像素值才能保留。sobel算子的用处是与原图进行卷积运算,得到一个新的图,这个图根据阈值范围留下数据后就是效果图。

Effect:
Here Insert Picture Description
5. Save pictures
with just one function imwrite () can be realized.

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
 Mat src = imread("bilibili.jpg");
 imshow("src", src);
 Mat dst;
 cvtColor(src, src, COLOR_BGR2GRAY);
 blur(src, src, Size(3, 3));
 Canny(src, dst, 3, 9, 3);
 imshow("canny", dst);
 imwrite("canny.jpg",dst);
 waitKey(0);
 return 0;
}
//和上一个代码相比只加了一个函数imwrite("canny.jpg",dst);
imwrite("canny.jpg",dst);//第一个参数是图片名,记得加后缀。这里也可以写绝对路径。第二个参数是保存哪张照片,这里选择边缘检测之后的图片。

We found main.cpp folder where more than a "canny.jpg". Here Insert Picture Description
This concludes the article, next stm32 write a tutorial on light water, as found on a talk with keil5 with article 32 of the amount of reading a lot.
Next recurrence probably every other day, because it takes time to write good, have to finish the job also Mo ......

Released five original articles · won praise 3 · Views 726

Guess you like

Origin blog.csdn.net/qq_43690756/article/details/105130236