wk-labs/lab5/zad1.py

44 lines
1.3 KiB
Python
Raw Normal View History

2021-11-28 23:44:09 +01:00
import cv2 as cv
import numpy as np
def click_event(event, x, y, flags, params):
if event == cv.EVENT_LBUTTONDOWN:
# print(x, ' ', y)
2021-11-29 18:31:35 +01:00
cv.circle(img_copy, (int(x), int(y)), 10, [0, 255, 255], -1)
cv.imshow('image', img_copy)
2021-11-28 23:44:09 +01:00
vertexes.append([x, y])
if len(vertexes) == 4:
add_img_in_area()
def add_img_in_area():
dst_points = np.array(vertexes, dtype=float)
2021-11-29 18:02:09 +01:00
src_points = np.array([[0, 0], [bakery_w, 0], [bakery_w, bakery_h], [0, bakery_h]], dtype=float)
2021-11-29 18:31:35 +01:00
2021-11-29 18:02:09 +01:00
mat_h = cv.findHomography(src_points, dst_points)[0]
image_transformed_h = cv.warpPerspective(bakery, mat_h, (img_w, img_h))
2021-11-29 18:31:35 +01:00
mask = np.zeros(img.shape[:2], dtype="uint8")
cv.fillPoly(mask, np.int32([dst_points]), (255, 255, 255))
bg_with_black_space = cv.bitwise_and(img, img, mask=cv.bitwise_not(mask))
result = bg_with_black_space + image_transformed_h
cv.imshow('image', result)
2021-11-28 23:44:09 +01:00
vertexes.clear()
if __name__ == '__main__':
vertexes = []
img = cv.imread('../img/billboards.jpg')
2021-11-29 18:31:35 +01:00
img_copy = img.copy()
2021-11-28 23:44:09 +01:00
bakery = cv.imread('../img/bakery.jpg')
bakery_h, bakery_w = bakery.shape[0:2]
2021-11-29 18:02:09 +01:00
img_h, img_w = img.shape[0:2]
2021-11-28 23:44:09 +01:00
cv.imshow('image', img)
cv.setMouseCallback('image', click_event)
cv.waitKey(0)
cv.destroyAllWindows()