opencv2笔记01-注意事项

1. Mat数据区和头信息(相同数据区、不同头信息)

Mat A, C;                                 // creates just the header parts
A = imread(argv[1], CV_LOAD_IMAGE_COLOR); // here we'll know the method used (allocate matrix)

Mat B(A);                                 // Use the copy constructor

C = A;                                    // Assignment operator

All the above objects, in the end, point to the same single data matrix. Their headers are different, however, and making a modification using any of them will affect all the other ones as well.


2. 创建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


3. 复制Mat数据区和头信息

Mat F = A.clone();
Mat G;
A.copyTo(G);

Sometimes you will want to copy the matrix itself too, so OpenCV provides the clone() and copyTo() functions.


4.构造函数定义Mat,并初始化

Mat M(2,2, CV_8UC3, Scalar(0,0,255));
cout << "M = " << endl << " " << M << endl << endl;

5.Create()函数定义Mat

M.create(4,4, CV_8UC(2));
cout << "M = "<< endl << " "  << M << endl << endl;

You cannot initialize the matrix values with this construction. It will only reallocate its matrix data memory if the new size will not fit into the old one.


6.获取Mat的一行或者一列

Mat::row(int y)

The method makes a new header for the specified matrix row and returns it. This is an O(1) operation, regardless of the matrix size. The underlying data of the new matrix is shared with the original matrix. Here is the example of one of the classical basic matrix processing operations, axpy, used by LU and many other algorithms:

inline void matrix_axpy(Mat& A, int i, int j, double alpha)
{
    A.row(i) += A.row(j)*alpha;
}

In the current implementation, the following code does not work as expected:

Mat A;
...
A.row(i) = A.row(j); // will not work
To achieve that, you should either turn this simple assignment into an expression or use the Mat::copyTo() method:

Mat A;
...
// works, but looks a bit obscure.
A.row(i) = A.row(j) + 0;

// this is a bit longer, but the recommended method.
A.row(j).copyTo(A.row(i));

7.opencv中感兴趣区域以及mask的使用 
http://blog.csdn.net/dengtaocs/article/details/38022153?utm_source=tuicool&utm_medium=referral

    Mat image,mask;  
    Rect r1(100,100,50,100);  
    Mat img1,img2,img3,img4;  
    for(;;)  
    {  
  
        sequence >> image;  
        mask = Mat::zeros(image.size(),CV_8UC1);  
        mask(r1).setTo(255);  
  
        img1 = image(r1);  
  
        image.copyTo(img2,mask);  
          
        image.copyTo(img3);  
        img3.setTo(0,mask);  
  
  
        imshow("Image sequence", image);  
        imshow("img1",img1);  
        imshow("img2",img2);  
        imshow("img3",img3);  
        imshow("mask",mask);  
        waitKey(0);  
    } 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值