当前位置:网站首页>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 .
边栏推荐
- Rabin-Miller素数测试
- 零基础自学SQL课程 | UNION 联合查询
- Uoj 553 [unr 4] caproic acid set [computational geometry (points in circle → points in half plane)]
- Tidb cloud launched Google cloud marketplace, empowering global developers with a new stack of real-time HTAP databases
- Wc2020 course selection
- 【IoT】智能硬件:如何获取硬件产品的wifi信号强度
- 【POJ3691】DNA repair (AC自动机+DP)
- 20200802 T3 I always like [generating function exclusion, Lagrangian inversion]
- [atcoder2376] black and white tree (game)
- 2021-10-24
猜你喜欢

测试4年裸辞失业,面试15k的测试岗被按在地上摩擦,结局让我崩溃大哭...

C language inherits memory management mechanism (unfinished)

Import on CSDN MD file

【AtCoder1980】Mysterious Light(数学模拟)

The solution of "no startup device" after running Bochs

批量拼接字符串
![[IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products](/img/85/5766d8269391820b5e142178530657.png)
[IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products

Zero foundation self-study SQL course | outer join external connection

Post-processing of ffmpeg miscellaneous notes

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
随机推荐
C language lesson 2
【AtCoder2305】Decrementing(博弈)
【AtCoder2000】Leftmost Ball (DP+组合数)
【AtCoder1981】Shorten Diameter(图论思维)
Simple use of string
Introduction to operations research
【IoT】项目管理:如何打造更好的跨职能团队?
[atcoder1984] wide swap
Uoj 554 [unr 4] challenges Hamilton [find Hamilton path (adjustment method)]
20200730 T3 small B farm [maximum perimeter empty rectangle (monotone stack + line segment tree)] & "ROI 2017 day 2" learning track
pmp到底是什么?
C language to write a calculator MVC (very interesting code architecture callback and constructor mode and the use of pointer functions)
forEach 中 return 和 for 中 break
【软件测试】这样的简历已经刷掉了90%的面试者
[untitled] Weng_ C lesson 1
What exactly is PMP?
TiDB Cloud 上线 Google Cloud Marketplace,以全新一栈式实时 HTAP 数据库赋能全球开发者
Classes and objects (medium)
20200810 T2 dispatch money
multi-sig SC