OpenCV Rect x and y represent (column,row) or (row,column)
Rect
x represent columns and y represent rows. This means that image.at(i,j) is using (i,j) as (row,column) but Rect(x,y) is using (x,y) as (column,row).
why should I use (y,x) instead of (x,y) to access a pixel in opencv?
find the answer in the comments. It is related to how a language store matrices:
Apart from Fortran, most languages store matrices in row-major order,
so the indices are row, column, aka y, x. – Paul R Feb 6 at 8:30
Accessing elements of a cv::Mat with atfloat(i, j). Is it (x,y) or (row,col)?
OpenCV, like may other libraries, treat matrices (and images) in row-major order. That means every access is defined as (row, column)
.
Notable exceptions from this general rule are Matlab and Eigen libraries.
how OpenCv indexing works?
Yes, OpenCV treats the first and second coordinates as horizontal and vertical respectively. Specifically, the first coordinate traverses the columns and the second traverses the rows.
Why does cv::Mat ctor expect swapped size?
cv::Mat
is view as a matrix and it is accessed/created in the order you will do with a matrix, for example:
int rows = 4000, cols = 3000, type = CV_8U;
cv::Mat something( rows, cols, type, cv::Scalar::all(0));
something.at<uchar>(178 /*row*/, 928/*col*/) = 124;
In OpenCV there is more than cv::Mat, for instance cv::Point, which uses cartesian coordinates, i.e. x,y. In matrix notation speaking, the points are (col, row). And guess what, it can be also used with .at
to specify a point, like this:
something.at<uchar>(cv::Point(928/*col*/,178/*row*/)) = 124;
We also have cv::Rect
, which is constructed with the top left point and bottom right point or with the top left point and the width and the height. In code it is something like:
cv::Rect r(10, 12, 100, 120);
// or in other words
cv::Rect r2(cv::Point(10,12), cv::Size(100, 120));
This cv::Size
notation (width, height) is consistent with several other programs/os (like windows) where you can see the size specified as width x height (in your example 4000 x 3000).
Having said all this, you can see that OpenCV made itself quite flexible in the notations that can be used. Some people will stick with the matrix notations for cv::Mat and cartesian coordinates for Points and Rects, some other people prefer everything in cartesian... Why this decision? you better ask the creators of OpenCV, but I can tell you several other libraries does the same (see an image as a matrix and use matrix notation for it).
I hope this clears your doubts, but if not just leave a comment
Related Topics
Return a 2D Array from a Function
Initializer Lists and Rhs of Operators
Strptime() Equivalent on Windows
Get Human Readable Ast from C++ Code
How Are Virtual Functions and Vtable Implemented
Recursive Lambda Functions in C++11
Operator New Initializes Memory to Zero
Alternative to Itoa() For Converting Integer to String C++
Differences Between Std::Make_Unique and Std::Unique_Ptr With New
What Happens If You Static_Cast Invalid Value to Enum Class
Declaring Variables Inside Loops, Good Practice or Bad Practice
How to List-Initialize a Vector of Move-Only Type
Why Are Standard Iterator Ranges [Begin, End) Instead of [Begin, End]