How to Access and Modify Pixel Value in an Image Using OpenCV Python
OpenCV is an open-source library that provides tools for image processing in Python. It can be used to read, write, and modify digital images. In this article, we will learn how to access and modify the pixel values of an image using OpenCV in Python.
Understanding Pixel Values
Before diving into the code, it is essential to understand what pixel values are. In an image, a pixel represents a single point on the image grid. Every pixel has a value, which is determined by its brightness and color. In grayscale images, the pixel value is a single value representing the intensity between 0 and 255, where 0 represents black, and 255 represents white. In color images, the pixel value consists of three values representing the intensity of red, green, and blue (RGB) channels. The combination of these three channels creates the color of the pixel.
Reading and Displaying an Image
To access and modify the pixel values of an image, we first need to read and display the image. To do this, we can use the cv2.imread()
and cv2.imshow()
functions, respectively. The following code reads and displays an image:
import cv2
# Read image
img = cv2.imread('image.jpg')
# Display image
cv2.imshow('Image', img)
cv2.waitKey(0)
In this code, the cv2.imread()
function reads the image named “image.jpg,” which is stored in the same directory as the script. After reading the image, we display it using the cv2.imshow()
function. The first argument of this function is the window name, which can be any string. The second argument is the image we want to display. Finally, we wait for a key to be pressed using the cv2.waitKey()
function. When any key is pressed, the window is destroyed.
Accessing Pixel Values
Now that we have displayed the image, we can access its pixel values. We can access the pixel values of an image using its coordinates. Every pixel in the image has an x-coordinate and a y-coordinate, which represents its position on the image grid. The pixel values can be accessed using the img[x,y]
notation, where x
and y
are the pixel coordinates. The following code accesses and prints the pixel value of a single pixel in the image:
import cv2
# Read image
img = cv2.imread('image.jpg')
# Access pixel value
pixel_value = img[100, 100]
# Print pixel value
print(pixel_value)
In this code, we first read the image using the cv2.imread()
function. Then, we access the pixel value of the pixel with coordinates (100, 100) using the img[100, 100]
notation. Finally, we print the pixel value to the console.
Modifying Pixel Values
Now that we know how to access pixel values, we can modify them to change the image. We can change the value of a pixel using the img[x,y] = value
notation, where value
is the new pixel value. The following code modifies the pixel value of a single pixel in the image:
import cv2
# Read image
img = cv2.imread('image.jpg')
# Modify pixel value
img[100, 100] = [255, 255, 255]
# Display modified image
cv2.imshow('Image', img)
cv2.waitKey(0)
In this code, we first read the image using the cv2.imread()
function. Then, we modify the pixel value of the pixel with coordinates (100, 100) to white using the img[100, 100] = [255, 255, 255]
notation. Finally, we display the modified image using the cv2.imshow()
function.
We can also modify the values of a range of pixels using slicing. Slicing allows us to access a range of pixels in an image using their coordinates. The following code modifies the value of a rectangular region of the image:
import cv2
# Read image
img = cv2.imread('image.jpg')
# Modify region of pixels
img[100:150, 100:150] = [255, 255, 255]
# Display modified image
cv2.imshow('Image', img)
cv2.waitKey(0)
In this code, we modify the values of the rectangular region of the image with the top-left corner at (100, 100) and the bottom-right corner at (150, 150). In this case, the values of all the pixels in this region are set to white, represented by the [255, 255, 255]array.
We can also use loops to modify the pixel values of an entire image. The following code modifies each pixel in the image by reducing the intensity of the red channel:
import cv2
# Read image
img = cv2.imread('image.jpg')
# Get image shape
height, width, channels = img.shape
# Loop over all pixels
for y in range(height):
for x in range(width):
# Reduce red channel
img[y, x, 2] /= 2
# Display modified image
cv2.imshow('Image', img)
cv2.waitKey(0)
In this code, we first read the image using the cv2.imread()
function. Then, we get the shape of the image using the img.shape
attribute. The height
, width
, and channels
variables hold the height, width, and number of channels of the image, respectively. We then use two loops to iterate over all pixels in the image. For each pixel, we reduce the intensity of the red channel by dividing its value by 2. Finally, we display the modified image using the cv2.imshow()
function.
Conclusion
OpenCV provides a powerful set of tools for accessing and modifying pixel values in digital images. In this article, we learned how to read and display an image using OpenCV in Python, as well as how to access and modify its pixel values using coordinates and slicing. We also covered how to use loops to modify the pixel values of an entire image. With this knowledge, you can now start exploring the vast world of image processing and computer vision using OpenCV in Python.