Basic Operations on Pixels

Accessing Pixel Values for Grayscale Images

Pixels are the building blocks of images. These intensity values are stored in a 2-D array, and these intensities are shown on the monitor for visualization and understanding by a human. These individual intensity values are called pixels in image processing terminology. From a programmer's perspective, the image is just a 2-D array. These values in the array can be accessed and manipulated easily.

In OpenCV-Python, the 2-D array is basically a Numpy array. So, working with images in OpenCV-Python is more like working with Numpy arrays. We can access and manipulate the pixel values in two ways. Let us start with the easy but slower way. The following lines of code read an image and access the pixel value at location (0,0).


1
2
3
4
5
6
7
import cv2
import numpy as np
path = r'F:\img\coins.jpg'
img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
value = img[0,0]
print(value)
value = img.item(0,0)
Output:49


Line 1 and 2: As usual, we start by importing the OpenCV and Numpy packages, respectively.
Line 3: Path of the image to read.
Line 4: Read the image. Notice the second parameter in the imread() function. This parameter indicates that we want to read a grayscale image (without color).
Line 5: This is where we read the pixel value from the image at location (0,0). First, we mention row and then column. Remember, it is an array, so in Python, it starts from 0. You can easily change these row and column values to read any pixel value within the image.
Line 6: We print the value of the pixel at (0,0). For our image, you can see the pixel value is 49.
The other recommended way to access the individual pixel values is to use the image object's item() method. This is faster and optimized. So, you can replace line 5 as:


                                    
value = img.item(0,0)


In this line, instead of using the array index, just we used the Numpy object's item() method and the parameter are just the same, i.e. row index followed by column index.

Modifying Pixel Values for Grayscale Images

After learning access to individual pixel values, modifying a pixel value is very easy. The following code shows how to modify the pixel value at location (0,0) and sets it to 100;

1
2
3
4
5
6
7
import cv2
import numpy as np
path = r'F:\img\coins.jpg'
img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
img[0,0] = 100
value = img.item(0,0)
print(value)
Output:100


Just look at line 5. The value of the pixel at location (0,0) is set to 100. Then again, we read the value from that location in line 6 and display it in line 7. The output we get is 100.

We can achieve the same goal using a better and recommended way to set the individual pixel values. We can use array.itemset() method to modify the pixel values, which is the counterpart of array.item() method.

1
2
3
4
5
6
7
import cv2
import numpy as np
path = r'F:\img\coins.jpg'
img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
img.itemset((0,0),100)
value = img.item(0,0)
print(value)
Output:100



Accessing Pixel Values for Color Images

The above discussion was limited to grayscale images. However, the idea can be extended to access and modify the color images. To remind you, a color image usually consists of three channels, red, green, and blue. Technically, we can say that instead of just a single 2-D array in grayscale images, now we have three 2-D arrays of the same size, one for each color channel. Let us make it more understandable by reading pixel values from a color image from the location (10,10):

1
2
3
4
5
6
7
# Color images
import cv2
import numpy as np
path = r'F:\img\lena.jpg'
img = cv2.imread(path,cv2.IMREAD_COLOR)
value = img[10,10,:]
print(value)
Output: [103 128 220]


Notice the changes in line 5 and line 6. In line 5, the second parameter is changed to read a color image now and in line 6 we provide three parameters inside the square brackets. As usually, first two are the row and column index; however, the third parameter is a colon which indicates that we want to read the pixel values at the particular location from all three channels. So you can see that the output is a list of three numbers.

Understanding the sequence of these numbers is important. Generally, when we talk about color images, we think of RGB colors, i.e., the sequence is like this first red, green, and then blue. However, in OpenCV, the sequence is BGR, so the first value (100) is the pixel value at location (10,10) from the blue channel, then 128 is pixel value from the same location from the green channel, and the last one is the pixel value from the red channel.

If we want to ready the pixel value from a particular channel, e.g. from blue channel, then we can modify line 6 as:


                                    
value = img[10,10,0]


Similarly, if we want to read the pixel value from location (10,10) from red channel, then we can read it as:


                                    
value = img[10,10,2]


Also, notice that the indexing of the channel also starts from 0. If we want to use the array.item() function, we can also read individual pixel values as:


                                    
value = img.item(10,10,0)



Modifying Pixel Values for Color Images

Modification of the pixel values for color image is also easy. For the direct access, we can provide three values at the same time instead of one value for BGR image as shown in the following code:

1
2
3
4
5
6
7
import cv2
import numpy as np
path = r'F:\img\lena.jpg'
img = cv2.imread(path,cv2.IMREAD_COLOR)
img[10,10,] = [255,255,255]
value = img[10,10,:]
print(value)
Output: [255 255 255]


Notice line 5, just assigned 255 for all the channels at location (10,10). If we want, we can assign just value to one channel at a time as shown below:

1
2
3
4
5
6
7
import cv2
import numpy as np
path = r'F:\img\lena.jpg'
img = cv2.imread(path,cv2.IMREAD_COLOR)
img[10,10,0] = 255
value = img[10,10,:]
print(value)
Output: [255 128 220]


Now notice that in line 6 above, only one value in the blue channel is changed to 255, and the rest are the same. We can also manipulate the individual pixel using array.itemset() method as below:

OpenCV Tutorials