opencv 网站tutorials学习翻译

本文档详述了学习OpenCV的过程,包括安装、图像显示、矩阵操作、图像处理等功能。作者通过翻译官方教程,介绍了如何在Linux上安装OpenCV,使用CMake构建,并展示了在Ubuntu上安装和显示图像的步骤。此外,还详细讨论了遍历图像像素、矩阵掩码操作、图像混合、对比度调整、基本绘图和离散傅里叶变换等核心概念和技术。
摘要由CSDN通过智能技术生成

因为要涉及到图像处理部分,所以需要整体第学习opencv的代码,以供后面项目上使用。

需要快速完成这个库的练习和使用。

大概需要两个星期的时间掌握c++以及java库的编写工作。

首先是安装opencv

然后根据官网的教程进行make make install的安装。


第一章 Introduction to OpenCV

这章介绍了opencv在各个版本上的安装过程,有些安装过程已经很老了,比如android部分,所以需要对cmake有一定的掌握。

现在写app都是用android studio,不过已经包含了opencv-sdk,这部分的安装可以查看之前的文章。

这章主要的小节有

1. Installation in Linux

2. Using OpenCV with gcc and CMake

3. Load and Display an Image

4. Load, Modify, and Save an Image


第一节 在ubuntu上安装opencv

需要安装一下库工具之类的程序

[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libdc1394-22-dev
安装完成之后下载

git clone https://github.com/opencv/opencv.git

下载完成之后开始编译

cmake的时候可以增加BUILD_DOCS,编译生成文档

cd ~/opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j7
sudo make install

第二节 显示图像

这里是用网页来显示图像所以ishow没什么作用,具体还是需要读取图像,然后保存,传递给php,进行网页前端显示。

这里需要指定保存的路径,然后php根据保存的路径进行显示,前面的文章已经介绍过,这里不再介绍。

所以还是需要通过php端读取数据,交给opencv处理保存,然后还是通过php读取数据返回给nginx服务器,显示到客户端。

DisplayImage.cpp文件只是读取文件到Mat结构体中

#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
    if ( argc != 2 )
    {
        printf("usage: DisplayImage.out <Image_Path>\n");
        return -1;
    }
    Mat image;
    image = imread( argv[1], 1 );
    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}
然后编写cmake文件,记得文件名大小写不要错。

CMakeLists.txt file

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
cmake make

然后运行

最好在窗口中显示结果。


第三节 加载和显示图片

和第二小节是重复的


第四节 加载修改和保存图片

这小节使用cvrColor将图片从BGR颜色变成Grayscale颜色模式,然后进行保存。

#include <opencv2/opencv.hpp>
using namespace cv;
int main( int argc, char** argv )
{
 char* imageName = argv[1];
 Mat image;
 image = imread( imageName, IMREAD_COLOR );
 if( argc != 2 || !image.data )
 {
   printf( " No image data \n " );
   return -1;
 }
 Mat gray_image;
 cvtColor( image, gray_image, COLOR_BGR2GRAY );
 imwrite( "../../images/Gray_Image.jpg", gray_image );
 namedWindow( imageName, WINDOW_AUTOSIZE );
 namedWindow( "Gray image", WINDOW_AUTOSIZE );
 imshow( imageName, image );
 imshow( "Gray image", gray_image );
 waitKey(0);
 return 0;
}
没什么好讲的,就是一个cvtColor函数。


第二章 OpenCV核心模块功能介绍

1. Mat - The Basic Image Container

2. How to scan images, lookup tables and time measurement with OpenCV

3. Mask operations on matrices

4. Operations with images

5. Adding (blending) two images using OpenCV

6. Changing the contrast and brightness of an image!

7. Basic Drawing

8. Random generator and text with OpenCV

9. Discrete Fourier Transform


第一节 图片矩阵

The first thing you need to know about Mat is that you no longer need to manually allocate its memory and release it as soon as you do not need it.

Mat is basically a class with two data parts: the matrix header and a pointer to the matrix containing the pixel values.

OpenCV使用引用,Moreover, the copy operators will only copy the headers and the pointer to the large matrix, not the data itself.

Mat A, C;                          // creates just the header parts
A = imread(argv[1], IMREAD_COLOR); // here we'll know the method used (allocate matrix)
Mat B(A);                                 // Use the copy constructor
C = A;                                    // Assignment operator
这几个都只是复制了矩阵的头,没有作实际的数据copy.

roi感兴趣的区域

Mat D (A, Rect(10, 10, 100, 100) ); // using a rectangle
Mat E = A(Range::all(), Range(1,3)); // using row and column boundaries
一个大矩阵中圈出一个小矩阵

Mat 有自动回收机制,所以在赋值的时候count自动加1,当count值为0的时候就会释放该内存。

clone和copyTo两个函数进行的实际的数据copy

Mat F = A.clone();
Mat G;
A.copyTo(G);
现在如果改变F或者G就不会影响到A的数据。

Mat结构的创建模式

几个参数,

Mat M(2,2, CV_8UC3, Scalar(0,0,255));
CV_[The number of bits per item][Signed or Unsigned][Type Prefix]C[The channel number]

这几个参数的解释,对应CV_8UC3

    int sz[3] = {2,2,2};
    Mat L(3,sz, CV_8UC(1), Scalar::all(0));
创建多维数组。


第二节 遍历图形像素和需要时间对比

这章列出了四种方法进行一张图片的遍历,举的例子就是缩小原来的图片的大小,减少计算量。

原理是这样的,设置一个除数值,所有的像素除以这个数,再乘以这个数,会得到这个数的整数倍,除数越大,计算量越小。

这里没有让每个数进行乘除运算,而是进行查表操作。

首先是获取这张表格,这里用的是8位的unsigned char类型,这样数据也已经很大。

how_to_scan_images imageName.jpg intValueToReduce [G]

G表示传送灰度图。

这里是获取表格的算法

    int divideWith = 0; // convert our input string to number - C++ style
    stringstream s;
    s << argv[2];
    s >> divideWith;
    if (!s || !divideWith)
    {
        cout << "Invalid number entered for dividing. " << endl;
        return -1;
    }
    uchar table[256];
    for (int i = 0; i
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值