python - 在 Python 中获取彩色形状周围边界的坐标

标签 python arrays opencv coordinates

我有一张图片,里面有一个蓝色的形状。我想以形状周围像素的数组格式获取坐标 (x,y)。我附上了样本图像。我想获取与蓝色形状接壤的像素的坐标。

enter image description here

最佳答案

这是一种使用 Python/OpenCV 列出边界沿线每个像素坐标的方法。 (我假设当你说像素时,你想要的不仅仅是多边形的顶点)

  • 阅读输入
  • 转换为灰色
  • Otsu 阈值和反转
  • 找到外部轮廓
  • 列出轮廓点

输入:

enter image description here

import cv2
import numpy as np

# read image
img = cv2.imread('blue_hexagon.png')

# convert to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# threshold and invert so hexagon is white on black background
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
thresh = 255 - thresh

# get contours
result = np.zeros_like(img)
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours = contours[0] if len(contours) == 2 else contours[1]
cntr = contours[0]
cv2.drawContours(result, [cntr], 0, (255,255,255), 1)

# print number of points along contour
print('number of points: ',len(cntr))

print('')

# list contour points
for pt in cntr:
    print(pt)

# save resulting images
cv2.imwrite('blue_hexagon_thresh.png',thresh)
cv2.imwrite('blue_hexagon_contour.png',result)  

# show thresh and contour   
cv2.imshow("thresh", thresh)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

阈值图像:

enter image description here

轮廓图:

enter image description here

文本信息:

number of points:  608

[[57 15]]
[[56 16]]
[[55 17]]
[[55 18]]
[[54 19]]
[[54 20]]
[[53 21]]
[[52 22]]
[[52 23]]
[[51 24]]
[[51 25]]
[[50 26]]
[[49 27]]
[[49 28]]
[[48 29]]
[[48 30]]
[[47 31]]
[[47 32]]
[[46 33]]
[[45 34]]
[[45 35]]
[[44 36]]
[[44 37]]
[[43 38]]
[[42 39]]
[[42 40]]
[[41 41]]
[[41 42]]
[[40 43]]
[[40 44]]
[[39 45]]
[[38 46]]
[[38 47]]
[[37 48]]
[[37 49]]
[[36 50]]
[[35 51]]
[[35 52]]
[[34 53]]
[[34 54]]
[[33 55]]
[[33 56]]
[[32 57]]
[[31 58]]
[[31 59]]
[[30 60]]
[[30 61]]
[[29 62]]
[[29 63]]
[[28 64]]
[[27 65]]
[[27 66]]
[[26 67]]
[[26 68]]
[[25 69]]
[[25 70]]
[[24 71]]
[[23 72]]
[[23 73]]
[[22 74]]
[[22 75]]
[[21 76]]
[[20 77]]
[[20 78]]
[[19 79]]
[[19 80]]
[[18 81]]
[[18 82]]
[[17 83]]
[[17 84]]
[[16 85]]
[[15 86]]
[[15 87]]
[[14 88]]
[[14 89]]
[[13 90]]
[[12 91]]
[[12 92]]
[[11 93]]
[[11 94]]
[[10 95]]
[[10 96]]
[[ 9 97]]
[[ 8 98]]
[[ 8 99]]
[[  7 100]]
[[  7 101]]
[[  6 102]]
[[  6 103]]
[[  5 104]]
[[  4 105]]
[[  4 106]]
[[  3 107]]
[[  3 108]]
[[  2 109]]
[[  2 110]]
[[  1 111]]
[[  1 112]]
[[  1 113]]
[[  2 114]]
[[  2 115]]
[[  3 116]]
[[  3 117]]
[[  4 118]]
[[  4 119]]
[[  5 120]]
[[  5 121]]
[[  6 122]]
[[  7 123]]
[[  7 124]]
[[  8 125]]
[[  8 126]]
[[  9 127]]
[[ 10 128]]
[[ 10 129]]
[[ 11 130]]
[[ 11 131]]
[[ 12 132]]
[[ 12 133]]
[[ 13 134]]
[[ 14 135]]
[[ 14 136]]
[[ 15 137]]
[[ 15 138]]
[[ 16 139]]
[[ 16 140]]
[[ 17 141]]
[[ 18 142]]
[[ 18 143]]
[[ 19 144]]
[[ 19 145]]
[[ 20 146]]
[[ 20 147]]
[[ 21 148]]
[[ 22 149]]
[[ 22 150]]
[[ 23 151]]
[[ 23 152]]
[[ 24 153]]
[[ 25 154]]
[[ 25 155]]
[[ 26 156]]
[[ 26 157]]
[[ 27 158]]
[[ 27 159]]
[[ 28 160]]
[[ 29 161]]
[[ 29 162]]
[[ 30 163]]
[[ 30 164]]
[[ 31 165]]
[[ 31 166]]
[[ 32 167]]
[[ 33 168]]
[[ 33 169]]
[[ 34 170]]
[[ 34 171]]
[[ 35 172]]
[[ 35 173]]
[[ 36 174]]
[[ 37 175]]
[[ 37 176]]
[[ 38 177]]
[[ 38 178]]
[[ 39 179]]
[[ 40 180]]
[[ 40 181]]
[[ 41 182]]
[[ 41 183]]
[[ 42 184]]
[[ 42 185]]
[[ 43 186]]
[[ 44 187]]
[[ 44 188]]
[[ 45 189]]
[[ 45 190]]
[[ 46 191]]
[[ 47 192]]
[[ 47 193]]
[[ 48 194]]
[[ 48 195]]
[[ 49 196]]
[[ 49 197]]
[[ 50 198]]
[[ 50 199]]
[[ 51 200]]
[[ 52 201]]
[[ 52 202]]
[[ 53 203]]
[[ 53 204]]
[[ 54 205]]
[[ 55 206]]
[[ 55 207]]
[[ 56 208]]
[[ 57 209]]
[[ 58 209]]
[[ 59 209]]
[[ 60 209]]
[[ 61 209]]
[[ 62 209]]
[[ 63 209]]
[[ 64 209]]
[[ 65 209]]
[[ 66 209]]
[[ 67 209]]
[[ 68 209]]
[[ 69 209]]
[[ 70 209]]
[[ 71 209]]
[[ 72 209]]
[[ 73 209]]
[[ 74 209]]
[[ 75 209]]
[[ 76 209]]
[[ 77 209]]
[[ 78 209]]
[[ 79 209]]
[[ 80 209]]
[[ 81 209]]
[[ 82 209]]
[[ 83 209]]
[[ 84 209]]
[[ 85 209]]
[[ 86 209]]
[[ 87 209]]
[[ 88 209]]
[[ 89 209]]
[[ 90 209]]
[[ 91 209]]
[[ 92 209]]
[[ 93 209]]
[[ 94 209]]
[[ 95 209]]
[[ 96 209]]
[[ 97 209]]
[[ 98 209]]
[[ 99 209]]
[[100 209]]
[[101 209]]
[[102 209]]
[[103 209]]
[[104 209]]
[[105 209]]
[[106 209]]
[[107 209]]
[[108 209]]
[[109 209]]
[[110 209]]
[[111 209]]
[[112 209]]
[[113 209]]
[[114 209]]
[[115 209]]
[[116 209]]
[[117 209]]
[[118 209]]
[[119 209]]
[[120 209]]
[[121 209]]
[[122 209]]
[[123 209]]
[[124 209]]
[[125 209]]
[[126 209]]
[[127 209]]
[[128 209]]
[[129 209]]
[[130 209]]
[[131 209]]
[[132 209]]
[[133 209]]
[[134 209]]
[[135 209]]
[[136 209]]
[[137 209]]
[[138 209]]
[[139 209]]
[[140 209]]
[[141 209]]
[[142 209]]
[[143 209]]
[[144 209]]
[[145 209]]
[[146 209]]
[[147 209]]
[[148 209]]
[[149 209]]
[[150 209]]
[[151 209]]
[[152 209]]
[[153 209]]
[[154 209]]
[[155 209]]
[[156 209]]
[[157 209]]
[[158 209]]
[[159 209]]
[[160 209]]
[[161 209]]
[[162 209]]
[[163 209]]
[[164 209]]
[[165 209]]
[[166 209]]
[[167 209]]
[[168 208]]
[[169 207]]
[[169 206]]
[[170 205]]
[[171 204]]
[[171 203]]
[[172 202]]
[[172 201]]
[[173 200]]
[[174 199]]
[[174 198]]
[[175 197]]
[[175 196]]
[[176 195]]
[[176 194]]
[[177 193]]
[[178 192]]
[[178 191]]
[[179 190]]
[[179 189]]
[[180 188]]
[[181 187]]
[[181 186]]
[[182 185]]
[[182 184]]
[[183 183]]
[[183 182]]
[[184 181]]
[[184 180]]
[[185 179]]
[[186 178]]
[[186 177]]
[[187 176]]
[[187 175]]
[[188 174]]
[[189 173]]
[[189 172]]
[[190 171]]
[[190 170]]
[[191 169]]
[[191 168]]
[[192 167]]
[[193 166]]
[[193 165]]
[[194 164]]
[[194 163]]
[[195 162]]
[[196 161]]
[[196 160]]
[[197 159]]
[[197 158]]
[[198 157]]
[[198 156]]
[[199 155]]
[[199 154]]
[[200 153]]
[[201 152]]
[[201 151]]
[[202 150]]
[[202 149]]
[[203 148]]
[[204 147]]
[[204 146]]
[[205 145]]
[[205 144]]
[[206 143]]
[[206 142]]
[[207 141]]
[[208 140]]
[[208 139]]
[[209 138]]
[[209 137]]
[[210 136]]
[[211 135]]
[[211 134]]
[[212 133]]
[[212 132]]
[[213 131]]
[[213 130]]
[[214 129]]
[[214 128]]
[[215 127]]
[[216 126]]
[[216 125]]
[[217 124]]
[[217 123]]
[[218 122]]
[[219 121]]
[[219 120]]
[[220 119]]
[[220 118]]
[[221 117]]
[[221 116]]
[[222 115]]
[[222 114]]
[[223 113]]
[[223 112]]
[[223 111]]
[[222 110]]
[[222 109]]
[[221 108]]
[[221 107]]
[[220 106]]
[[220 105]]
[[219 104]]
[[219 103]]
[[218 102]]
[[217 101]]
[[217 100]]
[[216  99]]
[[216  98]]
[[215  97]]
[[214  96]]
[[214  95]]
[[213  94]]
[[213  93]]
[[212  92]]
[[212  91]]
[[211  90]]
[[210  89]]
[[210  88]]
[[209  87]]
[[209  86]]
[[208  85]]
[[208  84]]
[[207  83]]
[[206  82]]
[[206  81]]
[[205  80]]
[[205  79]]
[[204  78]]
[[204  77]]
[[203  76]]
[[202  75]]
[[202  74]]
[[201  73]]
[[201  72]]
[[200  71]]
[[199  70]]
[[199  69]]
[[198  68]]
[[198  67]]
[[197  66]]
[[197  65]]
[[196  64]]
[[196  63]]
[[195  62]]
[[194  61]]
[[194  60]]
[[193  59]]
[[193  58]]
[[192  57]]
[[191  56]]
[[191  55]]
[[190  54]]
[[190  53]]
[[189  52]]
[[189  51]]
[[188  50]]
[[187  49]]
[[187  48]]
[[186  47]]
[[186  46]]
[[185  45]]
[[184  44]]
[[184  43]]
[[183  42]]
[[183  41]]
[[182  40]]
[[182  39]]
[[181  38]]
[[180  37]]
[[180  36]]
[[179  35]]
[[179  34]]
[[178  33]]
[[178  32]]
[[177  31]]
[[176  30]]
[[176  29]]
[[175  28]]
[[175  27]]
[[174  26]]
[[174  25]]
[[173  24]]
[[172  23]]
[[172  22]]
[[171  21]]
[[171  20]]
[[170  19]]
[[169  18]]
[[169  17]]
[[168  16]]
[[167  15]]
[[166  15]]
[[165  15]]
[[164  15]]
[[163  15]]
[[162  15]]
[[161  15]]
[[160  15]]
[[159  15]]
[[158  15]]
[[157  15]]
[[156  15]]
[[155  15]]
[[154  15]]
[[153  15]]
[[152  15]]
[[151  15]]
[[150  15]]
[[149  15]]
[[148  15]]
[[147  15]]
[[146  15]]
[[145  15]]
[[144  15]]
[[143  15]]
[[142  15]]
[[141  15]]
[[140  15]]
[[139  15]]
[[138  15]]
[[137  15]]
[[136  15]]
[[135  15]]
[[134  15]]
[[133  15]]
[[132  15]]
[[131  15]]
[[130  15]]
[[129  15]]
[[128  15]]
[[127  15]]
[[126  15]]
[[125  15]]
[[124  15]]
[[123  15]]
[[122  15]]
[[121  15]]
[[120  15]]
[[119  15]]
[[118  15]]
[[117  15]]
[[116  15]]
[[115  15]]
[[114  15]]
[[113  15]]
[[112  15]]
[[111  15]]
[[110  15]]
[[109  15]]
[[108  15]]
[[107  15]]
[[106  15]]
[[105  15]]
[[104  15]]
[[103  15]]
[[102  15]]
[[101  15]]
[[100  15]]
[[99 15]]
[[98 15]]
[[97 15]]
[[96 15]]
[[95 15]]
[[94 15]]
[[93 15]]
[[92 15]]
[[91 15]]
[[90 15]]
[[89 15]]
[[88 15]]
[[87 15]]
[[86 15]]
[[85 15]]
[[84 15]]
[[83 15]]
[[82 15]]
[[81 15]]
[[80 15]]
[[79 15]]
[[78 15]]
[[77 15]]
[[76 15]]
[[75 15]]
[[74 15]]
[[73 15]]
[[72 15]]
[[71 15]]
[[70 15]]
[[69 15]]
[[68 15]]
[[67 15]]
[[66 15]]
[[65 15]]
[[64 15]]
[[63 15]]
[[62 15]]
[[61 15]]
[[60 15]]
[[59 15]]
[[58 15]]

关于python - 在 Python 中获取彩色形状周围边界的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62824131/

相关文章:

python - 如何在不注释类型的情况下添加数据类字段?

java - 如何在java中的方法内部使用类中的变量

java - 使用Java Sockets发送ByteArray(Android编程)

c++ - 指针取不到数据

dll - OpenCV highgui110.dll 链接错误

python - 尝试/除非不排除ValueError

python 扭曲: fork for background non-returning processing

python - 将标准输出从 subprocess.Popen 保存到文件,并将更多内容写入文件

javascript - 添加了变量值但数组没有被推送

OpenCV 如何在 JAVA 中用二维数组初始化 Mat