title: How do I access the pixels of an image using OpenCV-Python?
body: <p>I want to know how to loop through all pixels of an image. I tried this:</p>
  
  <pre><code>import cv2
  import numpy as np
  
  x = np.random.randint(0,5,(500,500))
  img = cv2.imread('D:\Project\Capture1.jpg',0)
  p = img.shape
  print p
  rows,cols = img.shape
  
  for i in range(rows):
      for j in range(cols):
          k = x[i,j]
          print k
  </code></pre>
  
  <p>It prints a vertical set of numbers which is not in the form of an array. I am also getting an array out of bounds exception. Please suggest a method.</p>