2014-01-22 112 views
0

我有我想要複製到數組/列表的1x128灰度圖像。 當我讀出的圖像我看到它只有一個指數大:n = len(res)如何在OpenCV中訪問圖像的像素值

我想讀出每個像素值,並且說,如果像素I> 128它必須是1,否則必須爲0

for index in res: 
     if res[h] > 128: 
      res[h] = 1 
     else: 
      res[h] = 0 
     h = h + 1 

但資源只有一個元素,看起來像這樣:res = [[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 128 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255]]

我想這有一個數組,這樣我可以把每一個指標(即是轉換(到1或0)到輸入的神經網絡

Can s omeone解釋我該如何做到這一點? 這裏是我的一塊主要的:

if __name__ == "__main__": 
# Camera ID to read video from (numbered from 0) 
camera_id = 0 
dev = open_camera(camera_id) # open the camera as a video capture device 

while True: 
    img_orig = get_frame(dev) # Get a frame from the camera 
    if img_orig is not None: # if we did get an image 
     cv2.imshow("video", img_orig) # display the image in a window named "video" 
    else: # if we failed to capture (camera disconnected?), then quit 
     break 

    gray = cv2.cvtColor(img_orig, cv2.COLOR_BGR2GRAY) 
    cv2.imshow("video1", gray) # display the image in a window named "video" 
    crop = gray[310:410, 0:1280] 
    gauss = cv2.GaussianBlur(crop,(5,5),0) 
    (thresh, im_bw) = cv2.threshold(gauss, 128, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) 
    cv2.imshow('frame2',im_bw) 
    height1, width1 = gauss.shape 
    res = cv2.resize(im_bw,(width1/5, height1/100), interpolation = cv2.INTER_LINEAR) 
    height2, width2 = res.shape 
    cv2.imshow('frame3',res) 

    n = len(res) 
    h = 0 
    for index in res: 
     if res[h] > 128: 
      res[h] = 1 
     else: 
      res[h] = 0 
     h = h + 1 
    #print "Binair is: %s" % img_binair 
    print "image line is: %s" % res 
+0

儘可能避免python for循環。你可以使用numpy索引。嘗試'y = np.where(x> 127,1,0)' –

回答

0

你必須避免儘可能在Python循環,尤其是在你使用大型數組一樣的圖像。它會很慢。

如果您使用的是Numpy,請使用Numpy索引以獲得更好的性能。您可以使用np.where()。我會告訴你一個例子:

In [10]: x = np.random.randint(1,255,(5,5)) 

In [11]: x 
Out[11]: 
array([[ 71, 63, 219, 34, 73], 
     [254, 19, 5, 25, 178], 
     [165, 142, 20, 208, 61], 
     [ 29, 3, 22, 26, 10], 
     [ 11, 208, 147, 206, 186]]) 

In [12]: y = np.where(x>127,1,0) 

In [13]: y 
Out[13]: 
array([[0, 0, 1, 0, 0], 
     [1, 0, 0, 0, 1], 
     [1, 1, 0, 1, 0], 
     [0, 0, 0, 0, 0], 
     [0, 1, 1, 1, 1]]) 

或者,因爲你正在使用OpenCV的,可以直接申請cv2.threshold()這一點。我現在沒有OpenCV,所以我不能向您展示示例,但請嘗試下面的代碼行。

ret, y = cv2.threshold(x, 127, 1, cv2.THRESH_BINARY) 

y是您的輸出。 ret只是一些價值。現在忽略它。你可以閱讀更多關於threshold here.