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

标签 python opencv numpy fft correlation

我需要从 astroML Python module 执行两点相关函数,我的数据最初是一个 jpg 图像,黑白图像,我使用 OpenCV image thresholding 将其转换为二进制图像(不确定我做对了)。问题是现在我如何将 2D 二进制矩阵或 ones 和 zeros 转换为只有 ones 的坐标列表。基本代码行是这一行:

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 方法可以做到这一点?

我要对其执行分析的图像 - enter image description here

最佳答案

是的,就像我想通过遍历数组来完成的大多数事情一样: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]))

关于python - 将图像转换为 Python 中的二维坐标数组以实现两点相关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28373591/

相关文章:

python - 为什么 python 异常通常不打印违规值?

python - 使用 PuLP 进行线性优化,变量附加条件

c++ - 为什么这个 matlab 和 C++ 代码会产生不同的结果?

c++ - 线条和边缘检测器,opencv

iphone - 确定图像亮度/亮度

python - 将 numpy 数组列表合并到一个数组中(快速)

python - .eigenvals 创建一个新变量

python - 有人试过 NetBeans 6.5 Python IDE 吗?

python - pip 列出了错误安装的软件包版本?

python - 使用 Python Numpy 将图像投影到球体内部