C++ OpenCV Point(x,y) 表示 (column,row) 或 (row,column)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25642532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:22:40  来源:igfitidea点击:

OpenCV Point(x,y) represent (column,row) or (row,column)

c++opencv

提问by user3747190

I have a 300x200 image in a Matrix src. I am doing the following operation on the image.

我在 Matrix 中有一个 300x200 的图像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 );
  }
}
imshow("A",src);
waitKey(0);

I was expecting it to cover the entire image in white, but lower portion of the image remain empty. While if I do this

我期待它以白色覆盖整个图像,但图像的下部仍然是空的。如果我这样做

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

Entire image is covered in white. So, this means that src.at<uchar>(i,j)is using (i,j)as (row,column) but Point(x,y)is using (x,y)as (column,row)

整个图像被白色覆盖。所以,这意味着src.at<uchar>(i,j)使用(i,j)as (row,column) 但Point(x,y)使用(x,y)as (column,row)

回答by Micka

So, this means that src.at(i,j) is using (i,j) as (row,column) but Point(x,y) is using (x,y) as (column,row)

所以,这意味着 src.at(i,j) 使用 (i,j) 作为 (row,column) 但 Point(x,y) 使用 (x,y) 作为 (column,row)

That is right!Since this seems to confuse many people I'll write my interpreation for the reason:

没错!由于这似乎让很多人感到困惑,因此我将写下我的解释:

In OpenCV, cv::Matis used for both, images and matrices, since a discrete image is basically the same as a matrix.

在 OpenCV 中,cv::Mat用于图像和矩阵,因为离散图像基本上与矩阵相同。

In mathematics, we have some different things:

在数学中,我们有一些不同的东西:

  1. matrices, which have have a number of rows and a number of columns.
  2. graphs (of functions), which have multiple axes and graphically represent the graph in form of an image.
  3. points, which are ordered by the axes of the coordinate system which normally is a cartesian coordinate.
  1. 矩阵,有许多行和许多列。
  2. (函数的)图形,具有多个轴并以图像的形式以图形方式表示图形。
  3. 点,它们按坐标系的轴排序,坐标系通常是笛卡尔坐标。


1.For matricesthe mathematical notationis to order in row-major-order which is

1.对于矩阵,数学符号是按行主序排列,即

Following conventional matrix notation, rows are numbered by the first index of a two-dimensional array and columns by the second index, i.e., a1,2 is the second element of the first row, counting downwards and rightwards. (Note this is the opposite of Cartesian conventions.)

按照传统的矩阵表示法,行由二维数组的第一个索引编号,列由第二个索引编号,即 a1,2 是第一行的第二个元素,向下和向右计数。(请注意,这与笛卡尔约定相反。)

Taken from http://en.wikipedia.org/wiki/Row-major_order#Explanation_and_example

取自http://en.wikipedia.org/wiki/Row-major_order#Explanation_and_example

As in mathematics, row:0, column:0 is the top-left element of the matrix. Row/column are just like in tables...

在数学中,row:0, column:0 是矩阵的左上角元素。行/列就像在表格中一样......

0/0---column--->
 |
 |
row
 |
 |
 v


2.For Pointsa coordinate system is chosen that fulfils two things: 1. it uses the same unit-sizes and the same "origin" as the matrix notation, so top-left is Point(0,0) and axis length 1 means the length of 1 row or 1 column. 2. it uses "image notation" for axis-ordering, which means that abscissa (horizontal axis) is the first value designating the x-direction and the ordinate (vertical axis) is the second value designating the y-direction.

2.对于Points,选择一个满足两件事的坐标系:1. 它使用与矩阵符号相同的单位大小和相同的“原点”,所以左上角是 Point(0,0),轴长度 1 表示1 行或 1 列的长度。2. 它使用“图像符号”进行轴排序,这意味着横坐标(水平轴)是指定 x 方向的第一个值,而纵坐标(垂直轴)是指定 y 方向的第二个值。

The point where the axes meet is the common origin of the two number lines and is simply called the origin. It is often labeled O and if so then the axes are called Ox and Oy. A plane with x- and y-axes defined is often referred to as the Cartesian plane or xy plane. The value of x is called the x-coordinate or abscissa and the value of y is called the y-coordinate or ordinate.

The choices of letters come from the original convention, which is to use the latter part of the alphabet to indicate unknown values. The first part of the alphabet was used to designate known values.

轴线相交的点是两条数轴的共同原点,简称原点。它通常标记为 O,如果是,则轴称为 Ox 和 Oy。定义了 x 轴和 y 轴的平面通常称为笛卡尔平面或 xy 平面。x 的值称为 x 坐标或横坐标,y 的值称为 y 坐标或纵坐标。

字母的选择来自原始约定,即使用字母表的后半部分来表示未知值。字母表的第一部分用于指定已知值。

http://en.wikipedia.org/wiki/Cartesian_coordinate_system#Two_dimensions

http://en.wikipedia.org/wiki/Cartesian_coordinate_system#Two_dimensions

so in a perfect world we would choose the coordinate system of points/images to be:

所以在一个完美的世界中,我们会选择点/图像的坐标系:

 ^
 |
 |
 Y
 |
 |
0/0---X--->

but since we want to have that origin in top-left and positive values to go to bottom, it is instead:

但由于我们希望将左上角的原点和正值放在底部,因此改为:

0/0---X--->
 |
 |
 Y
 |
 |
 v


So for image processing people row-first notation might be weird, but for mathematicans x-axis-first would be strange to access a matrix.

因此,对于图像处理人员,行优先表示法可能很奇怪,但对于数学家来说,x 轴优先访问矩阵会很奇怪。

So in OpenCV you can use: mat.at<type>(row,column)or mat.at<type>(cv::Point(x,y))to access the same point if x=columnand y=rowwhich is perfectly comprehensible =)

因此,在 OpenCV 中,您可以使用:mat.at<type>(row,column)mat.at<type>(cv::Point(x,y))访问同一点,如果x=column并且y=row完全可以理解 =)

Hope this correct. I don't know much about the notations, but that's what my experience in mathematics and imaging tells me.

希望这是正确的。我不太了解符号,但这就是我在数学和成像方面的经验告诉我的。

回答by Dev Aggarwal

I found a quick and fast fix to this problem by just converting the coordinates from opencv to Cartesian coordinates in 4th quadrant, simply by putting a (-)ve sign in front of the y coordinate.

我找到了一个快速解决这个问题的方法,只需将坐标从 opencv 转换为第四象限的笛卡尔坐标,只需在 y 坐标前面放一个 (-)ve 符号。

This way, i was able to use my existing algorithms and all the standard Cartesian system equations with opencv without putting much overhead on the system by doing an expensive conversion between coordinate systems.

通过这种方式,我能够在 opencv 中使用我现有的算法和所有标准笛卡尔系统方程,而不会通过在坐标系之间进行昂贵的转换而给系统带来太多开销。

0/0---X--->
 |
 |
 Y
 |
 |
 v
 (opencv)

0/0---X----> 
|
|
|
-Y
|
|
v
(4th quadrant)

回答by Tommaso Di Noto

Here's a visual example to distinguish python's [row, columns] from OpenCV's [x,y].

这是一个视觉示例,用于区分 python 的 [row, columns] 和 OpenCV 的 [x,y]。

import numpy as np
import matplotlib.pyplot as plt
import cv2

img = np.zeros((5,5))  # initialize empty image as numpy array
img[0,2] = 1  # assign 1 to the pixel of row 0 and column 2

M = cv2.moments(img)  # calculate moments of binary image
cX = int(M["m10"] / M["m00"])  # calculate x coordinate of centroid
cY = int(M["m01"] / M["m00"])  # calculate y coordinate of centroid

img2 = np.zeros((5,5))  # initialize another empty image
img2[cX,cY] = 1  # assign 1 to the pixel with x = cX and y = cY

img3 = np.zeros((5,5))  # initialize another empty image
img3[cY,cX] = 1  # invert x and y

plt.figure()
plt.subplots_adjust(wspace=0.4)  # add space between subplots
plt.subplot(131), plt.imshow(img, cmap = "gray"), plt.title("With [rows,cols]")
plt.subplot(132), plt.imshow(img2, cmap = "gray"), plt.title("With [x,y]")
plt.subplot(133), plt.imshow(img3, cmap= "gray"), plt.title("With [y,x]"), plt.xlabel('x'), plt.ylabel('y')

This will output:

这将输出:

enter image description here

在此处输入图片说明