Basic Learning notes: opencv (23): a preliminary understanding of opencv Coordinate System

Source: Internet
Author: User

 

  Preface

I believe that many of my friends will encounter a small problem when using opencv, and sometimes they do not pay enough attention to such a small problem, or simply program it on the surface, so some inexplicable problems occurred during code debugging, and the time for the problem was too long. Recently I encountered this problem when debugging a project, that is, the difference between mat: At (x, y) and MAT: At (point (x, y, in my project, I think these two types of codes have the same effect. As a result, the debugging of this problem is entangled for two days (because there are thousands of lines of code related to opencv in this project, there is no doubt about the difference between the two, so sometimes some inexplicable results may take a lot of time to find the problem ). In fact, I have made notes about the differences between the two persons during the mat at visit, but I accidentally forgot it at this time. This time I wrote a small note to remind myself.

Development Environment: opencv2.4.3 + qtcreator2.5.1

 

  Lab Basics

This experiment uses a short example to illustrate the following four problems:

1. In the coordinate system, the zero point coordinate is the upper left corner of the image, the X axis is the above horizontal line of the image rectangle, and the Y axis is the vertical line on the left side of the image rectangle. This coordinate system is applicable to structures such as mat, rect, and point. (Although I learned on the Internet that the coordinate origin of some data structures in opencv is in the lower left corner of the image, I haven't met it yet ).

2. when using image. when at <TP> (x1, x2) is used to access the value of the midpoint of an image, X1 is not the X axis coordinate of the corresponding vertex in the image, but the Y coordinate of the corresponding vertex in the image. Therefore, the access result is actually the point (X2, X1) in the image, which is the same as the image. at <TP> (point (X2, X1.

3. if the image is shown as multi-channel, for example, if the number of channels in the image is N, when mat: At (X, Y) is used, the range of X is still 0 to the height of the image, while the value range of Y is 0 to the width of the image multiplied by N, because there are n channels at this time, therefore, each pixel occupies n columns. However, if you use mat: At (point) for access under the same circumstances, you do not need to consider the number of channels at this time, because you need to assign a value to get mat :: the at (point) value is not a number, but a corresponding N-dimensional vector.

4. When using the minmaxloc () function, a multi-channel image cannot provide its maximum and minimum coordinates, because each pixel actually has multiple coordinates, so it does not. Therefore, during programming, the two locations should be null.

 

  Lab code and comments

Main. cpp:

# Include <iostream> # include <opencv2/CORE/core. HPP> # include <opencv2/highgui. HPP> using namespace STD; using namespace CV; int main () {mat image, image_3c; image. create (SIZE (256,256), cv_8uc1); image_3c.create (SIZE (256,256), cv_8uc3); // 3-channel image. setto (0); image_3c.setto (0); image. at <uchar> (10,200) = 255; // Where the at function is used, 10,200 point (20,100); image. at <uchar> (point) = 250; // Where the at function is used, point (10,200) image_3c.at <uchar> (10,300) = 255; image_3c.at <uchar> (10,302) = 254; point point_3c (20,200); image_3c.at <uchar> (point_3c) = 250; double maxval = 0; // The maximum value must be an initial value, otherwise, the system reports the following error during running: Point maxloc; minmaxloc (image, null, & maxval, null, & maxloc); cout <"maximum single channel image:" <maxval <Endl; double min_3c, max_3c; // note that the minmaxloc () function cannot provide the maximum and minimum coordinates of a multi-channel, because each pixel actually has multiple coordinates, therefore, minmaxloc (image_3c, & min_3c, & max_3c, null, null) is not provided. cout <"3-channel Maximum image:" <max_3c <Endl; imshow ("image", image); imshow ("image_3c", image_3c); waitkey (0); Return 0 ;}

 

  Experiment results:

The output result of a single-channel image is as follows:

  

It can be seen that there are two white points in the black image (the reader can take a closer look, because there is only one pixel, so you need to find it by yourself, therefore, we can prove that mat: At (X, Y) is different from mat: At (x, y.

 

The output result of the 3-channel image is as follows:

  

It can be seen that there are two points in the 3-channel image, but in the program, the values of Y in mat: At (x, y) are 300 and 302, this exceeds the image width of 256. This also proves the 3rd points in the basic experiment.

 

The output result is as follows:

  

 

  Experiment summary:It can be seen that you must pay attention to some details at ordinary times.

 

 

 

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.