在python opencv中查找图像的所有X和Y坐标 [英] Finding all the X and Y coordinates of an image in python opencv

查看:1760
本文介绍了在python opencv中查找图像的所有X和Y坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是opencv-python的初学者.我想获取代码中提到的感兴趣区域的所有X和Y坐标,并将其存储在数组中.谁能给我关于如何进行的想法?我能够运行代码,但未显示任何结果.

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.

用于检测所有X和Y坐标的图像

Image for detecting all the X and Y coordinates

我写的示例代码写在下面,

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

推荐答案

在您的代码中,您使用的是for循环,这很耗时.您宁可使用快速且敏捷的numpy库.

In your code you are using a for loop which is time consuming. You could rather make use of the fast and agile numpy library.

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

  • 我使用numpy.where()方法检索两个数组的元组索引,其中第一个数组包含白色点的x坐标,第二个数组包含白色像素的y坐标.
  • indices返回:

    (array([  1,   1,   2, ..., 637, 638, 638], dtype=int64),
     array([292, 298, 292, ...,  52,  49,  52], dtype=int64))
    

    • 然后我使用zip()方法获取包含这些点的元组列表.
      • I then used the zip() method to get a list of tuples containing those points.
      • 打印coordinates给了我带有边缘的坐标列表:

        Printing coordinates gives me a list of coordinates with edges:

        [(1, 292), (1, 298), (2, 292), .....(8, 289), (8, 295), (9, 289), (9, 295), (10, 288)] 
        

        这篇关于在python opencv中查找图像的所有X和Y坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆