Movatterモバイル変換


[0]ホーム

URL:



Code forImage Transformations using OpenCV in Python Tutorial


View on Github

translation.py

import numpy as npimport cv2import matplotlib.pyplot as plt# read the input imageimg = cv2.imread("city.jpg")# convert from BGR to RGB so we can plot using matplotlibimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# disable x & y axisplt.axis('off')# show the imageplt.imshow(img)plt.show()# get the image shaperows, cols, dim = img.shape# transformation matrix for translationM = np.float32([[1, 0, 50],                [0, 1, 50],                [0, 0, 1]])# apply a perspective transformation to the imagetranslated_img = cv2.warpPerspective(img, M, (cols, rows))# disable x & y axisplt.axis('off')# show the resulting imageplt.imshow(translated_img)plt.show()# save the resulting image to diskplt.imsave("city_translated.jpg", translated_img)

scaling.py

import numpy as npimport cv2import matplotlib.pyplot as plt# read the input imageimg = cv2.imread("city.jpg")# convert from BGR to RGB so we can plot using matplotlibimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# disable x & y axisplt.axis('off')# show the imageplt.imshow(img)plt.show()# get the image shaperows, cols, dim = img.shape#transformation matrix for ScalingM = np.float32([[1.5, 0  , 0],            [0,   1.8, 0],            [0,   0,   1]])# apply a perspective transformation to the imagescaled_img = cv2.warpPerspective(img,M,(cols*2,rows*2))# disable x & y axisplt.axis('off')# show the resulting imageplt.imshow(scaled_img)plt.show()# save the resulting image to diskplt.imsave("city_scaled.jpg", scaled_img)

shearing.py

import numpy as npimport cv2import matplotlib.pyplot as plt# read the input imageimg = cv2.imread("city.jpg")# convert from BGR to RGB so we can plot using matplotlibimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# disable x & y axisplt.axis('off')# show the imageplt.imshow(img)plt.show()# get the image shaperows, cols, dim = img.shape# transformation matrix for Shearing# shearing applied to x-axisM = np.float32([[1, 0.5, 0],             [0, 1  , 0],            [0, 0  , 1]])# shearing applied to y-axis# M = np.float32([[1,   0, 0],#               [0.5, 1, 0],#               [0,   0, 1]])# apply a perspective transformation to the image                sheared_img = cv2.warpPerspective(img,M,(int(cols*1.5),int(rows*1.5)))# disable x & y axisplt.axis('off')# show the resulting imageplt.imshow(sheared_img)plt.show()# save the resulting image to diskplt.imsave("city_sheared.jpg", sheared_img)

reflection.py

import numpy as npimport cv2import matplotlib.pyplot as plt# read the input imageimg = cv2.imread("city.jpg")# convert from BGR to RGB so we can plot using matplotlibimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# disable x & y axisplt.axis('off')# show the imageplt.imshow(img)plt.show()# get the image shaperows, cols, dim = img.shape# transformation matrix for x-axis reflection M = np.float32([[1,  0, 0   ],                [0, -1, rows],                [0,  0, 1   ]])# transformation matrix for y-axis reflection# M = np.float32([[-1, 0, cols],#                 [ 0, 1, 0   ],#                 [ 0, 0, 1   ]])# apply a perspective transformation to the imagereflected_img = cv2.warpPerspective(img,M,(int(cols),int(rows)))# disable x & y axisplt.axis('off')# show the resulting imageplt.imshow(reflected_img)plt.show()# save the resulting image to diskplt.imsave("city_reflected.jpg", reflected_img)

rotation.py

import numpy as npimport cv2import matplotlib.pyplot as plt# read the input imageimg = cv2.imread("city.jpg")# convert from BGR to RGB so we can plot using matplotlibimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# disable x & y axisplt.axis('off')# show the imageplt.imshow(img)plt.show()# get the image shaperows, cols, dim = img.shape#angle from degree to radianangle = np.radians(10)#transformation matrix for RotationM = np.float32([[np.cos(angle), -(np.sin(angle)), 0],            [np.sin(angle), np.cos(angle), 0],            [0, 0, 1]])# apply a perspective transformation to the imagerotated_img = cv2.warpPerspective(img, M, (int(cols),int(rows)))# disable x & y axisplt.axis('off')# show the resulting imageplt.imshow(rotated_img)plt.show()# save the resulting image to diskplt.imsave("city_rotated.jpg", rotated_img)

cropping.py

import numpy as npimport cv2import matplotlib.pyplot as plt# read the input imageimg = cv2.imread("city.jpg")# convert from BGR to RGB so we can plot using matplotlibimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# disable x & y axisplt.axis('off')# show the imageplt.imshow(img)plt.show()# get 200 pixels from 100 to 300 on both x-axis & y-axis# change that if you will, just make sure you don't exceed cols & rowscropped_img = img[100:300, 100:300]# disable x & y axisplt.axis('off')# show the resulting imageplt.imshow(cropped_img)plt.show()# save the resulting image to diskplt.imsave("city_cropped.jpg", cropped_img)

Practical Python PDF Processing EBook - Topic - Top


Join 50,000+ Python Programmers & Enthusiasts like you!



Tags

Ethical Hacking with Python EBook - Resources - Middle


New Tutorials

Popular Tutorials


Practical Python PDF Processing EBook - Abdou Author - Bottom







[8]ページ先頭

©2009-2025 Movatter.jp