简体   繁体   English

如何使用 OpenCV 或 numpy 打印线上每个点的坐标?

[英]How to use OpenCV or numpy to print the coordinate of each point on the line?

I use the following code to print the coordinate of each point on the line.我使用以下代码打印线上每个点的坐标。

1st, I used two coordinates to draw a line on the black 2D plane. 1、我用两个坐标在黑色的二维平面上画一条线。

2nd,I used the coordinates of two points to calculate the slope and intercept. 2,我用两点的坐标来计算斜率和截距。

3rd,I print the coordinates of all the points on the line in the 2D plane.第三,我打印了 2D 平面线上所有点的坐标。

I don't think I'm smart enough to do this.我不认为我足够聪明来做这件事。 Although I can solve the problem, it's not an easy way.虽然我可以解决这个问题,但这不是一个简单的方法。

My code:我的代码:

import cv2
import numpy as np

y1=-304 #point1_y
y2=477 #point2_y
x1=-957 #point1_x
x2=883 #point2_x

img=np.zeros((300,300,3),np.uint8)
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),3)
cv2.imshow('Result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

k=(y2-y1)/(x2-x1)
b = y1 - k*x1
for x in range(1,300):
    y=k*x+b
    print(x,y)

Result image:结果图像:

在此处输入图片说明

Coordinate information:坐标信息:

1 102.62934782608696
2 103.05380434782609
3 103.47826086956522
4 103.90271739130435
5 104.32717391304348
6 104.75163043478261
7 105.17608695652174
8 105.60054347826087
9 106.025
10 106.44945652173914
……

For opencv, is there any easy way to output every point on the line?对于opencv,有没有什么简单的方法可以输出线上的每个点?

I don't know whether it is possible or not with opencv, but it is possible with numpy (as you are using numpy in your code, I am assuming it's ok to you).我不知道使用 opencv 是否可行,但使用 numpy 是可能的(因为您在代码中使用 numpy,我假设这对您没问题)。

import numpy as np
x = [-957, 883]
y = [-304, 477]
x_coor = np.arange(1, x2)
y_coor = np.interp(x_coor, x, y)
coordinates = np.column_stack((x_coor, y_coor))

Here is one way to get the coordinates of the line that intersect with pixels in the image using Python/OpenCV/Numpy.这是使用 Python/OpenCV/Numpy 获取与图像中像素相交的线的坐标的一种方法。 I use np.argwhere to get the coordinates for non-zero intensity pixels in the image.我使用 np.argwhere 来获取图像中非零强度像素的坐标。

import cv2 import numpy as np导入 cv2 导入 numpy 作为 np

y1=-304 #point1_y
y2=477 #point2_y
x1=-957 #point1_x
x2=883 #point2_x

# draw line in white on black background
img=np.zeros((300,300),np.uint8)
cv2.line(img,(x1,y1),(x2,y2),255,1)

# get coordinates
coords = np.argwhere(img)
print(coords)

cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()


 [102   0]
 [102   1]
 [103   2]
 [103   3]
 [104   4]
 [104   5]
 [105   6]
 [105   7]
 [105   8]
 [106   9]
 [106  10]
 [107  11]
 [107  12]
 [108  13]
 [108  14]
 [108  15]
 [109  16]
 [109  17]
 [110  18]
 [110  19]
 [110  20]
 [111  21]
 [111  22]
 [112  23]
 [112  24]
 [113  25]
 [113  26]
 [113  27]
 [114  28]
 [114  29]
 [115  30]
 [115  31]
 [116  32]
 [116  33]
 [116  34]
 [117  35]
 [117  36]
 [118  37]
 [118  38]
 [119  39]
 [119  40]
 [119  41]
 [120  42]
 [120  43]
 [121  44]
 [121  45]
 [122  46]
 [122  47]
 [122  48]
 [123  49]
 [123  50]
 [124  51]
 [124  52]
 [125  53]
 [125  54]
 [125  55]
 [126  56]
 [126  57]
 [127  58]
 [127  59]
 [127  60]
 [128  61]
 [128  62]
 [129  63]
 [129  64]
 [130  65]
 [130  66]
 [130  67]
 [131  68]
 [131  69]
 [132  70]
 [132  71]
 [133  72]
 [133  73]
 [133  74]
 [134  75]
 [134  76]
 [135  77]
 [135  78]
 [136  79]
 [136  80]
 [136  81]
 [137  82]
 [137  83]
 [138  84]
 [138  85]
 [139  86]
 [139  87]
 [139  88]
 [140  89]
 [140  90]
 [141  91]
 [141  92]
 [142  93]
 [142  94]
 [142  95]
 [143  96]
 [143  97]
 [144  98]
 [144  99]
 [144 100]
 [145 101]
 [145 102]
 [146 103]
 [146 104]
 [147 105]
 [147 106]
 [147 107]
 [148 108]
 [148 109]
 [149 110]
 [149 111]
 [150 112]
 [150 113]
 [150 114]
 [151 115]
 [151 116]
 [152 117]
 [152 118]
 [153 119]
 [153 120]
 [153 121]
 [154 122]
 [154 123]
 [155 124]
 [155 125]
 [156 126]
 [156 127]
 [156 128]
 [157 129]
 [157 130]
 [158 131]
 [158 132]
 [158 133]
 [159 134]
 [159 135]
 [160 136]
 [160 137]
 [161 138]
 [161 139]
 [161 140]
 [162 141]
 [162 142]
 [163 143]
 [163 144]
 [164 145]
 [164 146]
 [164 147]
 [165 148]
 [165 149]
 [166 150]
 [166 151]
 [167 152]
 [167 153]
 [167 154]
 [168 155]
 [168 156]
 [169 157]
 [169 158]
 [170 159]
 [170 160]
 [170 161]
 [171 162]
 [171 163]
 [172 164]
 [172 165]
 [173 166]
 [173 167]
 [173 168]
 [174 169]
 [174 170]
 [175 171]
 [175 172]
 [175 173]
 [176 174]
 [176 175]
 [177 176]
 [177 177]
 [178 178]
 [178 179]
 [178 180]
 [179 181]
 [179 182]
 [180 183]
 [180 184]
 [181 185]
 [181 186]
 [181 187]
 [182 188]
 [182 189]
 [183 190]
 [183 191]
 [184 192]
 [184 193]
 [184 194]
 [185 195]
 [185 196]
 [186 197]
 [186 198]
 [187 199]
 [187 200]
 [187 201]
 [188 202]
 [188 203]
 [189 204]
 [189 205]
 [189 206]
 [190 207]
 [190 208]
 [191 209]
 [191 210]
 [192 211]
 [192 212]
 [192 213]
 [193 214]
 [193 215]
 [194 216]
 [194 217]
 [195 218]
 [195 219]
 [195 220]
 [196 221]
 [196 222]
 [197 223]
 [197 224]
 [198 225]
 [198 226]
 [198 227]
 [199 228]
 [199 229]
 [200 230]
 [200 231]
 [201 232]
 [201 233]
 [201 234]
 [202 235]
 [202 236]
 [203 237]
 [203 238]
 [204 239]
 [204 240]
 [204 241]
 [205 242]
 [205 243]
 [206 244]
 [206 245]
 [206 246]
 [207 247]
 [207 248]
 [208 249]
 [208 250]
 [209 251]
 [209 252]
 [209 253]
 [210 254]
 [210 255]
 [211 256]
 [211 257]
 [212 258]
 [212 259]
 [212 260]
 [213 261]
 [213 262]
 [214 263]
 [214 264]
 [215 265]
 [215 266]
 [215 267]
 [216 268]
 [216 269]
 [217 270]
 [217 271]
 [218 272]
 [218 273]
 [218 274]
 [219 275]
 [219 276]
 [220 277]
 [220 278]
 [221 279]
 [221 280]
 [221 281]
 [222 282]
 [222 283]
 [223 284]
 [223 285]
 [223 286]
 [224 287]
 [224 288]
 [225 289]
 [225 290]
 [226 291]
 [226 292]
 [226 293]
 [227 294]
 [227 295]
 [228 296]
 [228 297]
 [229 298]
 [229 299]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM