繁体   English   中英

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

[英]Convert an image to a 2D array of coordinates in Python for two point correlation

我需要从astroML Python模块执行两点关联函数,我的数据原本是jpg图像(黑白),然后使用OpenCV图像阈值处理将其转换为二进制图像(不确定我做对了)。 问题是现在我如何将2D二进制矩阵或一个和零转换为仅一个的坐标列表。 基本代码行是这一行:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM