OpenCV python (2) image preprocessing: change image size && extract region of interest

1. Change image size

1. Get the image width, height, and number of channels

The specific method is as follows:

print(img.shape[0])  # 图片高height
print(img.shape[1])  # 图片宽width
print(img.shape[2])  # 图片通道数

2. The resize function

opencv-python provides the resize function to change the overall image size, the function is as follows.

cv2.resize(图像 ,(w, h), fx=倍数, fy=倍数)	#改变图像shape

Among them, (w, h) can directly change the image size, w refers to the width of the image, and h refers to the height of the image .
fx and fy are multiplied to increase or decrease the width or height of the image.

The specific usage method is as follows:

cv2.resize(img, (w, h))		# 指定宽高改变图像大小
cv2.resize(img, (0, 0), fx=num1, fy=num2)		# 指定倍数改变图像大小
# num1、num2为倍数

Combined with the width and height of the obtained image, the size of the image can also be changed in multiples through w and h, as shown below:

cv2.resize(img, (int(img.shape[1]*num1), int(img.shape[0]*num2)))		# 指定宽高改变图像大小
# num1、num2为倍数

It should be noted that the width and height of the image must be an integer, so add int() to change the value to an integer to ensure that the type is correct, otherwise the following error will occur.
insert image description here

Can't parse 'dsize'. Sequence item with index 0 has a wrong type

3. Case

The resolution of the original image is 712 * 709, and the changed image is 300 * 300, and the width and height of each image are 1/2 of the original image. The specific procedure is as follows:

import cv2  # 导入opencv库

if __name__ == '__main__':
    while True:
        img = cv2.imread('img/1.jpg', 1)    # 获取路径img/0.jpg的图像,图像类型为RGB图像

        # 此处为改变图像大小
        img_wh = cv2.resize(img, (300, 300))  # 通过w、h改变图像大小
        img_fxy = cv2.resize(img, (0, 0), fx=0.5, fy=0.5)  # 通过fx、fy改变图像大小

        cv2.imshow("img", img)     # 显示原图
        cv2.imshow("img_wh", img_wh)  # 显示w、h改变后的图
        cv2.imshow("img_fxy", img_fxy)  # 显示fx、fy改变后的图
        cv2.waitKey(1)  # 等待时间

The effect is as follows:
insert image description here

2. ROI area of ​​interest

1. Definition of the region of interest

When we obtain a frame of image from a complex environment through a camera or other means, the image contains a lot of information that we are not interested in. If we process the entire image, it is often very cumbersome. If we do not have enough experience, we will It's easy to hard code. Therefore, if the scene and requirements permit, you can try to frame the region of interest before image processing, which can effectively reduce the complexity of the program and effectively reduce the time for subsequent image processing. At the same time, it can also be applied in the process of image processing and used as needed.

2. ROI extraction in opencv-python

The method to extract the ROI is as follows:

img = cv2.imread()[y1:y2,x1:x2]	#ROI区域

The coordinates of the upper left corner are (x1, y1), and the coordinates of the lower right corner are (x2, y2)

3. Case

Before extracting the ROI, the image size is set to 600 * 600, and then the region of interest is extracted: the coordinates of the upper left corner are (200, 0), and the coordinates of the lower right corner are (600, 400), that is, from 0 to 400 on the y-axis, and on the x- axis From 200 to 600 . The specific procedure is as follows:

import cv2  # 导入opencv库

if __name__ == '__main__':
    while True:
        img = cv2.imread('img/1.jpg', 1)    # 获取路径img/0.jpg的图像,图像类型为RGB图像

        img = cv2.resize(img, (600, 600))  # 通过w、h改变图像大小

        img_ROI = img[0:400, 200:600]   # 提取感兴趣区域

        cv2.imshow("img", img)  # 显示w、h改变后的图
        cv2.imshow("ROI", img_ROI)  # 显示感兴趣区域
        cv2.waitKey(1)  # 等待时间

The effect is as follows:
insert image description here

3. Combined application: one of the deep learning data enhancement methods

As shown in the above program, based on it, the image can be converted to the original size by resizing again. The specific program is as follows, this time the image is changed to 300 * 300 (the SSD model image input size is generally 300 * 300 ):

import cv2  # 导入opencv库

if __name__ == '__main__':
    while True:
        img = cv2.imread('img/1.jpg', 1)    # 获取路径img/0.jpg的图像,图像类型为RGB图像

        img = cv2.resize(img, (300, 300))  # 通过w、h改变图像大小

        img_ROI = img[0:200, 100:300]   # 提取感兴趣区域

        img_2 = cv2.resize(img_ROI, (img.shape[1], img.shape[0]))     # 改变为原本图像大小
        cv2.imshow("img", img)  # 显示w、h改变后的图
        cv2.imshow("ROI", img_ROI)  # 显示感兴趣区域
        cv2.imshow("img_2", img_2)  # 显示最终图像
        cv2.waitKey(1)  # 等待时间

The effect is as follows: Through the above operations, the data volume of the deep learning dataset can be increased, and image cropping (usually random cropping )
insert image description here
is performed during training to prevent overfitting in training .

I am a student and I am currently studying. This article can be regarded as my study notes. Please correct me if I am wrong.

Guess you like

Origin blog.csdn.net/xztli/article/details/126149578