Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to access and modify pixel value in an image using OpenCV Python?
In OpenCV, you can access and modify pixel values in images using NumPy-style indexing. Since OpenCV images are stored as NumPy arrays, you can use array indexing to read pixel values and assignment operations to modify them.
Understanding Image Structure
OpenCV loads color images in BGR (Blue, Green, Red) format. Each pixel contains three values representing the intensity of each color channel (0-255).
Accessing a Single Pixel Value
To access a pixel at coordinates (row, column), use array indexing ?
import cv2
import numpy as np
# Create a sample image for demonstration
img = np.zeros((400, 400, 3), dtype=np.uint8)
img[100:300, 100:300] = (100, 150, 200) # Add a colored rectangle
# Access pixel value at position [200, 150]
pixel_value = img[200, 150]
print("Pixel value at [200,150]:", pixel_value)
# Access individual color channels
blue_channel = img[200, 150][0]
green_channel = img[200, 150][1]
red_channel = img[200, 150][2]
print("Blue channel:", blue_channel)
print("Green channel:", green_channel)
print("Red channel:", red_channel)
Pixel value at [200,150]: [100 150 200] Blue channel: 100 Green channel: 150 Red channel: 200
Modifying a Single Pixel
Use assignment to modify pixel values. The format is (Blue, Green, Red) ?
import cv2
import numpy as np
# Create a sample image
img = np.zeros((400, 400, 3), dtype=np.uint8)
img[100:300, 100:300] = (100, 150, 200)
print("Original pixel value at [200,150]:", img[200, 150])
# Modify pixel to red color (BGR format: 0, 0, 255)
img[200, 150] = (0, 0, 255)
print("Modified pixel value at [200,150]:", img[200, 150])
Original pixel value at [200,150]: [100 150 200] Modified pixel value at [200,150]: [ 0 0 255]
Accessing and Modifying Regions
Use slicing to work with rectangular regions of the image ?
import cv2
import numpy as np
# Create a sample image with gradient colors
img = np.zeros((400, 400, 3), dtype=np.uint8)
for i in range(400):
for j in range(400):
img[i, j] = (i % 256, j % 256, (i + j) % 256)
print("Original region shape:", img[100:200, 150:250].shape)
# Modify a rectangular region to blue color
img[100:200, 150:250] = (255, 0, 0) # Blue in BGR format
print("Region modified to blue color")
print("Sample pixel from modified region:", img[150, 200])
Original region shape: (100, 100, 3) Region modified to blue color Sample pixel from modified region: [255 0 0]
Practical Example with Image Display
Here's a complete example that creates an image and demonstrates pixel manipulation ?
import cv2
import numpy as np
# Create a sample image (since we can't load external files)
img = np.zeros((400, 600, 3), dtype=np.uint8)
# Create some patterns
img[50:150, 50:150] = (255, 100, 100) # Light blue square
img[200:300, 200:300] = (100, 255, 100) # Light green square
img[50:150, 400:500] = (100, 100, 255) # Light red square
# Access and print pixel values
print("Pixel at [100, 100]:", img[100, 100])
print("Pixel at [250, 250]:", img[250, 250])
# Modify individual pixels
img[100, 100] = (0, 255, 255) # Yellow pixel
img[250, 250] = (255, 255, 0) # Cyan pixel
# Modify a region to create a white rectangle
img[300:350, 100:500] = (255, 255, 255)
print("Modified pixel at [100, 100]:", img[100, 100])
print("Modified pixel at [250, 250]:", img[250, 250])
# Display the image (uncomment when running locally)
# cv2.imshow('Modified Image', img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
Key Points
| Operation | Syntax | Description |
|---|---|---|
| Access single pixel | img[row, col] |
Returns BGR array [B, G, R] |
| Access color channel | img[row, col][channel] |
0=Blue, 1=Green, 2=Red |
| Modify single pixel | img[row, col] = (B, G, R) |
Assigns new BGR values |
| Modify region | img[r1:r2, c1:c2] = (B, G, R) |
Sets all pixels in region |
Conclusion
OpenCV pixel manipulation uses NumPy indexing with BGR color format. Use img[row, col] for access and assignment for modification. Remember that OpenCV uses BGR order, not RGB.
