在python opencv中查找图像的所有X和Y坐标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44068654/
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
Finding all the X and Y coordinates of an image in python opencv
提问by Harikrishnan R
I am a beginner in opencv-python. I want to get all the X and Y coordinates of for the region of the interest mentioned in the code and store it in an array. Can anyone give me an idea on how to proceed? I was able to run the code, but is not showing any results.
我是 opencv-python 的初学者。我想获取代码中提到的感兴趣区域的所有 X 和 Y 坐标,并将其存储在一个数组中。谁能给我一个如何继续的想法?我能够运行代码,但没有显示任何结果。
Image for detecting all the X and Y coordinates
用于检测所有 X 和 Y 坐标的图像


The sample code i wrote is written below,
我写的示例代码写在下面,
import cv2
import numpy as np
import matplotlib.pyplot as plt
import imutils
img = cv2.imread("/home/harikrishnan/Desktop/image.jpg",0)
img1 = imutils.resize(img)
img2 = img1[197:373,181:300] #roi of the image
ans = []
for y in range(0, img2.shape[0]): #looping through each rows
for x in range(0, img2.shape[1]): #looping through each column
if img2[y, x] != 0:
ans = ans + [[x, y]]
ans = np.array(ans)
print ans
回答by Jeru Luke
In your code you are using a forloop which is time consuming. You could rather make use of the fast and agile numpylibrary.
在您的代码中,您正在使用一个for耗时的循环。您宁愿使用快速而敏捷的numpy库。
import cv2
import numpy as np
import matplotlib.pyplot as plt
import imutils
img = cv2.imread("/home/harikrishnan/Desktop/image.jpg",0)
img1 = imutils.resize(img)
img2 = img1[197:373,181:300] #roi of the image
indices = np.where(img2!= [0])
coordinates = zip(indices[0], indices[1])
- I used the numpy.where() method to retrieve a tuple indices of two arrays where the first array contains the x-coordinates of the white points and the second array contains the y-coordinates of the white pixels.
- 我使用 numpy.where() 方法来检索两个数组的元组索引,其中第一个数组包含白点的 x 坐标,第二个数组包含白色像素的 y 坐标。
indicesreturns:
indices返回:
(array([ 1, 1, 2, ..., 637, 638, 638], dtype=int64),
array([292, 298, 292, ..., 52, 49, 52], dtype=int64))
- I then used the
zip()method to get a list of tuples containing those points.
- 然后我使用该
zip()方法获取包含这些点的元组列表。
Printing coordinatesgives me a list of coordinates with edges:
打印coordinates给了我一个带边缘的坐标列表:
[(1, 292), (1, 298), (2, 292), .....(8, 289), (8, 295), (9, 289), (9, 295), (10, 288)]
回答by MysticVagabond
this post lets you know how to get the pixel of an image http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_core/py_basic_ops/py_basic_ops.html
这篇文章让你知道如何获取图像的像素 http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_core/py_basic_ops/py_basic_ops.html
if you just iterate over the image in a nested for-loop you can perform whatever operation you want on said pixel eg, add x and y to an array if the colour isnt white
如果您只是在嵌套的 for 循环中迭代图像,您可以对所述像素执行任何您想要的操作,例如,如果颜色不是白色,则将 x 和 y 添加到数组中
EDIT: i had to read up on what Chris commenting on the post meant by XY Problem but i agree, is there something youve tried and hasnt worked that youd like us to help fix?
编辑:我必须阅读 Chris 对 XY 问题所指的帖子发表的评论,但我同意,是否有您尝试过但没有奏效的事情希望我们帮助修复?

