تبدیلات همگن (Affine Transform)
انتقال (Translation) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import cv2 import numpy as np image = cv2.imread('images/input.jpg') # Store height and width of the image height, width = image.shape[:2] quarter_height, quarter_width = height/4, width/4 # | 1 0 Tx | # T = | 0 1 Ty | # T is our translation matrix T = np.float32([[1, 0, quarter_width], [0, 1,quarter_height]]) # We use warpAffine to transform the image using the matrix, T img_translation = cv2.warpAffine(image, T, (width, height)) cv2.imshow('Translation', img_translation) cv2.waitKey() cv2.destroyAllWindows() |
برای پیاده سازی این تبدیلات از cv2.warpAffine استفاده میکینم.
به طور مثال در کد زیر :
پس از خواندن عکس و دریافت عرض و طول تصویر:
1 2 3 |
cv2.imread('images/input.jpg') height, width = image.shape[:2] |
یک نسبت دلخواه برای انتقال طول و عرض تصویر در نظر گرفتیم :
1 |
quarter_height, quarter_width = height/4, width/4 |
با استفاده از ضرب ماتریس انتقال در مختصات تصویر ،تصویر انتقال پیدا میکند :
1 2 3 |
T = np.float32([[1, 0, quarter_width], [0, 1, quarter_height]]) # پارامتر اول ، سطر اول # پارامتر دوم ، سطر دوم |
از warpAffine برای تبدیل عکس استفاده میکنیم که ورودی های آن به صورت زیر است :
1 2 |
#image, transform matris , (width, height)=>output image cv2.warpAffine(image, T, (width, height)) |
چرخاندن (Rotations) :
*یادآوری
ماتریس دوران:
دوران در opencv
1 2 3 4 |
cv2.getRotationMatrix2D(rotation_center_x, rotation_center_y, angle of rotation, scale) # rotation_center_x : مرکز چرخش نسبت به عرض # rotation_center_y : مرکز چرخش نسبت به ارتفاع # angle of rotation : زوایه چرخش |
1 2 3 4 5 6 7 8 9 10 11 |
import cv2 import numpy as np image = cv2.imread('images/input.jpg') height, width = image.shape[:2] # Divide by two to rototate the image around its centre rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), 20, .5) print(rotation_matrix) rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height)) cv2.imshow('Rotated Image', rotated_image) cv2.waitKey() cv2.destroyAllWindows() |
- از نقص های این روش میتوان به ایجاد فضاهای سیاه اطراف تصویر اشاره کرد که در ادامه فضاهای سیاه ایجاد شده اطراف تصویر را با کراپ از بین میبریم.
روشی دیگر برای دوران :
ترانهاده ماتریس
ماتریس ترانهاده با تغییر جای سطر ها با ستون ها باعث چرخش تصویر میشود و فضای سیاه اطراف تصویر ایجاد نمی کند .
1 2 3 4 5 6 |
#Other Option to Rotate img = cv2.imread('images/input.jpg') rotated_image = cv2.transpose(img) cv2.imshow('Rotated Image - Method 2', rotated_image) cv2.waitKey() cv2.destroyAllWindows() |
flip تصویر
تصویر را در جهت افق و خط عمود آینه می کند.
1 2 3 4 5 6 |
# Let's now to a horizontal flip. flipped = cv2.flip(image, 1) cv2.imshow('original', image) cv2.imshow('Horizontal Flip', flipped) cv2.waitKey() cv2.destroyAllWindows() |
پارامتر دوم :
1 |
cv2.flip(image, 1) |
صفر یا false عکس را حول محور x ها قرینه میکند.
یک یا True عکس را حول محور y ها قرینه میکند.
تغییر اندازه (scaling, resizing) و درونیابی (interpolations)
1 2 3 4 5 |
cv2.resize(image, dsize(output image size), x scale, y scale, interpolation) # image : دریافت عکس ورودی # dsize(output image size) : اندازه تصویر خروجی # x scale, y scale : تغییر مقیاس در راستای x و y # interpolation : روش درون یابی |
- fx و fy پارامتر های نام دار هستند میتوان آنها را به تابع پاس نداد.
روش های درون یابی :
- Bilinear Interpolation (درج وابسته به ۴ پیکسل ۴ طرف)
- Bicubic interpolation (درج وابسته به ۴ پیکسل ۴ طرف به علاوه ۴ پیکسل اریب)
- Nearest Neighbor (نزدیکترین همسایه)
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 # load our input image image = cv2.imread('images/input.jpg') cv2.imshow('Original Image', image) cv2.waitKey() # bilinear interpolation (used by default) # Let's make our image 3/4 of it's original size image_scaled = cv2.resize(image, None, fx=0.75, fy=0.75) cv2.imshow('Scaling - Linear Interpolation', image_scaled) cv2.waitKey() # bicubic interpolation over 4x4 pixel neighborhood # Let's double the size of our image img_scaled = cv2.resize(image, None, fx=2, fy=2, interpolation = cv2.INTER_CUBIC) cv2.imshow('Scaling - Cubic Interpolation', img_scaled) cv2.waitKey() # nearest-neighbor interpolation # Let's skew the re-sizing by setting exact dimensions img_scaled = cv2.resize(image, (900, 400), interpolation = cv2.INTER_AREA) cv2.imshow('Scaling - Skewed Size', img_scaled) cv2.waitKey() cv2.destroyAllWindows() |
استفاده از ماتریس تبدیل
جهت هایی را که میخواهیم scale شوند را در قطر ماتریس قرار میدهیم.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import cv2 import numpy as np image = cv2.imread('images/input.jpg') # Store height and width of the image height, width = image.shape[:2] # | Sx 0 0 | # T = | 0 Sy 0 | # T is our translation matrix T = np.float32([[0.5, 0, 0], [0, 0.5,0]]) # We use warpAffine to transform the image using the matrix, T img_translation = cv2.warpAffine(image, T, (width, height)) cv2.imshow('Translation', img_translation) cv2.waitKey() cv2.destroyAllWindows() |
معایب :
- مشکل سیاهی تصویر را دارد.
- عکس بعد از اعمال interpolations باید crop شود .
Image Pyramids
در بعضی موارد، ما باید با تصاویری با وضوح مختلف تصویر مشابه کار کنیم. به عنوان مثال، هنگام جستجو برای چیزی در یک تصویر، مانند چهره، ما مطمئن نیستیم که کدام اندازه از شئ در تصویر وجود دارد.
1 2 3 4 5 6 7 8 9 |
import cv2 image = cv2.imread('images/input.jpg') smaller = cv2.pyrDown(image) larger = cv2.pyrUp(smaller) cv2.imshow('Original', image ) cv2.imshow('Smaller ', smaller ) cv2.imshow('Larger ', larger ) cv2.waitKey(0) cv2.destroyAllWindows() |
در این روش عکس را به صورت پلکانی به سایز های مختلف می بریم :
1 2 |
smaller = cv2.pyrDown(image) larger = cv2.pyrUp(smaller) |
کاربرد: آموزش به هوش مصنوعی برای تشخیص اشیاء
Cropping
Opencv تابع مشخص برای کراپ کردن ندارد.
روش های جایگزین :
یکی از روش های کراپ کردن slicing است که با دادن بازه ی دقیقی از x ,y (برای ساده تر شدن عکس را بصورت دو بعدی و سیاه سفید تصور میکنیم)همان بازه ی مشخص شده را کراپ می کنیم.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import cv2 import numpy as np image = cv2.imread('images/input.jpg') height, width = image.shape[:2] # Let's get the starting pixel coordiantes (top left of cropping rectangle) start_row, start_col = int(height * .25), int(width * .25) # Let's get the ending pixel coordinates (bottom right) end_row, end_col = int(height * .75), int(width * .75) # Simply use indexing to crop out the rectangle we desire cropped = image[100:300 , 200:800] cv2.imshow("Original Image", image) cv2.imshow("Cropped Image", cropped) cv2.waitKey(0) cv2.destroyAllWindows() |
نکات :
مختصات شروع و پایان حتما باید اعداد صحیح باشند.
در بازه نویسی به صورت [ x1 : x2 , y1:y2 ] :
x1 شامل بازه میشود اما x2 نوشته نمیشود .
روش دیگر اسلایس کردن ماتریس ها به کمک numpy است.
مثال:
a=np.array([[100,200,400],[1,2,3]])
میخواهیم عناصر سطر اول [0] و ستون دوم [1] را انتخاب کنیم.
a [0: , 1:] => array([[200, 400],[ 2, 3]])
شبکه های اجتماعی