Using Python, OpenCV captures and changes pixels, modifs image channels, and trims ROI

This blog will introduce the use of Python, OpenCV to get, change pixels, modify image channels, capture image interest ROI; Single-channel diagram, BGR three-channel diagram, four-channel transparent diagram, opaque diagram;

1. The rendering

In the original VS, change the lower right pixel to red and the upper left quarter to green. The result is as follows: Cut the area of interest: cut the upper left corner, upper right corner, lower left corner and lower right corner respectively, each accounting for 1/4; The renderings are as follows: The original VS image single channel grayscale effect is as follows: The upper left original picture VS the upper right R channel picture VS the lower left G channel picture VS the lower right B channel picture is as follows: Image 4 channel full transparent VS opaque renderings:

2. The source code

# USAGE
# python opencv_getting_setting.py --image fjdj.png

Import the necessary packages
import argparse

import cv2
import imutils
import numpy as np

Build command line arguments and parse
The default name is fjdj.jpg, the parent directory of the current py file


ap = argparse.ArgumentParser()
ap.add_argument("-i"."--image".type=str, default="fjdj.jpg".help="path to the input image")
args = vars(ap.parse_args())
ap = argparse.ArgumentParser()

Load the image, get the spatial dimension (width, height), show the original image to the screen
image = cv2.imread(args["image"])
image = imutils.resize(image, width=430)
origin = image.copy()
(h, w) = image.shape[:2]
cv2.imshow("Original", image)

The image exists as a Numpy array, gets the upper left corner, and the image index starts at 0
# The image is represented as a BGR channel, since BGR was standard at the beginning and later adjusted to RGB
(b, g, r) = image[0.0]
print("Pixel at (0, 0) - Red: {}, Green: {}, Blue: {}".format(r, g, b))

Get the pixel values of x=380 and y=380. Imagine the image as an M*N matrix, with M as rows and N as columns
(b, g, r) = image[380.380]
print("Pixel at (380, 380) - Red: {}, Green: {}, Blue: {}".format(r, g, b))

# update x=50, y=20 pixels to red
image[380.380] = (0.0.255)
(b, g, r) = image[380.380]
print("Pixel at (380, 380) - Red: {}, Green: {}, Blue: {}".format(r, g, b))

# Compute the center of the image
(cX, cY) = (w // 2, h // 2)

Use the array slice to get the upper-left quarter
tl = image[0:cY, 0:cX]
cv2.imshow("Top-Left Corner", tl)

Similarly, crop the upper right corner, lower left corner, and lower right corner with array slices and show them
tr = image[0:cY, cX:w]
br = image[cY:h, cX:w]
bl = image[cY:h, 0:cX]
cv2.imshow("Top-Right Corner", tr)
cv2.imshow("Bottom-Right Corner", br)
cv2.imshow("Bottom-Left Corner", bl)

# Use pixel slices to change the color of the pixel region
image[0:cY, 0:cX] = (0.255.0)

# Show updated pixel images
cv2.imshow("Updated (Top-Left Corner to Green)", image)

gray = cv2.cvtColor(origin, cv2.COLOR_BGR2GRAY)
cv2.imshow("Gray", gray)

(h, w) = origin.shape[:2]
zeros = np.zeros((h, w), dtype="uint8")
# Split Origin into red, green, and blue channels, and then we use Numpy zero sets to construct the representation for each channel separately
(B, G, R) = cv2.split(origin)
R = cv2.merge([zeros, zeros, R])
G = cv2.merge([zeros, G, zeros])
B = cv2.merge([B, zeros, zeros])
cv2.imshow("B G R", np.hstack([B, G, R]))
The original image of the output frame is in the upper left corner of the red channel, upper right corner of the green channel, lower right corner of the blue channel, lower left corner
output = np.zeros((h * 2, w * 2.3), dtype="uint8")
output[0:h, 0:w] = origin
output[0:h, w:w * 2] = R
output[h:h * 2.0:w] = G
output[h:h * 2, w:w * 2] = B
cv2.imshow("origin vs R vs G vs B", imutils.resize(output, width=700))

alpha0 = np.dstack([origin, np.ones((h, w), dtype="uint8") * 0])
cv2.imshow("alph 0", alpha0)
cv2.imwrite("alph 0.png", alpha0)

alpha1 = np.dstack([origin, np.ones((h, w), dtype="uint8") * 255])
cv2.imshow("alph 255", alpha1)
cv2.imwrite("alph 255.png", alpha1)
cv2.waitKey(0)
Copy the code

reference

  • www.pyimagesearch.com/2021/01/20/…