In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "methods of Python Opencv data Enhancement". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Common data enhancement operations are: zoom in or out the picture proportionally, rotate, translate, flip horizontally, change the image channel, and so on.
1. Zoom in and out proportionally
Scaling only changes the size of the image. The function cv2.resize () provided by OpenCV does this. The size of the image can be set manually, or you can specify a scaling factor. You can choose to use different interpolation methods. We recommend using cv2.INTER_AREA when zooming and v2.INTER_CUBIC (slow) and v2.INTER_LINEAR when expanding. By default, the interpolation method used by all operations that resize the image is cv2.INTER_LINEAR.
# zoom out-both width and height are reduced to the original scale times def zoom_down (img,scale): img = cv2.resize (img,None,fx= scale,fy= scale,interpolation=cv2.INTER_CUBIC) return img# zoom in-both width and height are enlarged to the original scale times def zoom_up (img,scale): img = cv2.resize (img,None,fx= scale,fy= scale,interpolation=cv2.INTER_CUBIC) return img
The second parameter in the resize library is the target size. For example, if I want to resize the image to a size of 300 to 300, I can write:
Img = cv2.resize (img, 300300) 2. Pan image
Panning is changing the position of the object. If you want to move in the direction of (xQuery y) and the distance of movement is (tx,ty), you can build the movement matrix in the following ways:
You can use the Numpy array to build this matrix (the data type is np.float32) and pass it to the function cv2.warpAffine ().
Mat_translation = np.float32 ([[1,0,20], [0,1,30]])
For example, the matrix above moves the image 20 pixels horizontally and 30 pixels vertically.
Example:
# Translation-horizontal translation or vertical translation def translation (img,tx,ty): height = img.shape [0] width = img.shape [1] mat_translation = np.float32 ([[1,0, tx], [0,1, ty]]) # Transformation matrix: calculation matrix for setting translation transformation: 2 rows and 3 columns img = cv2.warpAffine (img, mat_translation, (width + tx) Height + ty) # transform function return img
The tx and ty encapsulated here are the number of pixels that need to be moved horizontally and vertically, respectively.
3. Rotate the image
OpenCV provides a function: cv2.getRotationMatrix2D
# rotation def rotation (img,angle,scale): rows = img.shape [0] cols = img.shape [1] # where the first parameter is the rotation center, the second is the rotation angle, and the third is the scale factor after rotation # you can set the rotation center and scale factor And the problem of window size to prevent the rotation from exceeding the boundary M = cv2.getRotationMatrix2D ((cols / 2, rows / 2), angle, scale) # rotate the angle degree to the left and scale it to the original scale times img = cv2.warpAffine (img, M, (cols, rows)) # the third parameter is the size center return img4 of the output image. Mirror image transformation
Opencv provides the cv2.flip () function, which can be flipped horizontally when the second argument is 1 or vertically when it is 0. For the convenience of later calls, I encapsulated it myself.
# Image transform def mirror (img,mode): img = cv2.flip (img,mode) # mode = 1 horizontal flip mode = 0 vertical flip return img5. Add salt and pepper noise
Salt and pepper noise is a pure black or white pixel, randomly generated.
# add salt and pepper noise def spiced_salt_noise (img,prob): output = np.zeros (img.shape,np.uint8) thres = 1-prob for i in range (img.shape [0]): for j in range (img.shape [1]): rdn = random.random () if rdn
< prob: output[i][j] = 0 # 椒盐噪声由纯黑和纯白的像素点随机组成 elif rdn >Thres: output [I] [j] = 255else: output [I] [j] = img [I] [j] return output6. Add Gaussian noise
Different from salt and pepper noise, Gaussian noise is colored, and the greater the variance, the greater the noise.
# add Gaussian noise def gasuss_noise (image, mean = 0, var = 0.01):''add Gaussian noise mean: mean var: variance, the greater the variance, the more blurred' 'image = np.array (image/255, dtype=float) noise = np.random.normal (mean, var * * 0.5, image.shape) out = image + noise if out.min ()
< 0: low_clip = -1. else: low_clip = 0. out = np.clip(out, low_clip, 1.0) out = np.uint8(out*255) return out7.模糊化 将图片模糊或平滑有多种算法,例如高斯模糊、中值模糊、均值模糊等,我这里使用一个比较普通的cv2.blur()实现。同样也是先封装方便我后面调用。 # 模糊def blur(img,scale): img = cv2.blur(img,(scale,scale)) # scale越大越模糊 return img 这里的scale其实就是滤波器的尺寸,一般取奇数,scale越大越模糊, 8.重新组合颜色通道 在opencv中,图像的通道顺序为BGR,也就是蓝绿红,可以改变成其他顺序以得到不同的效果。 # 重新组合颜色通道def change_channel(img): b = cv2.split(img)[0] g = cv2.split(img)[1] r = cv2.split(img)[2] brg = cv2.merge([b, r, g]) # 可以自己改变组合顺序 return brg实例 我有以下几张测试图片: 我希望随机地对这些图片进行一些变换,最终执行结果如下: 可以看到程序对我的图片随机进行了各种变换,我这里只是一次变换,读者也可以尝试对图片同时进行多种变换。 本次程序如下: #!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2022/2/18 16:30# @Author : 若谷# @File : Data_Augumentation.py# @Software: PyCharmimport numpy as npimport cv2import randomimport osimport sys# 缩小 -- 宽和高都缩小为原来的scale倍def zoom_down(img, scale): img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC) return img# 放大 -- 宽和高都放大为原来的scale倍def zoom_up(img, scale): img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC) return img# 平移 -- 水平平移或竖直方向平移def translation(img, tx, ty): height = img.shape[0] width = img.shape[1] mat_translation = np.float32([[1, 0, tx], [0, 1, ty]]) # 变换矩阵:设置平移变换所需的计算矩阵:2行3列 img = cv2.warpAffine(img, mat_translation, (width + tx, height + ty)) # 变换函数 return img# 旋转def rotation(img, angle, scale): rows = img.shape[0] cols = img.shape[1] # 这里的第一个参数为旋转中心,第二个为旋转角度,第三个为旋转后的缩放因子 # 可以通过设置旋转中心,缩放因子,以及窗口大小来防止旋转后超出边界的问题 M = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, scale) # 向左旋转angle度并缩放为原来的scale倍 img = cv2.warpAffine(img, M, (cols, rows)) # 第三个参数是输出图像的尺寸中心 return img# 镜像变换def mirror(img, mode): img = cv2.flip(img, mode) # mode = 1 水平翻转 mode = 0 垂直翻 return img# 添加椒盐噪声def spiced_salt_noise(img, prob): output = np.zeros(img.shape, np.uint8) thres = 1 - prob for i in range(img.shape[0]): for j in range(img.shape[1]): rdn = random.random() if rdn < prob: output[i][j] = 0 # 椒盐噪声由纯黑和纯白的像素点随机组成 elif rdn >Thres: output [I] [j] = 255else: output [I] [j] = img [I] [j] return output# Fuzzy def blur (img, scale): img = cv2.blur (img, (scale, scale)) # the larger the scale, the more fuzzy return img# adds Gaussian noise def gasuss_noise (image, mean=0) Var=0.01):''add Gaussian noise mean: mean var: variance The greater the variance, the more blurred''image = np.array (image / 255, dtype=float) noise = np.random.normal (mean, var * * 0.5, image.shape) out = image + noise if out.min () < 0: low_clip =-1. Else: low_clip = 0. Out = np.clip (out, low_clip, 1.0) out = np.uint8 (out * 255) return out# reassemble the color channel def change_channel (img): B = cv2.split (img) [0] g = cv2.split (img) [1] r = cv2.split (img) [2] brg = cv2.merge ([b, r) G]) # you can change the combination order return brg# to do the above operations randomly def Data_Augument (): for i in images_list: img = cv2.imread (image_dir+i) # picture path + picture name cv2.imshow ('img',img) functions = [(' zoom_down', [img, 0.8]), # the first parameter is the function name This is followed by the parameters of the function call ('zoom_up', [img, 1.2]), (' translation', [img, 20,30]), ('rotation', [img, 15,0.9]), (' mirror', [img, 1]) ('spiced_salt_noise', [img, 0.01]), (' blur', [img, 5]), ('gasuss_noise', [img, 0,0.01]), (' change_channel' [img])] choice = random.choice (functions) # randomly select a function to execute this_module = sys. Modules [_ _ name__] # current file res = getattr (this_module, choice [0]) (* choice [1]) cv2.imwrite (output_dir + I) Res) if _ _ name__ ='_ _ main__': image_dir ='. / test/' # Source picture path images_list = os.listdir (image_dir) nums = len (os.listdir (image_dir)) print ('found% d pictures'% nums) output_dir ='. / output/' # the saved path after image transformation Data_Augument () # executes print This is the end of the introduction of "Python Opencv data Enhancement methods". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.