在Python中将图像转换为二维坐标数组以实现两点相关性

在Python中将图像转换为二维坐标数组以实现两点相关性,python,opencv,numpy,fft,correlation,Python,Opencv,Numpy,Fft,Correlation,我需要从中执行两点相关函数,我的数据最初是一幅jpg图像,黑白,我使用将其转换为二值图像(不确定是否正确)。现在的问题是,我如何将2D二进制矩阵或1和0转换为仅1的坐标列表。基本代码行如下所示: import numpy as np import cv2 from astroML.correlation import two_point import matplotlib.pyplot as plt im_normal = cv2.imread('example.jpg') im_gray =

我需要从中执行两点相关函数,我的数据最初是一幅jpg图像,黑白,我使用将其转换为二值图像(不确定是否正确)。现在的问题是,我如何将2D二进制矩阵或1和0转换为仅1的坐标列表。基本代码行如下所示:

import numpy as np
import cv2
from astroML.correlation import two_point
import matplotlib.pyplot as plt

im_normal = cv2.imread('example.jpg')
im_gray = cv2.imread('example.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE)
(thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
我必须在矩阵的所有单元上循环,并拉坐标,还是有一种简单的方法来做

我要在其上执行分析的图像-

是的,就像我想通过在阵列上循环来完成的大多数事情一样:numpy有一个内置的解决方案

[numpy.nonzero][1]

numpy.nonzero(a)
Return the indices of the elements that are non-zero.

    Returns a tuple of arrays, one for each dimension of a, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values can be obtained with:

    `a[nonzero(a)]`

    To group the indices by element, rather than dimension, use:

    `transpose(nonzero(a))`

    The result of this is always a 2-D array, with a row for each non-zero element.
代码示例:

>>> x = np.eye(3)
>>> x
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
>>> np.nonzero(x)
(array([0, 1, 2]), array([0, 1, 2]))