Affine transformation

Execution environment

Google Colaboratory

Preparing to load images with Google Colaboratory

python


from google.colab import files
from google.colab import drive
drive.mount('/content/drive')
img = cv2.imread("/content/drive/My Drive/Colab Notebooks/img/Lenna.bmp")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #Convert for matplotlib

Loading the required libraries

python


import cv2 #opencv
import matplotlib.pyplot as plt 
%matplotlib inline

Flip conversion

python


#Create new window
fig = plt.figure()

#Original image
ax1 = fig.add_subplot(2, 1, 1)
ax1.set_title("Original",fontsize=20)
plt.imshow(img)

#flip upside down= 0
ax2 = fig.add_subplot(2, 3, 4)
ax2.set_title("flipCode = 0",fontsize=20)
plt.imshow(cv2.flip(img,flipCode = 0))

#Up / down / left / right= -1
ax3 = fig.add_subplot(2, 3, 5)
ax3.set_title("flipCode = -1",fontsize=20)
plt.imshow(cv2.flip(img,flipCode = -1))

#Flip horizontal= 1
ax4 = fig.add_subplot(2, 3, 6)
ax4.set_title("flipCode = 1",fontsize=20)
plt.imshow(cv2.flip(img,flipCode = 1))
plt.subplots_adjust(hspace=0.6,wspace=1.2) #Adjust graph spacing.

plt.show()

Flip conversion result

q3_flip.PNG

Flip description

cv2.flip(src,flipCode) src = image flipCode = flip direction

flipcode direction
0 Upside down
<0 Up / Down / Left / Right
> 0 Flip left and right

resize

python


#Create new window
fig = plt.figure()

height = img.shape[0]
width = img.shape[1]

#Original image
ax1 = fig.add_subplot(2, 1, 1)
ax1.set_title("Original",fontsize=20)
plt.imshow(img)

#16×16
ax2 = fig.add_subplot(2, 3, 4)
ax2.set_title("16*16",fontsize=20)
plt.imshow(cv2.resize(img,(16,16)))

#50%
ax3 = fig.add_subplot(2, 3, 5)
ax3.set_title("50%",fontsize=20)
plt.imshow(cv2.resize(img,(int(width*0.5),int(height*0.5))))

#200%*50%
ax3 = fig.add_subplot(2, 3, 6)
ax3.set_title("200%*50%",fontsize=20)
plt.imshow(cv2.resize(img,(int(width*2),int(height*0.5))))
plt.subplots_adjust(hspace=0.6,wspace=1.2) #Adjust graph spacing.

plt.show()

Resize conversion result

q3_resize.PNG

rotation

Described at a later date

Perspective projection

Described at a later date

trimming

Described at a later date

Recommended Posts

Affine transformation
Affine transformation by OpenCV (CUDA)
[FSL] Affine transformation (translation + linear transformation)