Article directory
First, the basic operation of the image
1. Image IO operation
Here we will introduce how to read images, how to display images and how to save images.
1.1 Read image
API
cv.imread()
parameter:
-
image to read
-
read mode flag
- cv.IMREAD*COLOR: Loads the image in color mode, any image transparency will be ignored. This is the default parameter.
- cv.IMREAD*GRAYSCALE: load the image in grayscale mode
- cv.IMREAD_UNCHANGED: load image mode including alpha channel.
You can use 1, 0 or -1 to replace the above three flags
Reference Code
import numpy as np
import cv2 as cv
# 以灰度图的形式读取图像
img = cv.imread('messi5.jpg',0)
Note: If there is an error in the loaded path, no error will be reported, and a None value will be returned
1.2 Display image
API
cv.imshow()
parameter:
- The name of the window displaying the image, expressed as a string
- image to load
Note: After calling the API for displaying images, call cv.waitKey() to allow time for image drawing, otherwise the window will become unresponsive and the image cannot be displayed .
In addition, we can also use matplotlib to display images.
Reference Code
# opencv中显示
cv.imshow('image',img)
cv.waitKey(0)
# matplotlib中展示
plt.imshow(img[:,:,::-1])
1.3 Save image
API
cv.imwrite()
parameter:
- file name, where to save
- image to save
Reference Code
cv.imwrite('messigray.png',img)
1.4 Summary
We do this by loading a grayscale image, displaying the image, and saving the image if we press 's' and exit, or just exit without saving by pressing the ESC key.
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 读取图像
img = cv.imread('messi5.jpg',0)
# 2 显示图像
# 2.1 利用opencv展示图像
cv.imshow('image',img)
# 2.2 在matplotplotlib中展示图像
plt.imshow(img[:,:,::-1])
plt.title('匹配结果'), plt.xticks([]), plt.yticks([])
plt.show()
k = cv.waitKey(0)
# 3 保存图像
cv.imwrite('messigray.png',img)
2. Draw geometry
2.1 Draw a straight line
cv.line(img,start,end,color,thickness)
parameter:
- img: the image to draw the line
- Start,end: the start and end of the line
- color: the color of the line
- Thickness: line width
2.2 Draw a circle
cv.circle(img,centerpoint, r, color, thickness)
parameter:
- img: the image to draw the circle
- Centerpoint, r: center point and radius
- color: the color of the line
- Thickness: Line width, when it is -1, a closed pattern is generated and filled with color
2.3 Draw a rectangle
cv.rectangle(img,leftupper,rightdown,color,thickness)
parameter:
- img: the image to draw the rectangle on
- Leftupper, rightdown: coordinates of the upper left and lower right corners of the rectangle
- color: the color of the line
- Thickness: line width
2.4 Adding text to images
cv.putText(img,text,station, font, fontsize,color,thickness,cv.LINE_AA)
parameter:
- img: image
- text: the text data to write
- station: where the text is placed
- font: font
- Fontsize: font size
2.5 Effect display
We generate a completely black image, then draw the image inside and add text
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 创建一个空白的图像
img = np.zeros((512,512,3), np.uint8)
# 2 绘制图形
cv.line(img,(0,0),(511,511),(255,0,0),5)
cv.rectangle(img,(384,0),(510,128),(0,255,0),3)
cv.circle(img,(447,63), 63, (0,0,255), -1)
font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv.LINE_AA)
# 3 图像展示
plt.imshow(img[:,:,::-1])
plt.title('匹配结果'), plt.xticks([]), plt.yticks([])
plt.show()
result:
3. Obtain and modify the pixels in the image
We can obtain the pixel value of the pixel point through the coordinate values of the row and column. For BGR images, it returns an array of blue, green, red values. For grayscale images, only the corresponding intensity values are returned. Use the same method to modify the pixel value.
import numpy as np
import cv2 as cv
img = cv.imread('messi5.jpg')
# 获取某个像素点的值
px = img[100,100]
# 仅获取蓝色通道的强度值
blue = img[100,100,0]
# 修改某个位置的像素值
img[100,100] = [255,255,255]
4. Get the properties of the image
Image properties include number of rows, columns and channels, image data type, number of pixels, etc.
5. Splitting and merging of image channels
Sometimes it is necessary to work separately on B, G, R channel images. In this case, the BGR image needs to be segmented into individual channels. Or in other cases, it may be necessary to merge these individual channels into a BGR image. You can do it in the following way.
# 通道拆分
b,g,r = cv.split(img)
# 通道合并
img = cv.merge((b,g,r))
6. Change of color space
There are more than 150 color space conversion methods in OpenCV. There are two conversion methods most widely used, BGR↔Gray and BGR↔HSV.
API:
cv.cvtColor(input_image,flag)
parameter:
- input_image: image for color space conversion
- flag: conversion type
- cv.COLOR_BGR2GRAY : BGR↔Gray
- cv.COLOR_BGR2HSV: BGR→HSV
2. Arithmetic operations
1. Addition of images
You can add two images using OpenCV's cv.add() function, or you can simply add two images through numpy operations like res = img1 + img2. Both images should be of the same size and type, or the second image can be a scalar value.
Note: There is a difference between OpenCV addition and Numpy addition. OpenCV's addition is a saturating operation, while Numpy's addition is a modulo operation .
Refer to the following code:
>>> x = np.uint8([250])
>>> y = np.uint8([10])
>>> print( cv.add(x,y) ) # 250+10 = 260 => 255
[[255]]
>>> print( x+y ) # 250+10 = 260 % 256 = 4
[4]
This difference becomes more apparent when you add the two images. OpenCV results are a bit better. So we try to use the functions in OpenCV.
We will have the following two images:
code:
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 读取图像
img1 = cv.imread("view.jpg")
img2 = cv.imread("rain.jpg")
# 2 加法操作
img3 = cv.add(img1,img2) # cv中的加法
img4 = img1+img2 # 直接相加
# 3 图像显示
fig,axes=plt.subplots(nrows=1,ncols=2,figsize=(10,8),dpi=100)
axes[0].imshow(img3[:,:,::-1])
axes[0].set_title("cv中的加法")
axes[1].imshow(img4[:,:,::-1])
axes[1].set_title("直接相加")
plt.show()
The result looks like this:
2. Blending of images
This is actually addition, but the difference is that the weights of the two images are different, which will give people a feeling of blending or transparency. The calculation formula for image blending is as follows:
g ( x ) = ( 1 − α ) f 0 ( x ) + α f 1 ( x ) g(x) = (1−α)f0(x) + αf1(x)g(x)=(1−α)f0(x)+αf1(x)
By modifying the value of α (0 → 1), very cool blends can be achieved.
Now we blend the two images together. The weight of the first image is 0.7 and the weight of the second image is 0.3. The function cv2.addWeighted() can perform mixed operations on pictures according to the following formula.
dst = α ⋅ img 1 + β ⋅ img 2 + γ dst = α⋅img1 + β⋅img2 + γdst=a⋅img1+b⋅img2+c
Here γ is taken as zero.
Refer to the following code:
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 读取图像
img1 = cv.imread("view.jpg")
img2 = cv.imread("rain.jpg")
# 2 图像混合
img3 = cv.addWeighted(img1,0.7,img2,0.3,0)
# 3 图像显示
plt.figure(figsize=(8,8))
plt.imshow(img3[:,:,::-1])
plt.show()
The window will appear as shown below: