2. Opencv project creation and cmake compilation


The previous article ( 1 Ultra-detailed installation of opencv on ubuntu ) described the steps to install opencv on ubuntu18.4, and then you can develop projects based on the installed opencv library. The code for this article has been uploaded to my github repository, and you can view different versions of the code by switching tags.

1 Create a project directory

Create a folder called opencv under /home, and create three directories images, out, and src in the folder to store images that need to be processed and saved, executable files compiled and output, and project source code.
insert image description here

Enter the src directory, create two directories include, source, and CMakeLists.txt file. Among them, source is used to store the source file of the program, and include is used to store the header file.
insert image description here

The directory structure of the entire project is as follows:
insert image description here

2 Write and compile code

Use VScode to open the opencv folder,
create the main.cpp file ColChange.cpp file in the source directory, and create the ColChange.h file in the include directory. The source code of the file is as follows:

main.cpp

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "ColChange.h"

//using namespace cv;
using namespace std;


int main(int argc, char** argv )
{
  //读取图片,将图片存为Mat类的image实例中
  Mat image = imread("../images/123.jpg");

  if(image.empty()){
    printf("could not load image...\n");
    return -1;
  }
  //实例化一个QuickDemo
  QuickDemo qd;

  //qd.colorSpace_Demo(image);
  //qd.mat_creat_demo(image);
  qd.pixel_visit_demo(image);

  //创建窗口SSJ
  //cv::namedWindow("SSJ",WINDOW_FREERATIO);

  //在SSJ窗口上,显示图片
  //cv::imshow("SSJ", image);
  //显示状态阻塞
  cv::waitKey(0);

  return 0;
}

ColChange.cpp

#include "ColChange.h"

/*
 * Author:SSJ-xiaoguoke
 * Funtion:转换图像的色彩空间,transform the color space of the image
 */
void QuickDemo::colorSpace_Demo(Mat &image)
{
  Mat gray,hsv;
 
  cvtColor(image,hsv,COLOR_BGR2HSV);
  cvtColor(image, gray,COLOR_BGR2GRAY);

  imshow("HSV",hsv);
  imshow("huidu",gray);

  imwrite("../images/hsv.jpg",hsv);
  imwrite("../images/gray.jpg",gray);

}

/*
 * Author:SSJ-xiaoguoke
 * Funtion:创建Mat对象,Creating an image object
 */
void QuickDemo::mat_creat_demo(Mat &image)
{
    Mat m1,m2;

    m1 = image.clone();
    image.copyTo(m2);

/*
 * Size(8,8):创建的矩阵尺寸为 8*8
 * CV_8UC1:8位 U:无符号unsigned C:char型 1:单通道
 */
    //Mat m3 = Mat::zeros(Size(8,8),CV_8UC1);
    /*三通道*/
    Mat m3 = Mat::zeros(Size(500,500),CV_8UC3);
    /*创建一个值全是1的矩阵*/
    //Mat m3 = Mat::ones(Size(8,8),CV_8UC1);

    m3 = Scalar(0,0,255);
    //std::cout << m3 << std::endl;

    imshow("red",m3);

}

/*
 * Author:SSJ-xiaoguoke
 * Funtion:像素操作,Pixel operations
 */
void QuickDemo::pixel_visit_demo(Mat &image)
{
    int W = image.cols;
    int h = image.rows;
    int dims = image.channels();

    /*for(int row=0; row < h; row++){
        for(int col=0; col<W; col++){
            if(dims==1){

                int pv = image.at<uchar>(row,col);
                image.at<uchar>(row,col) = 255 - pv;

            }
            if(dims==3){

                Vec3b bgr = image.at<Vec3b>(row,col);
                image.at<Vec3b>(row,col)[0] = 255 - bgr[0];
                image.at<Vec3b>(row,col)[1] = 255 - bgr[1];
                image.at<Vec3b>(row,col)[2] = 255 - bgr[2];

            }
        }
    }*/
    /*通过指针的方法实现*/
    for(int row=0;row < h; row++){

        uchar* current_row = image.ptr<uchar>(row);

        for(int col=0;col < W;col++){
            if(dims==1){
                int pv = *current_row;
                *current_row++ = 255-pv;

            }
            if(dims==3){
                *current_row++ = 255 - *current_row;
                *current_row++ = 255 - *current_row;
                *current_row++ = 255 - *current_row;
            }
        }
    }

    imshow("Pixel operations",image);

}

ColChange.h

#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

class QuickDemo {
    public:
        void colorSpace_Demo(Mat &image);

        void mat_creat_demo(Mat &image);

        void pixel_visit_demo(Mat &image);

};

CMakeLists.txt

#可执行文件名
project(test)
#设置可执行程序的输出路径
set(EXECUTABLE_OUTPUT_PATH ../out/)
#设置源码路径
set(SOURCES
    ./source/main.cpp
    ./source/ColChange.cpp     
)
#添加头文件搜索路径
include_directories(./include)
#查找包
find_package(OpenCV 3 REQUIRED )
#添加编译的可执行文件
add_executable(test ${SOURCES})
#链接可执行文件
target_link_libraries(test ${OpenCV_LIBS})

After the code is written, enter the src directory and execute the command to compile the project

cmake ./
make

After the compilation is complete, an executable file called test will be output in the out directory. Put a picture under the image and rename it to 123.jpg (you can modify the code to change the name of the loaded picture)
and enter the out directory

./test

insert image description here
The result shown here is the result of me inverting the pixel values ​​of the image.

3 Opencv knowledge used

3.1 Reading, displaying and saving images

cv::imread("../images/123.jpg");

cv::imshow("窗口名字",mat);

/*par1:保存的路径
*par2: 要保存的Mat对象
*/
cv::imwrite("../images/tom.jpg",tom);

3.2 Color space conversion, image creation

/*将image的色彩空间装换成HSV,并将转换后的图像存在tom中*/
cvtColor(image, tom,COLOR_BGR2HSV);

Image cloning, duplication and assignment operations. Cloning and copying create new data, the assignment and the assignee share the same piece of data, and modifying one of them will also change the other.

Mat m1,m2,m3;
m1 = image.clone();
image.copyTo(m2);
m3 = image;

Create Mat objects and assign values

/*
 * Size(8,8):创建的矩阵尺寸为 8*8
 * CV_8UC1:8位 U:无符号unsigned C:char型 1:单通道
 */
Mat m3 = Mat::zeros(Size(500,500),CV_8UC3);
m3 = Scalar(0,0,255); //bgr, red通道拉满 其他通道为0 所以是一个纯红图片

3.2 Pixel manipulation

/*坐标法*/
int pv = image.at<uchar>(row,col);
image.at<uchar>(row,col) = 255 - pv;

/*指针法*/
uchar* current_row = image.ptr<uchar>(row);
for(int col=0;col < W;col++){
 if(dims==1){
        int pv = *current_row;
       *current_row++ = 255-pv;

 }
if(dims==3){
        *current_row++ = 255 - *current_row;
        *current_row++ = 255 - *current_row;
        *current_row++ = 255 - *current_row;
}

4 Use of C++ template function template class

4.1 Individual template functions

/*模板函数声明*/
template<typename T> void ColorSpace_Demo(Mat &image,T cs);
/*模板函数定义*/
template<typename T> 
void ColorSpace_Demo(Mat &image, T cs)
{
  Mat tom;
  
  cvtColor(image, tom,cs);
  
  imshow("Tom",tom);

  imwrite("../images/tom.jpg",tom);
}
/模板函数的使用/
ColorSpace_Demo<int>(image,40);

4.2 Template functions in classes

/*类中模板函数的声明*/
class QuickDemo {
    public:
        template<typename T> void ColorSpace_Demo(Mat &image,T cs);
           
        void mat_creat_demo(Mat &image);
};
/*类中模板函数的定义*/
template<typename T> void QuickDemo::ColorSpace_Demo(Mat &image, T cs)
{
	Mat tom;
	cvtColor(image, tom,cs);
	imshow("Tom",tom);
	
	imwrite("../images/tom.jpg",tom);
}
**/*类中模板函数的使用*/**
QuickDemo qd;
qd.ColorSpace_Demo<int>(image,40);

4.3 Functions in template classes

/*模板类函数的声明*/
template<typename T>
class QuickDemo {
    public:
        void ColorSpace_Demo(Mat &image,T cs);
};
/*模板类函数的定义*/
template<typename T> 
void QuickDemo<T>::ColorSpace_Demo(Mat &image, T cs)
{
  Mat tom;
  cvtColor(image, tom,cs);
  
  imshow("Tom",tom);

  imwrite("../images/tom.jpg",tom);
}

/*模板类函数的使用*/
QuickDemo<int> qd;
qd.ColorSpace_Demo(image,40);

Guess you like

Origin blog.csdn.net/weixin_44698673/article/details/127104337