Home > Backend Development > C++ > How Does OpenCV's `Point(x,y)` Differ from `at(i,j)` in Matrix Access?

How Does OpenCV's `Point(x,y)` Differ from `at(i,j)` in Matrix Access?

DDD
Release: 2024-12-09 17:48:15
Original
960 people have browsed it

How Does OpenCV's `Point(x,y)` Differ from `at(i,j)` in Matrix Access?

Understanding the Coordinate System in OpenCV

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.

Matrix Ordering

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.

Point Coordinates

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).

OpenCV Matrix Access

OCV's at(i,j) method allows accessing elements using (row, column) indices, with the top-left corner of the matrix being (0,0).

Point Access

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.

Example

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);
  }
}
Copy after login

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.

Conclusion

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template