get Perspective Transform
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import cv2 import numpy as np image = cv2.imread('images/scan.jpg') cv2.imshow('Original', image) cv2.waitKey(0) # Cordinates of the 4 corners of the original image points_A = np.float32([[320,15], [700,215], [85,610], [530,780]]) # Cordinates of the 4 corners of the desired output # We use a ratio of an A4 Paper 1 : 1.41 points_B = np.float32([[0,0], [420,0], [0,594], [420,594]]) # Use the two sets of four points to compute # the Perspective Transformation matrix, M M = cv2.getPerspectiveTransform(points_A, points_B) warped = cv2.warpPerspective(image, M, (420,594)) cv2.imshow('warpPerspective', warped) cv2.waitKey(0) cv2.destroyAllWindows() |
اگر بخواهیم یک تبدیل affine رو یک تصویر انجام بدهیم می توانیم از getPerspectiveTransform استفاده کنیم. به این صورت که مختصات چهار گوشه قسمتی از تصویر که می خواهیم تبدیل رو آن انجام شود را مشخص می کنیم (points_A) و همچنین مشخص می کنیم که می خواهیم این چهار نقطه رو چه نقاطی در تصویر جدید نگاشت شوند(points_B). تابع getPerspectiveTransform یک ماتریس 3*3 بر می گرداند که درواقع اگر هر کدام از چهار نقطه رو تصویر اصلی به فرم [xi, yi, 1] در این ماتریس ضرب شوند حاصل نقاط مورد نظر روی تصویر جدید خواهند بود که به فرم [ti*x’i, ti* y’i, ti] خواهد بود(مختصات تصویر جدید ‘x و ‘y می باشد و ti عدد ثابت است). حال برای اینکه تصویر جدید را ایجاد کنیم از getPerspectiveTransform استفاده می کنیم که به عنوان ورودی عکس اولیه و ماتریس تبدیل و اندازه عکس جدید را می گیرد و عکس جدید را برمی گرداند.
get Affine Transform
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import cv2 import numpy as np image = cv2.imread('images/qodsi.jpg') rows,cols,ch = image.shape cv2.imshow('Original', image) cv2.waitKey(0) # Cordinates of the 4 corners of the original image points_A = np.float32([[320,15], [700,215], [85,610]]) # Cordinates of the 4 corners of the desired output # We use a ratio of an A4 Paper 1 : 1.41 points_B = np.float32([[0,0], [420,0], [0,594]]) # Use the two sets of four points to compute # the Perspective Transformation matrix, M M = cv2.getAffineTransform(points_A, points_B) warped = cv2.warpAffine(image, M, (cols, rows)) print(ch) cv2.imshow('warpPerspective', warped) cv2.waitKey(0) cv2.destroyAllWindows() |
با استفاده از getAffineTransform نیز می توان یک تبدیل affine انجام داد با این تفاوت که از سه نقطه استفاده می کنیم. طبق رابطه ی مشخص شده, حاصل getAffineTransform یک ماتریس 2*3 می باشد که اگر هر کدام از سه تقطه اولیه به فرم [xi, yi, 1] در این ماتریس ضرب شوند حاصل نگاشت این نقاط در تصویر جدید به فرم [x’i, y’i] خواهد بود. با استفاده از warpAffine تصویر جدید به دست می اید که ورودی ان همان طول و عرض تصویر اصلی و تابع تبدیل(M) و تصویر اولیه می باشد.
شبکه های اجتماعی