resize
cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])
|Method | interpolation |
---|---|
Nearest Neighbor | cv2.INTER_NEAREST |
Bilinear | cv2.INTER_LINEAR |
Bicubic | cv2.INTER_CUBIC |
In [51]: rszNN = cv2.resize(I, (I.shape[1]*2, I.shape[0]*2), interpolation=cv2.INTER_NEAREST)
...: rszBL = cv2.resize(I, (I.shape[1]*2, I.shape[0]*2), interpolation=cv2.INTER_LINEAR)
...: rszBC = cv2.resize(I, (I.shape[1]*2, I.shape[0]*2), interpolation=cv2.INTER_CUBIC)
import numpy as np
import cv2
import matplotlib.pyplot as plt
I = cv2.imread('./data/SIDBA/Lenna.bmp')
rszNN = cv2.resize(I, (I.shape[1]*2, I.shape[0]*2), interpolation=cv2.INTER_NEAREST)
rszBL = cv2.resize(I, (I.shape[1]*2, I.shape[0]*2), interpolation=cv2.INTER_LINEAR)
rszBC = cv2.resize(I, (I.shape[1]*2, I.shape[0]*2), interpolation=cv2.INTER_CUBIC)
sz = np.array([I.shape[0],I.shape[1]])
csz = np.array([32,32])
tlpos = (sz - csz)//2
brpos = tlpos + csz
croppedNN = rszNN[tlpos[0]:brpos[0],tlpos[1]:brpos[1],:]
croppedBL = rszBL[tlpos[0]:brpos[0],tlpos[1]:brpos[1],:]
croppedBC = rszBC[tlpos[0]:brpos[0],tlpos[1]:brpos[1],:]
fig, axes = plt.subplots(ncols=3)
axes[0].imshow(croppedNN)
axes[0].set_title('nearest')
axes[0].set(adjustable='box-forced',aspect='equal')
axes[1].imshow(croppedBL)
axes[1].set_title('bilinear')
axes[1].set(adjustable='box-forced',aspect='equal')
axes[2].imshow(croppedBC)
axes[2].set_title('bicubic')
axes[2].set(adjustable='box-forced',aspect='equal')
fig.show()
rotate
Wenn Sie sich um die Bildmitte drehen möchten, verwenden Sie getRotationMatrix2D und warpAffine. Es ist jedoch einfacher, die später beschriebene Drehung von Scipy zu verwenden.
cv2.getRotationMatrix2D(center, angle, scale)
cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
import cv2
import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage
I = cv2.imread('./data/SIDBA/Lenna.bmp')
rIntr = 15
rs = 0
re = 360
Ir = []
for r in range(rs, re+1, rIntr):
center = (I.shape[1]*0.5,I.shape[0]*0.5)
rotMat = cv2.getRotationMatrix2D(center, r, 1.0)
Irot = cv2.warpAffine(I, rotMat, (I.shape[1],I.shape[0]), flags=cv2.INTER_LINEAR)
Ir.append(Irot)
cols = 4
rows = int(np.ceil(len(Ir) / float(cols)))
fig, axes = plt.subplots(nrows=rows, ncols=cols, figsize=(3*cols,3*rows))
for idx, I in enumerate(Ir):
r = idx // cols
c = idx % cols
title = 'rotate=%d' % (rIntr*idx)
axes[r,c].imshow(cv2.cvtColor(I, cv2.COLOR_BGR2RGB))
axes[r,c].set_title(title)
axes[r,c].set(adjustable='box-forced',aspect='equal')
axes[r,c].get_xaxis().set_visible(False)
axes[r,c].get_yaxis().set_visible(False)
for i in range(idx+1, rows*cols):
r = i // cols
c = i % cols
fig.delaxes(axes[r,c])
fig.show()
Wenn das Bild rechteckig ist
scipy drehen ist einfach mit scipy zu tun
import cv2
import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage
I = cv2.imread('./data/SIDBA/Lenna.bmp')
rIntr = 15
rs = 0
re = 360
Ir = []
for r in range(rs, re+1, rIntr):
Irot = ndimage.rotate(I, r, reshape=False)
Ir.append(Irot)
cols = 4
rows = int(np.ceil(len(Ir) / float(cols)))
fig, axes = plt.subplots(nrows=rows, ncols=cols, figsize=(3*cols,3*rows))
for idx, I in enumerate(Ir):
r = idx // cols
c = idx % cols
title = 'rotate=%d' % (rIntr*idx)
axes[r,c].imshow(cv2.cvtColor(I, cv2.COLOR_BGR2RGB))
axes[r,c].set_title(title)
axes[r,c].set(adjustable='box-forced',aspect='equal')
axes[r,c].get_xaxis().set_visible(False)
axes[r,c].get_yaxis().set_visible(False)
for i in range(idx+1, rows*cols):
r = i // cols
c = i % cols
fig.delaxes(axes[r,c])
fig.show()
flip
cv2.flip(src, flipCode[, dst])
Ich weiß nicht, welcher FlipCode vertikal oder horizontal ist
flipCode = 0 ... vertical flipCode = 1 ... horizontal
Sie können fliplr und flipud von numpy verwenden, die später beschrieben werden. lr bedeutet links, rechts, ud bedeutet oben, unten.
import cv2
import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage
I = cv2.imread('./data/SIDBA/Lenna.bmp')
Iv = cv2.flip(I, 0)
Ih = cv2.flip(I, 1)
fig, axes = plt.subplots(ncols=3, figsize=(15,10))
axes[0].imshow(cv2.cvtColor(I, cv2.COLOR_BGR2RGB))
axes[0].set_title('original')
axes[0].set(adjustable='box-forced',aspect='equal')
axes[0].get_xaxis().set_visible(False)
axes[0].get_yaxis().set_visible(False)
axes[1].imshow(cv2.cvtColor(Iv, cv2.COLOR_BGR2RGB))
axes[1].set_title('flip vertical')
axes[1].set(adjustable='box-forced',aspect='equal')
axes[1].get_xaxis().set_visible(False)
axes[1].get_yaxis().set_visible(False)
axes[2].imshow(cv2.cvtColor(Ih, cv2.COLOR_BGR2RGB))
axes[2].set_title('flip horizontal')
axes[2].set(adjustable='box-forced',aspect='equal')
axes[2].get_xaxis().set_visible(False)
axes[2].get_yaxis().set_visible(False)
fig.show()
numpy
numpy.fliplr(m) Horizontaler Flip numpy.flipud(m) Vertikaler Flip
import cv2
import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage
I = cv2.imread('./data/SIDBA/Lenna.bmp')
Iv = np.flipud(I)
Ih = np.fliplr(I)
fig, axes = plt.subplots(ncols=3, figsize=(15,10))
axes[0].imshow(cv2.cvtColor(I, cv2.COLOR_BGR2RGB))
axes[0].set_title('original')
axes[0].set(adjustable='box-forced',aspect='equal')
axes[0].get_xaxis().set_visible(False)
axes[0].get_yaxis().set_visible(False)
axes[1].imshow(cv2.cvtColor(Iv, cv2.COLOR_BGR2RGB))
axes[1].set_title('flip vertical')
axes[1].set(adjustable='box-forced',aspect='equal')
axes[1].get_xaxis().set_visible(False)
axes[1].get_yaxis().set_visible(False)
axes[2].imshow(cv2.cvtColor(Ih, cv2.COLOR_BGR2RGB))
axes[2].set_title('flip horizontal')
axes[2].set(adjustable='box-forced',aspect='equal')
axes[2].get_xaxis().set_visible(False)
axes[2].get_yaxis().set_visible(False)
fig.show()
Recommended Posts