Home >Backend Development >C++ >Why does `Point(x,y)` in OpenCV produce unexpected results when used with `line()` compared to `src.at(i,j)`?

Why does `Point(x,y)` in OpenCV produce unexpected results when used with `line()` compared to `src.at(i,j)`?

Linda Hamilton
Linda HamiltonOriginal
2024-12-19 07:25:09127browse

Why does `Point(x,y)` in OpenCV produce unexpected results when used with `line()` compared to `src.at(i,j)`?

OpenCV: Understanding Point(x,y) Representation

In image processing, understanding the ordering of points can be crucial. OpenCV, a popular computer vision library, utilizes a coordinate system that can lead to confusion regarding the representation of points.

The Question:

Consider an image stored in a Matrix src with dimensions 300x200. When applying the following operation:

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 );
  }
}
imshow("A",src);
waitKey(0);

The expected result is to fill the entire image with white lines. However, only the upper portion becomes white.

This behavior contrasts with the following operation:

for(int i = 0; i < src.rows; i++){
  for(int j = 0; j < src.cols; j++){
    src.at<uchar>(i,j)=255;
  }
}
imshow("A",src);
waitKey(0);

which successfully fills the entire image in white.

The Answer:

The explanation lies in the different representations used for points:

  • src.at(i,j): Uses (i,j) as (row,column).
  • Point(x,y): Uses (x,y) as (column,row).

This discrepancy stems from the dual nature of OpenCV's Mat, which represents both images and matrices. In matrix notation, the row-major order is used, where the first index represents the row and the second index represents the column. This convention aligns with src.at(i,j).

In contrast, the coordinate system for points follows the image notation, where the first value specifies the x-direction (column) and the second value specifies the y-direction (row). This explains the use of (column,row) ordering in Point(x,y) and the alignment with src.at(column,row).

Understanding this distinction is essential for effectively accessing and modifying points in OpenCV.

The above is the detailed content of Why does `Point(x,y)` in OpenCV produce unexpected results when used with `line()` compared to `src.at(i,j)`?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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