In image processing libraries like OpenCV, the coordinate system and order of matrix elements can be confusing. This article aims to clarify these concepts, particularly the use of Point(x,y) in OpenCV compared to at(i,j) for accessing matrix elements.
OpenCV's cv::Mat data structure is utilized for both images and matrices. Mathematically, matrices are ordered in row-major order, meaning the first index designates the row and the second index the column.
Points in a coordinate system are commonly ordered with the x-axis first and the y-axis second (Cartesian coordinate system). However, in image processing, the axis ordering used is known as image notation, where the first value represents the x-direction (abscissa) and the second value the y-direction (ordinate).
OCV's at(i,j) method allows accessing elements using (row, column) indices, with the top-left corner of the matrix being (0,0).
The Point(x,y) constructor in OpenCV enables accessing elements of a cv::Mat using image notation, where x represents the column and y the row.
Consider a 300x200 matrix (src):
for (int i = 0; i < src.rows; i++) { for (int j = 0; j < src.cols; j++) { line(src, Point(i, j), Point(i, j), Scalar(255, 0, 0), 1, 8); } }
This code is intended to draw a white line on the entire image. However, it only fills in the upper portion of the image. This is because the line function uses the Point(i,j) constructor, which interprets i as the column and j as the row.
Using at(i,j) instead of Point(i,j) would correctly fill in the entire image with white, as it uses (row, column) ordering.
Understanding the distinction between row/column ordering in matrices and the image notation used for points in OpenCV is crucial for effectively manipulating images and matrices in OpenCV. The at(i,j) method and the Point(x,y) constructor provide convenient ways to access elements based on different coordinate systems, catering to the specific needs of the application.
The above is the detailed content of How Does OpenCV's `Point(x,y)` Differ from `at(i,j)` in Matrix Access?. For more information, please follow other related articles on the PHP Chinese website!