0% found this document useful (0 votes)
4 views2 pages

Image Affine Transformation Code

The document contains a Python script that performs an affine transformation on an image using the PIL library. It defines a function that applies scaling, translation, rotation, and reflection to the image, and then transforms the image accordingly. Finally, it displays the transformed image using matplotlib's imshow function.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Image Affine Transformation Code

The document contains a Python script that performs an affine transformation on an image using the PIL library. It defines a function that applies scaling, translation, rotation, and reflection to the image, and then transforms the image accordingly. Finally, it displays the transformed image using matplotlib's imshow function.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import os

from PIL import Image

from pylab import *

goruntu = [Link]('[Link]')

def affine_transform(goruntu,scale=(1, 1),kayma=(0, 0),

donme=0,translate=(0, 0),aynalama=(1,1)):

k_x, k_y = kayma

tx, ty = translate

ax, ay = aynalama

theta = donme

sx, sy = scale

x_kayma_matris = matrix([[1, k_x, 0], [0, 1, 0],[0, 0, 1]])

y_kayma_matris = matrix([[1, 0, 0], [k_y, 1, 0], [0, 0, 1]])

donme_matris = matrix([[cos(theta), -sin(theta), 0],

[sin(theta), cos(theta), 0],

[0, 0, 1]])

scale_matris = matrix([[sx, 0, 0], [0, sy, 0], [0, 0, 1]])

aynalama_matris = matrix([[ax, 0, 0], [0, ay, 0], [0, 0, 1]])

oteleme=matrix([[1, 0, tx], [0, 1, ty], [0, 0, 1]])

trans_matris = donme_matris * y_kayma_matris

trans_matris*=x_kayma_matris * scale_matris*aynalama_matris

trans_matris+=oteleme

w, h = [Link]

once = []

x, y = [], []

[Link](array([[0, 0, 1]]).transpose())

[Link](array([[w-1, 0, 1]]).transpose())

[Link](array([[0, h-1, 1]]).transpose())

[Link](array([[w-1, h-1, 1]]).transpose())


for i in range(len(once)):

sonra = trans_matris * once[i]

[Link](sonra[0, 0])

[Link](sonra[1, 0])

yeni_yukseklik = int(round(max(y) - min(y))) + 1

yeni_genislik = int(round(max(x) - min(x))) + 1

trans_matris = matrix([[1, 0, -min(x)], [0, 1, -min(y)],

[0, 0, 1]]) * trans_matris

t_inv = [Link](trans_matris)

t_inv_tuple = (t_inv[0, 0], t_inv[0, 1],

t_inv[0, 2],t_inv[1, 0],

t_inv[1, 1], t_inv[1, 2])

return t_inv_tuple,yeni_genislik, yeni_yukseklik

affine_tuple ,ww, hh= affine_transform(goruntu,scale=(1.2, 1.2),

kayma=(0.0, 0.0),

donme=pi/4,

translate=(10,0),

aynalama=(-1,-1))

wp_goruntu = [Link]((ww, hh),[Link],

affine_tuple)

imshow(wp_goruntu)

You might also like