Previous portal:
“Python Image Processing OpenCV (1) : Getting Started”
Normal operations
1. Read pixels
Reading pixels can be accessed by row coordinates and column coordinates. Gray images directly return gray values, while color images return B, G and R components.
It is important to note that OpenCV reads images as BGR storage displays.
Gray image reading operation:
import cv2 as cv
# Grayscale image reading
gray_img = cv.imread("maliao.jpg", cv.IMREAD_GRAYSCALE)
print(gray_img[20.30])
# display images
cv.imshow("gray_img", gray_img)
# wait for input
cv.waitKey()
cv.destroyAllWindows()
Copy the code
For reading pixel values of grayscale images, only the corresponding grayscale is returned.
Color image reading operation:
import cv2 as cv
# Color image reading
color_img = cv.imread("maliao.jpg", cv.IMREAD_COLOR)
print(color_img[20.30])
blue = color_img[20.30.0]
print(blue)
green = color_img[20.30.1]
print(green)
red = color_img[20.30.2]
print(red)
# display images
cv.imshow("color_img", color_img)
# wait for input
cv.waitKey()
cv.destroyAllWindows()
Print the result
[ 3 2 236]
3
2
236
Copy the code
It is important to note when get pixel color image of the meaning of the second parameter 1 | 2 | 3 is to obtain BGR three channels of pixels.
2. Modify the pixels
When modifying a pixel, assign a new pixel directly to the pixel.
If it is a grayscale image, directly assign the value.
If it is a color image, the pixels of the three channels of BGR need to be assigned successively.
import cv2 as cv
# Grayscale image reading
gray_img = cv.imread("maliao.jpg", cv.IMREAD_GRAYSCALE)
print(gray_img[20.30])
# Pixel assignment
gray_img[20.30] = 255
print(gray_img[20.30])
Print the result
72
255
# Color image reading
color_img = cv.imread("maliao.jpg", cv.IMREAD_COLOR)
print(color_img[20.30])
# Pixels are assigned in sequence
color_img[20.30.0] = 255
color_img[20.30.1] = 255
color_img[20.30.2] = 255
print(color_img[20.30])
Print the result
[ 3 2 236]
[255 255 255]
Copy the code
If it is too much trouble to assign to the three BGR channels in sequence, you can also assign to the pixels directly from the array:
# One pixel assignment
color_img[20.30] = [0.0.0]
print(color_img[20.30])
Print the result
[0 0 0]
Copy the code
The following is an assignment to a region of pixels, all of which are assigned white:
import cv2 as cv
color_img = cv.imread("maliao.jpg", cv.IMREAD_COLOR)
color_img[50:100.50:100] = [255.255.255]
cv.imshow("color_img", color_img)
cv.waitKey()
cv.destroyAllWindows()
Copy the code
Use Numpy to operate
1. Read pixels
Use Numpy to read pixels as follows:
Return value = image.item (position argument)Copy the code
Gray image and color image are read as follows:
import cv2 as cv
# Read grayscale image
gray_img = cv.imread("maliao.jpg", cv.IMREAD_GRAYSCALE)
print(gray_img.item(20.30))
Print the result
72
# Read color image
color_img = cv.imread("maliao.jpg", cv.IMREAD_COLOR)
blue = color_img.item(20.30.0)
print(blue)
green = color_img.item(20.30.1)
print(green)
red = color_img.item(20.30.2)
print(red)
Print the result
3
2
236
Copy the code
2. Modify the pixels
To modify a pixel, use Numpy’s Itemset () method as follows:
Image. Itemset (Position, new value)Copy the code
Here is an example where I changed [20, 30] to white:
import cv2 as cv
# Read color image
color_img = cv.imread("maliao.jpg", cv.IMREAD_COLOR)
print(color_img[20.30])
color_img.itemset((20.30.0), 255)
color_img.itemset((20.30.1), 255)
color_img.itemset((20.30.2), 255)
print(color_img[20.30])
# output result
[ 3 2 236]
[255 255 255]
Copy the code
Note: Normal operations are usually used to select regions of an array, such as the square [50:100, 50:100] selected in the example above. For single pixel access, the Numpy array methods array.item() and array.itemset() are considered better.
Matplotlib displays the image
We can read the image through OpenCV and use Matplotlib to display the image.
import cv2 as cv
from matplotlib import pyplot as plt
img=cv.imread('maliao.jpg', cv.IMREAD_COLOR)
plt.imshow(img)
plt.show()
Copy the code
If we used Matplotlib directly to display the image OpenCV read in, we would get the following blue Mario:
This is because for OpenCV pixels are in BGR order, whereas Matplotlib follows RGB order.
There are a number of possible solutions (not counting circular pixels, this is silly), as follows:
import cv2 as cv
from matplotlib import pyplot as plt
img=cv.imread('maliao.jpg',cv.IMREAD_COLOR)
# method1
b,g,r=cv.split(img)
img2=cv.merge([r,g,b])
plt.imshow(img2)
plt.show()
# method2
img3=img[:,:,::- 1]
plt.imshow(img3)
plt.show()
# method3
img4=cv.cvtColor(img, cv.COLOR_BGR2RGB)
plt.imshow(img4)
plt.show()
Copy the code
As a result I will not post, these three methods can complete BGR to RGB conversion.
The sample code
If you need to get the source code, you can reply “OpenCV” on the public account to get it.
reference
Blog.csdn.net/eastmount/a…
woshicver.com/