2015-02-06 174 views
1

我需要從astroML Python module執行兩點關聯函數,我的數據本來是JPG圖片,黑色和白色,而我用它轉換爲二進制圖像OpenCV image thresholding(不知道我這樣做是正確的)。現在的問題是如何我現在的2D二元矩陣或一和零轉換爲只有那些座標列表。基本的代碼行,這是一個:轉換的圖像座標Python中的二維數組有兩個點相關

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

回答

2

是啊,就像我想過通過循環在陣列上實現最多的事: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])) 
+1

感謝克里斯託弗! – Ohm 2015-02-06 20:23:15

相關問題