How do I access image pixels using OpenCV-Python?

I want to know how to iterate over all the pixels of an image. I tried this:

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 

It prints a vertical set of numbers that does not have the shape of an array. I also get an array outside of the exception. Please suggest a method.

+11
python image-processing opencv pixels
source share
10 answers

Access to a specific pixel in Python

 import cv2 image = cv2.imread("sample.jpg") pixel= image[200, 550] print pixel 

: [73 89 102]

+10
source share

I don't understand what the purpose of your variable x is. You do not need it.

Just use:

 for i in range(rows): for j in range(cols): k = img[i,j] print k 

which will print a truly vertical set of numbers. If you want to change the pixel values, use img.itemset() . http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_core/py_basic_ops/py_basic_ops.html

If you want to print the whole array, use print(img)

+8
source share

Access using an array index will be slow with an empty array.

You can use the item() method to access and itemset to make changes.

for example

 for i in range(0,img.shape[0]): for j in range(0,img.shape[1]): pixel = img.item(i, j) print pixel 
+3
source share

Try the following:

 import numpy as np import Image image = Image.open("example.png") image_data = np.asarray(image) for i in range(len(image_data)): for j in range(len(image_data[0])): print(image_data[i][j]) # this row prints an array of RGB color for each pixel in the image 
+2
source share
  import cv2 import numpy as np image = cv2.imread('C:/Users/Asus/Desktop/test.jpg', cv2.IMREAD_COLOR) for x in range (1,480): for y in range (1,640): pixel = image[x,y] print pixel 
+1
source share

You can do it

  for (int y = 0; y<im.rows; y++) { for (int x = 0; x<im.cols; x++) { Vec3b color = im.at<Vec3b>(Point(x, y)); //you can print color this has the pixel value } } 
+1
source share

The vertical array is the RGB (Reg, Green, Blue) channel values ​​for the image. If you need one value for a pixel, you can first convert the image to grayscale. It really depends on your application and what you want to do with the image, converting to grayscale is just one approach.

To convert to grayscale

 grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 

Some basic operations are shown in the documentation.

0
source share

you are reading the grayscale image

img = cv2.imread ('D: \ Project \ Capture1.jpg', 0)

here you get only intencity

0
source share

This code will give you the pixel values ​​in the 'k' array after going through the loop.

 import cv2 import numpy as np img = cv2.imread('sample.jpg',0) rows,cols = img.shape k = [] for i in range(rows): for j in range(cols): k.append(img[i,j]) print k 
0
source share
 import cv2 import numpy as np imagename = "capure.jpg" img = cv2.imread(imagename, 0) # 0 params, for gray image height, width = img.shape[:2] # image height and width print(img) # all image pixels value in array print(img[10, 10]) # one pixel value in 10,10 coordinate for y in range(height): for x in range(width): print(img[x], img[y], end = "\t") 
0
source share

All Articles