一、柱形畸变
import cv2
import numpy as np
def cylindrical_projection ( image, f) :
h, w = image. shape[ : 2 ]
map_x, map_y = np. meshgrid( np. arange( w) , np. arange( h) )
x_c = w / 2
y_c = h / 2
theta = ( map_x - x_c) / f
h_c = ( map_y - y_c) / np. sqrt( ( map_x - x_c) ** 2 + f** 2 )
map_x_cyl = f * np. tan( theta) + x_c
map_y_cyl = f * h_c + y_c
return cv2. remap( image, map_x_cyl. astype( np. float32) , map_y_cyl. astype( np. float32) , cv2. INTER_LINEAR)
image = cv2. imread( r"/media/face/Face/Face/ABERDEEN_Face/ABERDEEN/adrian1.jpg" )
focal_length = 120
result = cylindrical_projection( image, focal_length)
cv2. imwrite( "test.jpg" , result)
def cylindrical_projection ( image, f) :
h, w = image. shape[ : 2 ]
map_x, map_y = np. meshgrid( np. arange( w) , np. arange( h) )
x_c = w / 2
y_c = h * 1.5
theta = ( map_x - x_c) / f
h_c = ( map_y - y_c) / np. sqrt( ( map_x - x_c) ** 2 + f** 2 )
map_x_cyl = f * np. tan( theta) + x_c
map_y_cyl = f * h_c + y_c
return cv2. remap( image, map_x_cyl. astype( np. float32) , map_y_cyl. astype( np. float32) , cv2. INTER_LINEAR)
二、桶形畸变
import cv2
import numpy as np
def Barrel_projection ( image, f) :
"""
模拟桶形畸变效果
:param image: 输入图像
:param k_factor: 桶形畸变因子,值越大畸变越明显
:return: 模拟桶形畸变后的图像
"""
h, w = image. shape[ : 2 ]
y_i, x_i = np. ogrid[ : h, : w]
center_x, center_y = w // 2 , h // 2
r = np. sqrt( ( x_i - center_x) ** 2 + ( y_i - center_y) ** 2 )
r_max = np. sqrt( ( w / 2 ) ** 2 + ( h / 2 ) ** 2 )
r_distorted = r * ( 1 + f * ( r / r_max) ** 2 )
theta = np. arctan2( y_i - center_y, x_i - center_x)
x_distorted = center_x + r_distorted * np. cos( theta)
y_distorted = center_y + r_distorted * np. sin( theta)
distorted_image = cv2. remap( image, x_distorted. astype( np. float32) , y_distorted. astype( np. float32) ,
interpolation= cv2. INTER_LINEAR)
return distorted_image
image = cv2. imread( r"/media/face/Face/Face/ABERDEEN_Face/ABERDEEN/adrian1.jpg" )
focal_length = 5
result = Barrel_projection( image, focal_length)
cv2. imwrite( "test.jpg" , result)
三、旋转变换(不丢失信息)
import cv2
import numpy as np
def rotate_projection ( image, angle) :
( h, w) = image. shape[ : 2 ]
( cX, cY) = ( w / 2 , h / 2 )
M = cv2. getRotationMatrix2D( ( cX, cY) , angle, 1.0 )
cos = np. abs ( M[ 0 , 0 ] )
sin = np. abs ( M[ 0 , 1 ] )
nW = int ( ( h * sin) + ( w * cos) )
nH = int ( ( h * cos) + ( w * sin) )
M[ 0 , 2 ] += ( nW / 2 ) - cX
M[ 1 , 2 ] += ( nH / 2 ) - cY
img_rotated = cv2. warpAffine( image, M, ( nW, nH) )
return img_rotated
image = cv2. imread( r"/media/face/Face/Face/ABERDEEN_Face/ABERDEEN/adrian1.jpg" )
focal_length = 60
result = rotate_projection( image, focal_length)
cv2. imwrite( "test.jpg" , result)
四、扇形畸变
import cv2
import numpy as np
def fan_distortion ( img, center, radius, angle_range) :
height, width = img. shape[ : 2 ]
map_x, map_y = np. meshgrid( np. arange( width) , np. arange( height) )
map_x = map_x. astype( np. float32)
map_y = map_y. astype( np. float32)
dist = np. sqrt( ( map_x - center[ 0 ] ) ** 2 + ( map_y - center[ 1 ] ) ** 2 )
angle = np. arctan2( map_y - center[ 1 ] , map_x - center[ 0 ] )
dist_normalized = dist / radius
distortion = ( dist_normalized** 2 ) * angle_range
distorted_x = dist * np. cos( angle + distortion) + center[ 0 ]
distorted_y = dist * np. sin( angle + distortion) + center[ 1 ]
map_distorted = cv2. remap( img, distorted_x, distorted_y, cv2. INTER_LINEAR)
return map_distorted
center = ( 0 , image. shape[ 0 ] // 2 )
radius = min ( image. shape[ 1 ] , image. shape[ 0 ] ) // 1
angle_range = np. pi / 6
distorted_image = fan_distortion( image, center, radius, angle_range)
cv2. imwrite( "test.jpg" , distorted_image)
五、透视畸变(不丢失信息)
import cv2
import numpy as np
import random
def random_perspective_distortion ( img) :
height, width = img. shape[ : 2 ]
random_offset = 150
img = cv2. copyMakeBorder( img, random_offset, random_offset, random_offset, random_offset, borderType= 0 )
new_width = width + 2 * random_offset
new_height = height + 2 * random_offset
pts_original = np. float32( [ [ 0 , 0 ] , [ new_width, 0 ] , [ 0 , new_height] , [ new_width, new_height] ] )
pts_random = np. float32( [ [ 0 + np. random. uniform( - random_offset, random_offset) ,
0 + np. random. uniform( - random_offset, random_offset) ] ,
[ new_width + np. random. uniform( - random_offset, random_offset) ,
0 + np. random. uniform( - random_offset, random_offset) ] ,
[ 0 + np. random. uniform( - random_offset, random_offset) ,
new_height + np. random. uniform( - random_offset, random_offset) ] ,
[ new_width + np. random. uniform( - random_offset, random_offset) ,
new_height + np. random. uniform( - random_offset, random_offset) ] ] )
matrix = cv2. getPerspectiveTransform( pts_original, pts_random)
distorted_image = cv2. warpPerspective( img, matrix, ( new_width, new_height) )
return distorted_image
image = cv2. imread( r"/media/face/Face/Face/ABERDEEN_Face/ABERDEEN/adrian1.jpg" )
distorted_image = random_perspective_distortion( image)
cv2. imwrite( "test.jpg" , distorted_image)