BS版图形系统 - OpenCV - 第2章笔记


QQ 282397369

2 OpenCV基础知识导论

2.1 技术要求

2.2 基本CMake配置文件

2.3 创建一个库

2.4 管理依赖项

2.5 让脚本更复杂

2.6 图像和矩阵

  • 图像、像素
  • 值范围0-255,或其他范围:高动态范围成像、热图像领域[0 ~ 1]
  • 矩阵,宽度 x 高度 x 颜色通道数

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

2.7 读/写图像

  • imread: CV_LOAD_IMAGE_GRAYSCALE、IMREAD_UNCHANGED、IMREAD_COLOR、IMREAD_GRAYSCALE
  • imwrite
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

// OpenCV includes
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;

int main( int argc, const char** argv )
{
	// Read images
	Mat color= imread("../lena.jpg");
	Mat gray= imread("../lena.jpg", IMREAD_GRAYSCALE);
	
	if(! color.data ) // Check for invalid input
	{
		cout << "Could not open or find the image" << std::endl ;
	 	return -1;
	}

	// Write images
	imwrite("lenaGray.jpg", gray);
	
	// Get same pixel with opencv function
	int myRow=color.rows-1;
	int myCol=color.cols-1;
	auto pixel= color.at<Vec3b>(myRow, myCol);
	cout << "Pixel value (B,G,R): (" << (int)pixel[0] << "," << (int)pixel[1] << "," << (int)pixel[2] << ")" << endl;
	
	// show images
	imshow("Lena BGR", color);
	imshow("Lena Gray", gray);
	// wait for any key press
	waitKey(0);
	return 0;
}

在这里插入图片描述

  • at

2.8 读取视频和摄像头

  • CommandLineParser
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

// OpenCV includes
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;

// OpenCV command line parser functions
// Keys accecpted by command line parser
const char* keys =
{
	"{help h usage ? | | print this message}"
    "{@video | | Video file, if not defined try to use webcamera}"
};

int main( int argc, const char** argv )
{
	CommandLineParser parser(argc, argv, keys);
    parser.about("Chapter 2. v1.0.0");
    //If requires help show
    if (parser.has("help"))
	{
	    parser.printMessage();
	    return 0;
	}

	String videoFile= parser.get<String>(0);
	
	// Check if params are correctly parsed in his variables
	if (!parser.check())
	{
	    parser.printErrors();
	    return 0;
	}

	VideoCapture cap; // open the default camera
	if(videoFile != "")
		cap.open(videoFile);
	else
		cap.open(0);
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    namedWindow("Video",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        if(frame.empty())
        	return 0;
        imshow("Video", frame);
        if(waitKey(30) >= 0) break;
    }
    // Release the camera or video cap
    cap.release();
	
	return 0;
}

2.9 其他基本对象类型

  • Vec、Scalar、Point、Size、Rect、RotatedRect

Vec

  • Vec2b、Vec3b、Vec4b
  • Vec2s、Vec3s、Vec4s
  • Vec2i、Vec3i、Vec4i
  • Vec2f、Vec3f、Vec4f、Vec6f
  • Vec2d、Vec3d、Vec4d、Vec6d
  • 运算:+ - 数乘 == != norm

Scalar

Point

  • Point2i, Point, Point2f, Point2d
  • 运算:+ - 数乘 除数 += -= *=数 /=数 == !=

Size

  • width, height, area()

Rect

  • 左上角、宽高
  • ROI: Region of Interest

RotatedRect

  • 中心点、宽高、旋转角度
  • boundingBox()
    在这里插入图片描述

2.10 基本矩阵运算

  • 创建Mat,构造函数
  • CV_number_typeC( n)
  • Mat::zeros[全0], Mat::ones[全1], eye[单位矩阵]
  • 运算: + - 数乘 mul
  • 转置t()
  • 求逆inv()
  • 计算非零元素: countNonZero
  • 通道的平均值和标准差: meanStdDev
  • 查找最大最小值:minMaxLoc

2.11 基本数据存储

  • XML/YAML持久层
  • 写入FileStorage
#include "opencv2/opencv.hpp"
using namespace cv;

int main(int, char** argv)
{
	// create our writter
    FileStorage fs("test.yml", FileStorage::WRITE);
    // Save an int
    int fps= 5;
    fs << "fps" << fps;
    // Create some mat sample
    Mat m1= Mat::eye(2,3, CV_32F);
	Mat m2= Mat::ones(3,2, CV_32F);
	Mat result= (m1+1).mul(m1+3);
	// write the result
    fs << "Result" << result;
    // release the file
    fs.release();

    FileStorage fs2("test.yml", FileStorage::READ);

    Mat r;
    fs2["Result"] >> r;
    std::cout << r << std::endl;

    fs2.release();

    return 0;
}

2.12 总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值