AppDividend
Latest Code Tutorials

How to Access Pixel Data in Image using Python OpenCV

0

OpenCV-Python is the Python library designed to solve computer vision problems. We can read the Image using the imread() function, write or save the Image using the imwrite() function, display the image in the image window using the imshow() method.

How to Access Pixel Data in Image

To access pixel data in Image, use numpy and opencv-python library. Import numpy and cv2(opencv-python) module inside your Python program file. Then read the image file using the imread() function. The imread() Method takes two parameters.

  1. Image path
  2. Channel(If 1 then black and white and if 2 then color)

Let’s print the Image.

# app.py

import numpy as np
import cv2

img = cv2.imread('forest.jpg', 1)

print(img)

Output

[[[231 204 178]
  [226 199 173]
  [227 200 174]
  ...
  [218 188 153]
  [218 188 153]
  [219 189 154]]

 [[227 200 174]
  [224 197 171]
  [228 201 175]
  ...
  [216 186 151]
  [214 184 149]
  [212 182 147]]

 [[224 197 171]
  [223 196 170]
  [228 201 175]
  ...
  [215 185 150]
  [212 182 147]
  [209 179 144]]

 ...

 [[ 42  94 131]
  [ 52 105 142]
  [ 84 144 180]
  ...
  [ 51  55  56]
  [ 53  50  52]
  [ 47  42  44]]

 [[ 61  96 139]
  [ 44  84 126]
  [ 64 113 153]
  ...
  [ 37  42  43]
  [ 39  36  38]
  [ 41  34  37]]

 [[ 29  56 100]
  [ 33  67 110]
  [ 45  87 130]
  ...
  [ 34  39  40]
  [ 38  35  37]
  [ 48  41  44]]]

You can see that it prints the array. Let’s see the type of the array.

import numpy as np
import cv2

img = cv2.imread('forest.jpg', 1)

print(type(img))

Output

<class 'numpy.ndarray'>

You can see that it is a numpy array. We have already imported the numpy library because of this reason. OpenCV-Python Image considers an image as a numpy array. So we can use all the numpy array functions to access the image pixel and data, and we can modify the data as well.

Find Length of Image using len() Method

To find the image length, use the len() Method and pass the Image.

import numpy as np
import cv2

img = cv2.imread('forest.jpg', 1)

print(len(img))

Output

6000

The image length of my local Image is 6000 px. It indicates the number of rows in the Image.

If you want to find the top rows, then you can do this.

import numpy as np
import cv2

img = cv2.imread('forest.jpg', 1)

print(len(img[0]))

Output

4000

That means the Image has 4000 vertical columns.

To find the number of channels of the Image, use this technique.

import numpy as np
import cv2

img = cv2.imread('forest.jpg', 1)

print(len(img[0][0]))

Output

3

Find the shape of the Image using shape property

To find the shape of an image, use shape property. The shape property returns the tuple contains three things.

  1. Width
  2. Height
  3. Channel
import numpy as np
import cv2

img = cv2.imread('forest.jpg', 1)

print(img.shape)

Output

(6000, 4000, 3)

Here, you can see that number of rows is 6000, columns are 4000, and the channel is 3.

Find the data type of the Image

To find a data type of the Image, use the dtype property on the Image.

import numpy as np
import cv2

img = cv2.imread('forest.jpg', 1)

print(img.dtype)

Output

uint8

The data type of pixel array is an unsigned integer value 8. What this tells us is that the maximum value of any image pixel is 255. If you are processing the Image using OpenCV, then you have to understand the maximum value of the Image. It can vary Image to Image.

Access pixels of the Image using numpy array

To access any pixels of the Image, use slice notation in img array.

import numpy as np
import cv2

img = cv2.imread('forest.jpg', 1)

print(img[11, 21])

Output

[226 201 175]

We are accessing the 11th row and 21st column, and in return, we get the array whose pixel values are 226, 201, and 175 at that array.

To find the total number of pixels of the Image, use the size property of the numpy array.

import numpy as np
import cv2

img = cv2.imread('forest.jpg', 1)

print(img.size)

Output

72000000

That means our Image has a total of 72,000,000 pixels.

That is it for covering the basics of an Image pixel, data, size, length using OpenCV-Python, and Numpy.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.