2014-01-22 116 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.