当前位置:网站首页>Image data enhancement (translation, rotation, brightness transformation, flipping, adding Gaussian noise, scaling, cropping)
Image data enhancement (translation, rotation, brightness transformation, flipping, adding Gaussian noise, scaling, cropping)
2022-06-11 07:46:00 【Keep_ Trying_ Go】
1. translation :
import cv2
import tensorflow as tf
import numpy as np
from PIL import Image
from skimage import transform,data
import matplotlib.pyplot as plt
def Move(image):
fig,ax=plt.subplots(nrows=1,ncols=3,figsize=(12,6))
ax[0].imshow(image)
ax[0].set_title('Original Image',fontsize=14)
width,height=image.shape[0],image.shape[1]
M=np.float32([[1,0,100],[0,1,50]])
image1=cv2.warpAffine(image,M,(height,width))
ax[1].imshow(image1)
ax[1].set_title('Move Image',fontsize=14)
M=np.float32([[1,0,50],[0,1,100]])
image2=cv2.warpAffine(image,M,(height,width))
ax[2].imshow(image2)
ax[2].set_title('Move Image',fontsize=14)
plt.show()
image='D:\\1.jpg'
img=cv2.imread(image)
Move(img)
Experimental results :
Reference link :https://blog.csdn.net/imxlw00/article/details/112767623
2. rotate :
import cv2
import matplotlib.pyplot as plt
Mode one :
def Rotate(image):
fig,ax=plt.subplots(nrows=1,ncols=3,figsize=(12,6))
ax[0].imshow(image)
ax[0].set_title('Original Image',fontsize=14)
# width,height=image.shape[0],image.shape[1]
image=cv2.rotate(image,cv2.cv2.ROTATE_90_CLOCKWISE)
ax[1].imshow(image)
ax[1].set_title('Rotate 90 Image',fontsize=14)
image=cv2.rotate(image,cv2.ROTATE_180)
ax[2].imshow(image)
ax[2].set_title('Rotate 180 Image',fontsize=14)
plt.show()
image='D:\\1.jpg'
img=cv2.imread(image)
Rotate(img)
Experimental results :

Mode two :
def Rotate(image):
fig,ax=plt.subplots(nrows=1,ncols=3,figsize=(12,6))
ax[0].imshow(image)
ax[0].set_title('Original Image',fontsize=14)
width,height=image.shape[0],image.shape[1]
# The first parameter is the center of rotation , The second parameter is rotation angle , The third parameter : Zoom ratio
M=cv2.getRotationMatrix2D((width/2,height/2),45,1)
image=cv2.warpAffine(image,M,(width,height))
ax[1].imshow(image)
ax[1].set_title('Rotate 45 Image',fontsize=14)
M=cv2.getRotationMatrix2D((width/2,height/2),90,1)
image=cv2.warpAffine(image,M,(width,height))
ax[2].imshow(image)
ax[2].set_title('Rotate 180 Image',fontsize=14)
plt.show()
image='D:\\1.jpg'
img=cv2.imread(image)
Rotate(img)
Experimental results :
Mode three :
import imutils
def Rotate(image):
fig,ax=plt.subplots(nrows=1,ncols=3,figsize=(12,6))
ax[0].imshow(image)
ax[0].set_title('Original Image',fontsize=14)
# The first parameter is zero image, The second parameter is the number of degrees of rotation
image=imutils.rotate(image,90)
ax[1].imshow(image)
ax[1].set_title('Rotate 90 Image',fontsize=14)
image=imutils.rotate(image,180)
ax[2].imshow(image)
ax[2].set_title('Rotate 180 Image',fontsize=14)
plt.show()
image='D:\\1.jpg'
img=cv2.imread(image)
Rotate(img)
Experimental results :
Mode 4 :
from skimage import transform,data
def Rotate(image):
fig,ax=plt.subplots(nrows=1,ncols=3,figsize=(12,6))
ax[0].imshow(image)
ax[0].set_title('Original Image',fontsize=14)
# The first parameter is zero image, The second parameter is the number of degrees of rotation , The third parameter is whether to change the size
image=transform.rotate(image,90,resize=False)
ax[1].imshow(image)
ax[1].set_title('Rotate 90 Image',fontsize=14)
image=transform.rotate(image,180,resize=True)
ax[2].imshow(image)
ax[2].set_title('Rotate 180 Image',fontsize=14)
plt.show()
image='D:\\1.jpg'
img=cv2.imread(image)
Rotate(img)
Experimental results :
References and citations :
https://blog.csdn.net/weixin_39862871/article/details/116164648
https://blog.csdn.net/qq_37674858/article/details/80708393
https://blog.csdn.net/denny2015/article/details/50532971
3. Brightness conversion :
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import cv2
from skimage import transform,data,exposure
from PIL import Image
def Chnage_Light(image):
fig,ax=plt.subplots(nrows=1,ncols=3,figsize=(12,6))
ax[0].imshow(image)
ax[0].set_title('Original Image',fontsize=14)
image1=exposure.adjust_gamma(image,1.5)
ax[1].imshow(image1)
ax[1].set_title('Change light Image',fontsize=14)
image2=exposure.adjust_gamma(image,0.8)
ax[2].imshow(image2)
ax[2].set_title('Change light Image',fontsize=14)
plt.show()
image='D:\\3.jpg'
img=cv2.imread(image)
Chnage_Light(img)
Experimental results :
References and citations :
https://blog.csdn.net/qq_28891989/article/details/95592462
4. Flip :
import cv2
import matplotlib.pyplot as plt
def invert(image):
fig,ax=plt.subplots(nrows=1,ncols=3,figsize=(12,6))
ax[0].imshow(image)
ax[0].set_title('Original Image',fontsize=14)
image=cv2.flip(image,0)
ax[1].imshow(image)
ax[1].set_title('Vertical Image',fontsize=14)
image=cv2.flip(image,1)
ax[2].imshow(image)
ax[2].set_title('Horizontal Image',fontsize=14)
plt.show()
image='D:\\1.jpg'
img=cv2.imread(image)
invert(img)
Experimental results :
5. Add Gaussian noise :
import numpy as np
import cv2
import tensorflow as tf
import matplotlib.pyplot as plt
from PIL import Image
from skimage import transform,data
def AddNoise(image,prob):
output = np.zeros(image.shape,np.uint8)
thres = 1 - prob
for i in range(image.shape[0]):
for j in range(image.shape[1]):
rdn = random.random()
if rdn < prob:
output[i][j] = 0
elif rdn > thres:
output[i][j] = 255
else:
output[i][j] = image[i][j]
return output
Mode one :
def Add_Noise(image):
fig,ax=plt.subplots(nrows=1,ncols=3,figsize=(12,6))
ax[0].imshow(image)
ax[0].set_title('Original Image',fontsize=14)
image1=AddNoise(image,0.1)
ax[1].imshow(image1)
ax[1].set_title('AddNoise Image',fontsize=14)
image2=AddNoise(image,0.3)
ax[2].imshow(image2)
ax[2].set_title('AddNoise light Image',fontsize=14)
plt.show()
image='D:\\3.jpg'
img=cv2.imread(image)
Add_Noise(img)
Experimental results :
Mode two :
def Gasuss_Noise(image, mean=0, var=0.001):
'''
Add Gaussian noise
mean : mean value
var : variance
'''
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
def Add_GasussNoise(image):
fig,ax=plt.subplots(nrows=1,ncols=3,figsize=(12,6))
ax[0].imshow(image)
ax[0].set_title('Original Image',fontsize=14)
image1=Gasuss_Noise(image)
ax[1].imshow(image1)
ax[1].set_title('Add GasussNoise Image',fontsize=14)
image2=Gasuss_Noise(image,1,2)
ax[2].imshow(image2)
ax[2].set_title('Add GasussNoise Image',fontsize=14)
plt.show()
image='D:\\3.jpg'
img=cv2.imread(image)
Add_GasussNoise(img)
Experimental results :
6. The zoom :
import cv2
import imutils
from PIL import Image
import matplotlib.pyplot as plt
Mode one :
def Rescale(image):
fig,ax=plt.subplots(nrows=1,ncols=3,figsize=(12,6))
ax[0].imshow(image)
ax[0].set_title('Original Image',fontsize=14)
image=cv2.resize(image,(64,64))
ax[1].imshow(image)
ax[1].set_title('Resize 64 Image',fontsize=14)
image=cv2.resize(image,(224,224))
ax[2].imshow(image)
ax[2].set_title('Resize 224 Image',fontsize=14)
plt.show()
image='D:\\1.jpg'
img=cv2.imread(image)
Rescale(img)
Experimental results :
Mode two :
from skimage import transform,data
def Rescale(image):
fig,ax=plt.subplots(nrows=1,ncols=3,figsize=(12,6))
ax[0].imshow(image)
ax[0].set_title('Original Image',fontsize=14)
image=transform.resize(image,(64,64))
ax[1].imshow(image)
ax[1].set_title('Resize 64 Image',fontsize=14)
image=transform.resize(image,(224,224))
ax[2].imshow(image)
ax[2].set_title('Resize 224 Image',fontsize=14)
plt.show()
image='D:\\1.jpg'
img=cv2.imread(image)
Rescale(img)
Experimental results :
Mode three :
import tensorflow as tf
(x_train,y_train),(x_test,y_test)=tf.keras.datasets.mnist.load_data()
Be careful :# This requires great attention ,transform.rescale Medium image It's a grayscale image , That is, the color channel is 1
def Rescale(image):
fig,ax=plt.subplots(nrows=1,ncols=3,figsize=(12,6))
ax[0].imshow(image)
ax[0].set_title('Original Image',fontsize=14)
image=transform.rescale(image,[0.3,0.5])
ax[1].imshow(image)
ax[1].set_title('Rescale row is 0.3 and col 0.5 Image',fontsize=14)
image=transform.rescale(image,[2,2])
ax[2].imshow(image)
ax[2].set_title('Rescale row is 0.1 and col 0.2 Image',fontsize=14)
plt.show()
Rescale(x_train[0])
Experimental results :
References and citations :
https://blog.csdn.net/duanyajun987/article/details/82663224
7. Tailoring :
import cv2
import numpy as np
from PIL import Image
import tensorflow as tf
import matplotlib.pyplot as plt
def Crop(image):
fig,ax=plt.subplots(nrows=1,ncols=3,figsize=(12,6))
ax[0].imshow(image)
ax[0].set_title('Original Image',fontsize=14)
Crop_Box=[12,12,78,78]
image1=image.crop(Crop_Box)
ax[1].imshow(image1)
ax[1].set_title('Rotate 90 Image',fontsize=14)
Crop_Box=[0,0,100,100]
image2=image.crop(Crop_Box)
ax[2].imshow(image2)
ax[2].set_title('Rotate 180 Image',fontsize=14)
plt.show()
image='D:\\1.jpg'
img=Image.open(image)
Crop(img)
Experimental results :
For the above , I've given you links to articles in the references and citations section . The other parts are implemented by ourselves .
边栏推荐
- After 4 years of naked resignation from the test, the test post of 15K interview was rubbed on the ground, and the result made me collapse and cry
- MFC auxiliary CString string splicing
- 2020080 simulation competition [horizontal and vertical coordinates do not affect each other, cactus minimum cut, combined meaning translation formula]
- Sort - merge sort
- 【AtCoder2305】Decrementing(博弈)
- Sort - select sort
- Servlet、ServletConfig、ServletContext
- 860. 柠檬水找零
- The solution of "no startup device" after running Bochs
- Introduction to operations research
猜你喜欢

forEach 中 return 和 for 中 break
![2020080 simulation competition [horizontal and vertical coordinates do not affect each other, cactus minimum cut, combined meaning translation formula]](/img/4d/a67a63d2c4eb80c98315c3057b01b9.jpg)
2020080 simulation competition [horizontal and vertical coordinates do not affect each other, cactus minimum cut, combined meaning translation formula]
![20200727 T2 small w playing game [generating function (binomial inversion technique)]](/img/a5/ae2192f4f37232cdcb01e81ad0297c.jpg)
20200727 T2 small w playing game [generating function (binomial inversion technique)]

2021-11-05 definition of cache

二本畢業,銀行外包測試工作 4 個月有餘。聊聊一些真實感受 ...

Deux diplômés, la Banque a externalisé le travail d'essai pendant plus de quatre mois. Parler de vrais sentiments...

2022.6.7 特长生模拟

QObject usage skills -- control function class

Detailed explanation of character function and string function (including simulation implementation)

Implementation of stack (C language)
随机推荐
C# 微信上传Form-data
The maximum number of divisors of numbers in the int range is 1536
Bidirectional linked list simple template (pointer version)
Wc2020 course selection
[atcoder2307] tree game
forEach 中 return 和 for 中 break
About static keyword
【软件测试】这样的简历已经刷掉了90%的面试者
Sort - merge sort
测试4年裸辞失业,面试15k的测试岗被按在地上摩擦,结局让我崩溃大哭...
【HDU6357】Hills And Valleys(DP)
Simple use of string
C language - growth diary-04- preliminary exploration of local variables (local variables)
Wc2020 guessing game
Uoj 553 [unr 4] caproic acid set [computational geometry (points in circle → points in half plane)]
QObject usage skills -- control function class
MFC custom string linked list
C memory alignment
How to output the percent sign "%" in printf function in C language
MFC auxiliary CString string splicing