opencv-basic data structures and examples

Source: Internet
Author: User


Basic data structures and examples for OPENCV

The strong mat types in the OpenCV are already familiar to us. Here are some of the basic data types that are commonly used in engineering. Including:

Vec

Scalar

Point

Size

Rect

Rotatedrect


1. Vec class
1.1 Basic Concepts

Vec is a template class that is used primarily to store numeric vectors.


1.2 usage
(1) Accessing VEC vector members using []
myvector[0]=0;

(2) Use it to define any type of vector

Vec<double, 8> Myvector; Defines a vector that holds 8 variables of type double

(3) The following predefined types can be used

typedef vec<uchar, 2> vec2b;typedef Vec<uchar, 3> vec3b;typedef Vec<uchar, 4> Vec4b;typedef Vec< Short, 2> vec2s;typedef vec<short, 3> vec3s;typedef vec<short, 4> vec4s;typedef Vec<int, 2> Vec2i;ty Pedef vec<int, 3> vec3i;typedef vec<int, 4> vec4i;typedef vec<float, 2> Vec2f;typedef Vec<float, 3&G T Vec3f;typedef vec<float, 4> vec4f;typedef vec<float, 6> vec6f;typedef vec<double, 2> Vec2d;typedef Vec <double, 3> vec3d;typedef vec<double, 4> vec4d;typedef vec<double, 6> Vec6d;

(4) The operations supported by VEC are as follows:

V1 = v2 + v3v1 = V2-V3V1 = V2 * scalev1 = scale * V2v1 =-v2v1 + = V2V1 = v2, V1! = V2norm (v1) (Euclidean norm)

1.3Sample Code
(1) vector definition and element access

Vec     cv::vec<double, 8>  myvector;     for (int i=0; i<myvector.rows;i++)          myvector[i] = i;     cout<< "myvector=" <<myVector<<endl;     cout<< "myvector[0]=" <<myVector[0]<<endl;     cout<< "myvector[3]=" <<myVector[3]<<endl;
Operation Result:

(2) Basic operation

Cv::vec<int, 6> v1,v2,v3;        for (int i=0; i<v2.rows;i++) {//v2.rows Returns the number of rows v2 the vector                   v2[i] = i;                   V3[i] = i+1;        }                 V1 = v2 + v3;         cout<< "V2       =" <<v2<<endl;         cout<< "V3       =" <<v3<<endl;         cout<< "v1=v2+v3=" <<v1<<endl;         cout<< "v1=v2*2  =" <<v2*2<<endl;         cout<< "V1=-v2   =" <<-v2<<endl;         cout<< "V1==v2   =" << (v1==v2) <<endl;         cout<< "V1!=v2   =" << (v1!=v2) <<endl;         cout<< "Norm (v2) =" <<norm (v2) <<endl;

Operation Result:


2. Scalarclass
2.1 Basic Concepts

Scalar is a template class derived from the Vec class, a vector that can hold 4 elements, and is widely used to transfer and read pixel values in an image.


2.2 usage

You can use [] to access the scalar value. Or use the following method to define the value of the BGR three channels.

CV:: Scalar (B, G, R)

2.3 Sample code
(1) cv::scalar structure
Cv::scalar myscalar;myscalar = cv::scalar (0,255,0);cout<< "myscalar =" <<myscalar<<endl;system (" Pause ");

Operation Result:


(2) reading color image pixel values

Each pixel of a color image corresponds to three parts: RGB three channels. So the Cv::mat class that contains the color image returns a vector that contains three 8-bit values. OPENCV defines a type for such a short vector, which is our cv::vec3b. This vector contains three unsigned characters (unsigned character) types of data.

OPENCV Storage channel order is: Blue, green, red is BGR.
Therefore, the methods for accessing elements in color pixels are as follows:

Cv::mat pImg = Cv::imread ("lena.jpg", 1), if (!pimg.data) return 0;int x = n, y = 100;cv::scalar pixel=pimg.at<vec3b> (x, y);cout<< "B chanel of Pixel is =" <<pixel.val[0]<<endl;cout<< "G chanel of Pixel is =" <<p ixel.val[1]<<endl;cout<< "R chanel of Pixel is =" <<pixel.val[2]<<endl;system ("pause");

Operation Result:

3. Pointclass 3.1 basic Concepts

Commonly used to represent 2-D coordinates (x, y).

3.2 usage

(1) Image coordinates

For images, we can define this:

CV::P oint pt;pt.x = 10;pt.y = 8;
Or

CV::P oint pt = Point  (10, 8);

(2) or use the following predefined:
typedef point_<int> POINT2I;TYPEDEF point2i point;typedef point_<float> point2f;typedef Point_<double > point2d;
(3) Basic operation
PT1 = pt2 + pt3;pt1 = PT2-PT3;PT1 = pt2 * a;pt1 = a * pt2;pt1 + = pt2;pt1-= pt2;pt1 *= a;double value = Norm (PT); L2 NORMPT1 = = pt2;pt1! = pt2;

3.3 sample code
(1) Set coordinate points
POINTCV::P oint pt;pt.x = 278;pt.y = 269;//or//CV::P oint  pt (278,269); Cv::scalar pix = Pimg.at<vec3b> (PT); cout<< "Pix (" <<pt.x<< "," <<pt.y<< ") =" <<pix<<endl;

Operation Result:



(2) Various operations
CV::P oint pt1 (10,20); CV::P oint pt2 (2,3);cout<< "pt1     =" <<pt1<<endl;cout<< "pt2     =" <<pt2<<endl;cout<< "pt1+pt2 =" <<pt1+pt2<<endl;cout<< "pt1+=pt2=" <<pt1 <<endl;cout<< "pt1-pt2 =" <<pt1-pt2<<endl;cout<< "pt2*2   =" <<pt2*2<< Endl

Operation Result:


4. Size class
4.1 Basic Concepts

The template class size can represent an image or a rectangle. It contains 2 Members: width, height, and a useful area function areas ().

4.2 usage
Cv::size Size (int w, int h);//Cv::size size;size.width = W;size.height = h;

4.3 Sample code
Sizecv::size size1 (6,3); Cv::size size2;size2.width = 4;size2.height = 2;cv::mat mat1 (size1,cv_8uc1,cv::scalar (0)); Cv::mat mat2 (size2,cv_8uc3,cv::scalar);cout<< "MAT1 =" <<endl<<mat1<<endl;cout< <endl<< "MAT2 =" <<endl<<mat2<<endl;system ("pause");

Operation Result:



5. Rect class
5.1 Basic Concepts

Rect is another template class for defining a 2-D rectangle. It is defined by two parameters:

    • Upper-left corner coordinate of the rectangle: (x, Y)
    • Width and height of rectangle: width, height

Rect can be used to define the ROI area of an image.


5.2usage
Cv::rect Rect (x, y, width, height);

5.3 sample code
Rectcv::mat pImg = Imread ("lena.jpg", 1); Cv::rect  Rect (180,200,200,200);//(x, y) = (180,200), W=200,HEIGHT=200CV :: Mat  roi = Cv::mat (pImg, rect); Cv::mat  pimgrect = Pimg.clone (); Cv::rectangle (Pimgrect,rect,cv::scalar ( 0,255,0), 2); Cv::imshow ("Original image with Rectangle", Pimgrect), Cv::imshow ("Roi", ROI); Cv::waitkey ();
Operation Result:


6. Rotatedrect class
6.1 Basic Concepts

The last basic data class is a special kind of rectangle called Rotatedrect. This class represents a rotated rectangle by the center point, width and height, and rotation angle.


6.2usage

To rotate the constructor of a rectangle class:

Rotatedrect (const point2f& Center, const size2f& size, float angle);

Parameters:

    • Center : Center Point coordinates point2f type;
    • size : Width and height of the rectangle, size2f type;
    • Angle : Rotation angle in clockwise direction, float type.

6.3 Sample code
ROTATEDRECTCV::P oint2f Center (100,100); cv::size2f size (100,50); float angle = 45; Rotatedrect Rrect (center, size,  angle); Cv::mat image (200,200,cv_8uc3,cv::scalar (0)); POINT2F vertices[4];rrect.points (vertices); (int i = 0; i < 4; i++) line (image, Vertices[i], vertices[(i+1)%4], Scala R (0,255,0)); Rect brect = Rrect.boundingrect (); Rectangle (image, Brect, Scalar (255,0,0)), Imshow ("rectangles", image); Waitkey (0);
Operation Result:

opencv-basic data structures and examples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.