您的位置:首页 > 运维架构

Opencv 笔记 opencv tutorial 2.1节 mat

2015-06-17 22:04 393 查看
前言:opencv中,mat类型非常基础和重要。以下是opencv tutorial 2.1章节的中英文整理。
Mat
简介

IplImage c
结构 需要管理内存
mat是c++中的类class
自动内存分配
Mat
包括:
header
A pointer to the matrix containing the pixel values
(can take
dimentionlity
根据存储方法)
头大小不变
矩阵大小变
复制数据的速度由矩阵大小决定
将图像传给函数是常见做法
图像处理很耗计算量
避免不必要的大图像复制
引用计数 reference counting

每个mat有自己的头 但是他们可以共同指向一个矩阵地址
ROI区域

Mat D = A(Rect(10,10,100,100) )//use a Rect

Mat E = A(Range:all(),range(1,3);
一个矩阵区域由多个mat掌控,但是不需要处理clean之类
有一个counter计数header的数目,whenever
a header is cleaned, the counter is decreased. When the counter is zero, the matrix is freed.
复制矩阵数据的方式:修改F G 不会影响A

Mat F = A.Clone()

Mat G; A.CopyTo(G);
A.copyTo(G,mask
) mask不为0的地方 复制

Opencv 输出图像的allocation是自动的

Opencv c++ interface 都不用考虑内存释放 memory freeing
mat创建方式

Mat A = Imread(Filename, CVLOADIMAGECOLOR)

Mat B(A) //copy constructor

Mat C = A // assignment operator 只复制头

A B C point to the same single matrix
保存方法:
颜色空间 color space
+ 数据类型 data type
Grayscale
RGB
适合人眼系统
HSV
对光照不敏感
YCrCb jpeg
图像格式
CIE Lab
用来计算颜色距离

code the transparency of a color alpha(A)is needed
Data type: denth

深度

最小的是
char
分为 unsigned
(0-255)signed(-127-127)
1byte Int 是2个字符呦

Float 32bits 4
bytes

Double 64bits 8 bytes

Int
类型的宏

#define CV_8U 0

#define CV_8S 1

#define CV_16U 2

#define CV_16S 3

#define CV_32S 4

#define CV_32F 5

#define CV_64F 6

#define CV_8UC1 CV_MAKETYPE(CV_8U,1)

#define CV_8UC2 CV_MAKETYPE(CV_8U,2)

#define CV_8UC3 CV_MAKETYPE(CV_8U,3)

#define CV_8UC4 CV_MAKETYPE(CV_8U,4)

#define CV_8UC(n) CV_MAKETYPE(CV_8U,(n))

#define CV_8SC1 CV_MAKETYPE(CV_8S,1)

#define CV_8SC2 CV_MAKETYPE(CV_8S,2)

#define CV_8SC3 CV_MAKETYPE(CV_8S,3)

#define CV_8SC4 CV_MAKETYPE(CV_8S,4)

#define CV_8SC(n) CV_MAKETYPE(CV_8S,(n))

#define CV_16UC1 CV_MAKETYPE(CV_16U,1)

#define CV_16UC2 CV_MAKETYPE(CV_16U,2)

#define CV_16UC3 CV_MAKETYPE(CV_16U,3)

#define CV_16UC4 CV_MAKETYPE(CV_16U,4)

#define CV_16UC(n) CV_MAKETYPE(CV_16U,(n))

#define CV_16SC1 CV_MAKETYPE(CV_16S,1)

#define CV_16SC2 CV_MAKETYPE(CV_16S,2)

#define CV_16SC3 CV_MAKETYPE(CV_16S,3)

#define CV_16SC4 CV_MAKETYPE(CV_16S,4)

#define CV_16SC(n) CV_MAKETYPE(CV_16S,(n))

#define CV_32SC1 CV_MAKETYPE(CV_32S,1)

#define CV_32SC2 CV_MAKETYPE(CV_32S,2)

#define CV_32SC3 CV_MAKETYPE(CV_32S,3)

#define CV_32SC4 CV_MAKETYPE(CV_32S,4)

#define CV_32SC(n) CV_MAKETYPE(CV_32S,(n))

#define CV_32FC1 CV_MAKETYPE(CV_32F,1)

#define CV_32FC2 CV_MAKETYPE(CV_32F,2)

#define CV_32FC3 CV_MAKETYPE(CV_32F,3)

#define CV_32FC4 CV_MAKETYPE(CV_32F,4)

#define CV_32FC(n) CV_MAKETYPE(CV_32F,(n))

#define CV_64FC1 CV_MAKETYPE(CV_64F,1)

#define CV_64FC2 CV_MAKETYPE(CV_64F,2)

#define CV_64FC3 CV_MAKETYPE(CV_64F,3)

#define CV_64FC4 CV_MAKETYPE(CV_64F,4)

#define CV_64FC(n) CV_MAKETYPE(CV_64F,(n))

显式创建mat类型:

加载图像Load image


E.g. Mat M = imread(file name,1)

Mat构造函数创建

m() constructor 返回另一个mat的实例

E.g Mat A(2,2,CV_8UC3, Scalar(0,0,255))

查看mat数值可以直接用<<显示
只能用于二维矩阵 E.g cout<<A

IplImage -->
mat

IplImage *img = cvLoadImage(filename,1)

Mat mtx(img) //只创造头 共享矩阵数据

create函数

Mat M;

M.create(4,4,CV_8UC2) //不能用来初始化矩阵数值 it just reallocate. It's matrix Data memory if the new size doesn't fit into the old one;

Create 创建的矩阵是连续的

Matlab类型初始化
zeros() ones() eyes()

E.g Mat m Mat ::zeros(2,2,CV_8UC3)

使用comma separated initializer :

E.g. Mat C = (Mat_<double>(3,3) <<0,0,1,…9)

clone() or CopyTo ()

Create a new header for an existing Mat object and
it

E.g Mat RowRange = C.row(1).clone()

使用
randu来填充matrix
value


Mat R = M(2,2,CV_8UC3)
赋值操作符

Randu(R,Scalar::all(0), Scalar::all(0))

使用C++ arrays and initialize via
constructor
//多维矩阵

Int sz[3] = {2,2,2}
Mat L(3, sz,CV_8UC(1),Scalar::all(0))// 2+维mat
其中数据类型写法请注意
u_C1[j] = Mat( img.size(),CV_8UC1, Scalar(0) );
是有效的
u_C1[j] = Mat( img.size(),CV_8UC1, 0 );是无效的

Convert image color space

E.g Mat Converted

CvtColor(M,Converted ,CV_RGB2GRAY)

打印mat
时可以用 format(R,"python") csv numpy C

下面都是示例代码:
Load & display image use Opencv highgui module

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream> //for standard I/O

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}

Mat image;
image =
imread(argv[1], IMREAD_COLOR); // Read the file

if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}

namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.

imshow( "Display window", image ); // Show our image inside it.

waitKey(0); // Wait for a keystroke in the window
里面的参数表示用户要等多久0表示无限等下去
直到用户按下
return 0;
}

imread函数 第二个参数。

• CV_LOAD_IMAGE_UNCHANGED (<0) loads the image as is (including the alpha channel if present)
• CV_LOAD_IMAGE_GRAYSCALE ( 0) loads the image as an intensity one
• CV_LOAD_IMAGE_COLOR (>0) loads the image in the RGB format
OpenCV offers support for the image formats Windows bitmap (bmp),
portable image formats (pbm, pgm, ppm) and Sun raster
(sr, ras). With help of plugins (you need to specify to use them if you build yourself the library, nevertheless in the packages we ship present by default) you may also load image formats
like JPEG (jpeg, jpg, jpe), JPEG 2000 (jp2 - codenamed in the CMake as Jasper), TIFF files
(tiff, tif) and portable network graphics (png). Furthermore, OpenEXR is also a possibility.

namedWindow
(file name ,第二个参数)。
CV_WINDOW_AUTOSIZE
如果你不使用Qt只能使用这项
CV_WINDOW_NORMAL (CV_WINDOW_KEEPRATIO) (CV_WINDOW_FREERATIO).
如果你使用Qt那么
还可以设定是否改变原图比例
Load modify and save an image
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main( int argc, char** argv )
{

char* imageName = argv[1];

Mat image;

image = imread( imageName, 1 );

if( argc != 2 || !image.data )

{
printf( " No image data \n " );
return -1;

}

Mat gray_image;

cvtColor( image, gray_image, CV_RGB2GRAY );

imwrite( "../../images/Gray_Image.jpg", gray_image );

namedWindow( imageName, CV_WINDOW_AUTOSIZE );

namedWindow( "Gray image", CV_WINDOW_AUTOSIZE );

imshow( imageName, image );

imshow( "Gray image", gray_image );

waitKey(0);

return 0;
}
Imread Imwrite Imshow namedWindow
中的目录参数都是
char*

曾经有一个疑问,CV_8U, Scalar(255)这样的是几通道?现在相同了,就是单通道。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: