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

2024-04-20 09:18:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用下面的代码打印线上每个点的坐标

首先,我用两个坐标在黑色的2D平面上画了一条线

第二,我用两点的坐标来计算斜率和截距

第三,我在二维平面上打印直线上所有点的坐标

我认为我不够聪明,不能做这件事。虽然我能解决这个问题,但这不是一个容易的办法

我的代码:

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)

结果图像:

enter image description here

坐标信息:

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
……

对于opencv,是否有简单的方法输出线上的每个点?


Tags: 代码importimgnpcv2平面直线x1
2条回答

下面是使用Python/OpenCV/Numpy获取与图像中像素相交的直线坐标的一种方法。我使用np.argwhere获取图像中非零强度像素的坐标

进口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]]

我不知道使用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))

相关问题 更多 >