Ubuntu创建opencv工程并运用cvtColor函数显示多张图片

本文档介绍了如何在Ubuntu系统中创建一个OpenCV工程,重点是利用cvtColor函数处理多张图片。首先,创建工程目录及cpp源码文件,接着编写用于读取和处理图像的代码。然后,创建CMakeLists.txt文件来配置编译规则。最后,通过终端编译工程并运行可执行文件,成功实现了图像的处理操作。

一、创建工程目录,并进入目录

mkdir example5   //新建文件夹 
cd  example5       //进入目录

写两个文件,一个是二、创建cpp文件,一个是编译文件
在这里插入图片描述

二、创建源码文件
cv.cpp是源码文件,用于读取图像视频,想要进行的操作都要在cpp中实现,程序如下

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
#include <math.h>
 
using namespace cv;
using namespace std;
 
 
int main()
{
        Mat dst;
        cout << "Hello OpenCV " << CV_VERSION << endl;
 
	// 载入图像
	Mat myMat = imread("myImage.JPG", 1);
       
        if(myMat.empty()){
           cout << "could not load image..." << endl;
           return -1;
        }
	
	// 创建一个窗口
	namedWindow("myMat image", WINDOW_AUTOSIZE);//窗口名称

    // 显示图像
	imshow("input", myMat);
        
         //定义函数类型
         Mat dst1,dst2,dst3;

        //使用cvtColor函数转换图形[cvtColor(原图像,转换后的图像,转换);]
        cvtColor(myMat,dst1,COLOR_BGR2HLS);
        cvtColor(myMat,dst2,COLOR_BGR2GRAY);
        cvtColor(myMat,dst3,COLOR_BGR2HSV);

        //给转换后的图像命名图像窗口
        namedWindow("dst1 image HLS", WINDOW_AUTOSIZE);
        namedWindow("dst2 image GRAY", WINDOW_AUTOSIZE);
        namedWindow("dst3 image HSV", WINDOW_AUTOSIZE);
        
        imshow("dst1 image HLS", dst1);
        imshow("dst2 image GRAY", dst2);
        imshow("dst3 image HSV", dst3);
	// 等待按键延时 ms
	waitKey(0);

    return 0;
}

三、创建编译文件
创建CMakeLists.txt文件,编辑编译代码,程序如下

# cmake needs this line 要求的最低版本
cmake_minimum_required(VERSION 2.8)

# Define project name 定义工程名
project(example_project)

# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI 自动查找库
find_package(OpenCV REQUIRED)

# Declare the executable target built from your sources 声明可执行目标文件及源文件
add_executable(example example.cpp)	# 目标文件,源文件0,源文件1,...

# Link your application with OpenCV libraries 将目标文件与库链接
target_link_libraries(example ${OpenCV_LIBS})	# 目标文件,库路径

四、打开终端
打开终端输入指令编译,生成Makefile

cmake .

在这里插入图片描述
编译工程

g++ cv.cpp -std=c++11 -o test `pkg-config --cflags --libs opencv4`
//源码文件名位cv.cpp,可执行文件为test

运行可执行文件

./test

效果
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值